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 | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 21 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 22 | #include <string> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 23 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 24 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 26 | #include "base/casts.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 27 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 28 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 30 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "class_linker-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 32 | #include "compiler_callbacks.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 33 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "dex_file-inl.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 35 | #include "gc_root-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "gc/accounting/card_table-inl.h" |
| 37 | #include "gc/accounting/heap_bitmap.h" |
| 38 | #include "gc/heap.h" |
| 39 | #include "gc/space/image_space.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 40 | #include "handle_scope.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 41 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 42 | #include "interpreter/interpreter.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 43 | #include "leb128.h" |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 44 | #include "method_helper-inl.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | #include "oat.h" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 46 | #include "oat_file.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 47 | #include "object_lock.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 48 | #include "mirror/art_field-inl.h" |
| 49 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 50 | #include "mirror/class.h" |
| 51 | #include "mirror/class-inl.h" |
| 52 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 53 | #include "mirror/dex_cache-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | #include "mirror/iftable-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | #include "mirror/object-inl.h" |
| 56 | #include "mirror/object_array-inl.h" |
| 57 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 58 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 60 | #include "mirror/string-inl.h" |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 61 | #include "os.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 62 | #include "runtime.h" |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 63 | #include "entrypoints/entrypoint_utils.h" |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 64 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 66 | #include "handle_scope-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 67 | #include "thread-inl.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 68 | #include "utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 69 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 70 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 71 | |
| 72 | namespace art { |
| 73 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 75 | __attribute__((__format__(__printf__, 1, 2))) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 76 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 77 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 78 | va_list args; |
| 79 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 80 | Thread* self = Thread::Current(); |
| 81 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 82 | self->ThrowNewExceptionV(throw_location, "Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 83 | va_end(args); |
| 84 | } |
| 85 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 86 | static void ThrowEarlierClassFailure(mirror::Class* c) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 87 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 88 | // The class failed to initialize on a previous attempt, so we want to throw |
| 89 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 90 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 91 | // the previous error. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 92 | Runtime* runtime = Runtime::Current(); |
| 93 | bool is_compiler = runtime->IsCompiler(); |
| 94 | if (!is_compiler) { // Give info if this occurs at runtime. |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 95 | LOG(INFO) << "Rejecting re-init on previously-failed class " << PrettyClass(c); |
| 96 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 98 | CHECK(c->IsErroneous()) << PrettyClass(c) << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 99 | Thread* self = Thread::Current(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 100 | if (is_compiler) { |
| 101 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
| 102 | mirror::Throwable* pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
| 103 | self->SetException(ThrowLocation(), pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 104 | } else { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 105 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 106 | if (c->GetVerifyErrorClass() != NULL) { |
| 107 | // TODO: change the verifier to store an _instance_, with a useful detail message? |
| 108 | std::string temp; |
| 109 | self->ThrowNewException(throw_location, c->GetVerifyErrorClass()->GetDescriptor(&temp), |
| 110 | PrettyDescriptor(c).c_str()); |
| 111 | } else { |
| 112 | self->ThrowNewException(throw_location, "Ljava/lang/NoClassDefFoundError;", |
| 113 | PrettyDescriptor(c).c_str()); |
| 114 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 118 | static void WrapExceptionInInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 119 | Thread* self = Thread::Current(); |
| 120 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 121 | |
| 122 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 123 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 124 | |
| 125 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 126 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 127 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 128 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 129 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 130 | if (!is_error) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 131 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 132 | self->ThrowNewWrappedException(throw_location, "Ljava/lang/ExceptionInInitializerError;", |
| 133 | nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 134 | } |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 137 | static size_t Hash(const char* s) { |
| 138 | // This is the java.lang.String hashcode for convenience, not interoperability. |
| 139 | size_t hash = 0; |
| 140 | for (; *s != '\0'; ++s) { |
| 141 | hash = hash * 31 + *s; |
| 142 | } |
| 143 | return hash; |
| 144 | } |
| 145 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 146 | // Gap between two fields in object layout. |
| 147 | struct FieldGap { |
| 148 | uint32_t start_offset; // The offset from the start of the object. |
| 149 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 150 | }; |
| 151 | struct FieldGapsComparator { |
| 152 | explicit FieldGapsComparator() { |
| 153 | } |
| 154 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 155 | NO_THREAD_SAFETY_ANALYSIS { |
| 156 | // Sort by gap size, largest first. |
| 157 | return lhs.size > rhs.size; |
| 158 | } |
| 159 | }; |
| 160 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
| 161 | |
| 162 | // Adds largest aligned gaps to queue of gaps. |
| 163 | void AddFieldGap(uint32_t gap_start, uint32_t gap_end, FieldGaps* gaps) { |
| 164 | DCHECK(gaps != nullptr); |
| 165 | |
| 166 | uint32_t current_offset = gap_start; |
| 167 | while (current_offset != gap_end) { |
| 168 | size_t remaining = gap_end - current_offset; |
| 169 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 170 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 171 | current_offset += sizeof(uint32_t); |
| 172 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 173 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 174 | current_offset += sizeof(uint16_t); |
| 175 | } else { |
| 176 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 177 | current_offset += sizeof(uint8_t); |
| 178 | } |
| 179 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 180 | } |
| 181 | } |
| 182 | // Shuffle fields forward, making use of gaps whenever possible. |
| 183 | template<int n> |
| 184 | static void ShuffleForward(const size_t num_fields, size_t* current_field_idx, |
| 185 | MemberOffset* field_offset, |
| 186 | mirror::ObjectArray<mirror::ArtField>* fields, |
| 187 | std::deque<mirror::ArtField*>* grouped_and_sorted_fields, |
| 188 | FieldGaps* gaps) |
| 189 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 190 | DCHECK(current_field_idx != nullptr); |
| 191 | DCHECK(grouped_and_sorted_fields != nullptr); |
| 192 | DCHECK(fields != nullptr || (num_fields == 0 && grouped_and_sorted_fields->empty())); |
| 193 | DCHECK(gaps != nullptr); |
| 194 | DCHECK(field_offset != nullptr); |
| 195 | |
| 196 | DCHECK(IsPowerOfTwo(n)); |
| 197 | while (!grouped_and_sorted_fields->empty()) { |
| 198 | mirror::ArtField* field = grouped_and_sorted_fields->front(); |
| 199 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 200 | if (Primitive::ComponentSize(type) < n) { |
| 201 | break; |
| 202 | } |
| 203 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 204 | MemberOffset old_offset = *field_offset; |
| 205 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 206 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 207 | } |
| 208 | CHECK(type != Primitive::kPrimNot) << PrettyField(field); // should be primitive types |
| 209 | grouped_and_sorted_fields->pop_front(); |
| 210 | fields->Set<false>(*current_field_idx, field); |
| 211 | if (!gaps->empty() && gaps->top().size >= n) { |
| 212 | FieldGap gap = gaps->top(); |
| 213 | gaps->pop(); |
| 214 | DCHECK(IsAligned<n>(gap.start_offset)); |
| 215 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 216 | if (gap.size > n) { |
| 217 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 218 | } |
| 219 | } else { |
| 220 | DCHECK(IsAligned<n>(field_offset->Uint32Value())); |
| 221 | field->SetOffset(*field_offset); |
| 222 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 223 | } |
| 224 | ++(*current_field_idx); |
| 225 | } |
| 226 | } |
| 227 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 228 | const char* ClassLinker::class_roots_descriptors_[] = { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 229 | "Ljava/lang/Class;", |
| 230 | "Ljava/lang/Object;", |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 231 | "[Ljava/lang/Class;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 232 | "[Ljava/lang/Object;", |
| 233 | "Ljava/lang/String;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 234 | "Ljava/lang/DexCache;", |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 235 | "Ljava/lang/ref/Reference;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 236 | "Ljava/lang/reflect/ArtField;", |
| 237 | "Ljava/lang/reflect/ArtMethod;", |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 238 | "Ljava/lang/reflect/Proxy;", |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 239 | "[Ljava/lang/String;", |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 240 | "[Ljava/lang/reflect/ArtField;", |
| 241 | "[Ljava/lang/reflect/ArtMethod;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 242 | "Ljava/lang/ClassLoader;", |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 243 | "Ljava/lang/Throwable;", |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 244 | "Ljava/lang/ClassNotFoundException;", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 245 | "Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 246 | "Z", |
| 247 | "B", |
| 248 | "C", |
| 249 | "D", |
| 250 | "F", |
| 251 | "I", |
| 252 | "J", |
| 253 | "S", |
| 254 | "V", |
| 255 | "[Z", |
| 256 | "[B", |
| 257 | "[C", |
| 258 | "[D", |
| 259 | "[F", |
| 260 | "[I", |
| 261 | "[J", |
| 262 | "[S", |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 263 | "[Ljava/lang/StackTraceElement;", |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 266 | ClassLinker::ClassLinker(InternTable* intern_table) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 267 | // dex_lock_ is recursive as it may be used in stack dumping. |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 268 | : dex_lock_("ClassLinker dex lock", kDefaultMutexLevel), |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 269 | dex_cache_image_class_lookup_required_(false), |
| 270 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 271 | class_roots_(nullptr), |
| 272 | array_iftable_(nullptr), |
| 273 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 274 | init_done_(false), |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 275 | log_new_dex_caches_roots_(false), |
| 276 | log_new_class_table_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 277 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 278 | portable_resolution_trampoline_(nullptr), |
| 279 | quick_resolution_trampoline_(nullptr), |
| 280 | portable_imt_conflict_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 281 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 282 | quick_generic_jni_trampoline_(nullptr), |
| 283 | quick_to_interpreter_bridge_trampoline_(nullptr) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 284 | CHECK_EQ(arraysize(class_roots_descriptors_), size_t(kClassRootsMax)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 285 | memset(find_array_class_cache_, 0, kFindArrayCacheSize * sizeof(mirror::Class*)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 286 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 287 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 288 | // To set a value for generic JNI. May be necessary in compiler tests. |
| 289 | extern "C" void art_quick_generic_jni_trampoline(mirror::ArtMethod*); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 290 | extern "C" void art_quick_resolution_trampoline(mirror::ArtMethod*); |
| 291 | extern "C" void art_quick_imt_conflict_trampoline(mirror::ArtMethod*); |
| 292 | extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*); |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 293 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 294 | void ClassLinker::InitWithoutImage(const std::vector<const DexFile*>& boot_class_path) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 295 | VLOG(startup) << "ClassLinker::Init"; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 296 | CHECK(!Runtime::Current()->GetHeap()->HasImageSpace()) << "Runtime has image. We should use it."; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 297 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 298 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 299 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 300 | // java_lang_Class comes first, it's needed for AllocClass |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 301 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 302 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 303 | // 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] | 304 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 305 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
| 306 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(down_cast<mirror::Class*>( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 307 | heap->AllocNonMovableObject<true>(self, nullptr, |
| 308 | mirror::Class::ClassClassSize(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 309 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 310 | CHECK(java_lang_Class.Get() != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 311 | mirror::Class::SetClassClass(java_lang_Class.Get()); |
| 312 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 313 | if (kUseBakerOrBrooksReadBarrier) { |
| 314 | java_lang_Class->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 315 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 316 | java_lang_Class->SetClassSize(mirror::Class::ClassClassSize()); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 317 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 318 | heap->DecrementDisableMovingGC(self); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 319 | // AllocClass(mirror::Class*) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 320 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 321 | // Class[] is used for reflection support. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 322 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 323 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Class>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 324 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 325 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 326 | // 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] | 327 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
| 328 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 329 | CHECK(java_lang_Object.Get() != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 330 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 331 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 332 | java_lang_Object->SetStatus(mirror::Class::kStatusLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 333 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 334 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 335 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
| 336 | AllocClass(self, java_lang_Class.Get(), mirror::ObjectArray<mirror::Object>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 337 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 338 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 339 | // Setup the char (primitive) class to be used for char[]. |
| 340 | Handle<mirror::Class> char_class(hs.NewHandle( |
| 341 | AllocClass(self, java_lang_Class.Get(), mirror::Class::PrimitiveClassSize()))); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 342 | // The primitive char class won't be initialized by |
| 343 | // InitializePrimitiveClass until line 459, but strings (and |
| 344 | // internal char arrays) will be allocated before that and the |
| 345 | // component size, which is computed from the primitive type, needs |
| 346 | // to be set here. |
| 347 | char_class->SetPrimitiveType(Primitive::kPrimChar); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 348 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 349 | // Setup the char[] class to be used for String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 350 | Handle<mirror::Class> char_array_class(hs.NewHandle( |
| 351 | AllocClass(self, java_lang_Class.Get(), |
| 352 | mirror::Array::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 353 | char_array_class->SetComponentType(char_class.Get()); |
| 354 | mirror::CharArray::SetArrayClass(char_array_class.Get()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 355 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 356 | // Setup String. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 357 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
| 358 | AllocClass(self, java_lang_Class.Get(), mirror::String::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 359 | mirror::String::SetClass(java_lang_String.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 360 | java_lang_String->SetObjectSize(mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 361 | java_lang_String->SetStatus(mirror::Class::kStatusResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 362 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 363 | // Setup Reference. |
| 364 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
| 365 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize()))); |
| 366 | mirror::Reference::SetClass(java_lang_ref_Reference.Get()); |
| 367 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
| 368 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusResolved, self); |
| 369 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 370 | // 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] | 371 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class> >( |
| 372 | mirror::ObjectArray<mirror::Class>::Alloc(self, object_array_class.Get(), |
| 373 | kClassRootsMax)); |
| 374 | CHECK(!class_roots_.IsNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 375 | SetClassRoot(kJavaLangClass, java_lang_Class.Get()); |
| 376 | SetClassRoot(kJavaLangObject, java_lang_Object.Get()); |
| 377 | SetClassRoot(kClassArrayClass, class_array_class.Get()); |
| 378 | SetClassRoot(kObjectArrayClass, object_array_class.Get()); |
| 379 | SetClassRoot(kCharArrayClass, char_array_class.Get()); |
| 380 | SetClassRoot(kJavaLangString, java_lang_String.Get()); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 381 | SetClassRoot(kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 382 | |
| 383 | // Setup the primitive type classes. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 384 | SetClassRoot(kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 385 | SetClassRoot(kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
| 386 | SetClassRoot(kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 387 | SetClassRoot(kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 388 | SetClassRoot(kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 389 | SetClassRoot(kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 390 | SetClassRoot(kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 391 | SetClassRoot(kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 392 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 393 | // Create array interface entries to populate once we can load system classes. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 394 | array_iftable_ = GcRoot<mirror::IfTable>(AllocIfTable(self, 2)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 395 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 396 | // Create int array type for AllocDexCache (done in AppendToBootClassPath). |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 397 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
| 398 | AllocClass(self, java_lang_Class.Get(), mirror::Array::ClassSize()))); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 399 | int_array_class->SetComponentType(GetClassRoot(kPrimitiveInt)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 400 | mirror::IntArray::SetArrayClass(int_array_class.Get()); |
| 401 | SetClassRoot(kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 402 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 403 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 404 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 405 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 406 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
| 407 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 408 | SetClassRoot(kJavaLangDexCache, java_lang_DexCache.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 409 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 410 | java_lang_DexCache->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 411 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 412 | // Constructor, Field, Method, and AbstractMethod are necessary so |
| 413 | // that FindClass can link members. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 414 | Handle<mirror::Class> java_lang_reflect_ArtField(hs.NewHandle( |
| 415 | AllocClass(self, java_lang_Class.Get(), mirror::ArtField::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 416 | CHECK(java_lang_reflect_ArtField.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 417 | java_lang_reflect_ArtField->SetObjectSize(mirror::ArtField::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 418 | SetClassRoot(kJavaLangReflectArtField, java_lang_reflect_ArtField.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 419 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusResolved, self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 420 | mirror::ArtField::SetClass(java_lang_reflect_ArtField.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 421 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 422 | Handle<mirror::Class> java_lang_reflect_ArtMethod(hs.NewHandle( |
| 423 | AllocClass(self, java_lang_Class.Get(), mirror::ArtMethod::ClassSize()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 424 | CHECK(java_lang_reflect_ArtMethod.Get() != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 425 | java_lang_reflect_ArtMethod->SetObjectSize(mirror::ArtMethod::InstanceSize()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 426 | SetClassRoot(kJavaLangReflectArtMethod, java_lang_reflect_ArtMethod.Get()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 427 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusResolved, self); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 428 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 429 | mirror::ArtMethod::SetClass(java_lang_reflect_ArtMethod.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 430 | |
| 431 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 432 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 433 | AllocClass(self, java_lang_Class.Get(), |
| 434 | mirror::ObjectArray<mirror::String>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 435 | object_array_string->SetComponentType(java_lang_String.Get()); |
| 436 | SetClassRoot(kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 437 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 438 | Handle<mirror::Class> object_array_art_method(hs.NewHandle( |
| 439 | AllocClass(self, java_lang_Class.Get(), |
| 440 | mirror::ObjectArray<mirror::ArtMethod>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 441 | object_array_art_method->SetComponentType(java_lang_reflect_ArtMethod.Get()); |
| 442 | SetClassRoot(kJavaLangReflectArtMethodArrayClass, object_array_art_method.Get()); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 443 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 444 | Handle<mirror::Class> object_array_art_field(hs.NewHandle( |
| 445 | AllocClass(self, java_lang_Class.Get(), |
| 446 | mirror::ObjectArray<mirror::ArtField>::ClassSize()))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 447 | object_array_art_field->SetComponentType(java_lang_reflect_ArtField.Get()); |
| 448 | SetClassRoot(kJavaLangReflectArtFieldArrayClass, object_array_art_field.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 449 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 450 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 451 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 452 | // these roots. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 453 | CHECK_NE(0U, boot_class_path.size()); |
| 454 | for (size_t i = 0; i != boot_class_path.size(); ++i) { |
| 455 | const DexFile* dex_file = boot_class_path[i]; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 456 | CHECK(dex_file != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 457 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 458 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 459 | |
| 460 | // now we can use FindSystemClass |
| 461 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 462 | // run char class through InitializePrimitiveClass to finish init |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 463 | InitializePrimitiveClass(char_class.Get(), Primitive::kPrimChar); |
| 464 | SetClassRoot(kPrimitiveChar, char_class.Get()); // needs descriptor |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 465 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 466 | // Create runtime resolution and imt conflict methods. Also setup the default imt. |
| 467 | Runtime* runtime = Runtime::Current(); |
| 468 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
| 469 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod()); |
| 470 | runtime->SetDefaultImt(runtime->CreateDefaultImt(this)); |
| 471 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 472 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 473 | // we do not need friend classes or a publicly exposed setter. |
| 474 | quick_generic_jni_trampoline_ = reinterpret_cast<void*>(art_quick_generic_jni_trampoline); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 475 | if (!runtime->IsCompiler()) { |
| 476 | // We need to set up the generic trampolines since we don't have an image. |
| 477 | quick_resolution_trampoline_ = reinterpret_cast<void*>(art_quick_resolution_trampoline); |
| 478 | quick_imt_conflict_trampoline_ = reinterpret_cast<void*>(art_quick_imt_conflict_trampoline); |
| 479 | quick_to_interpreter_bridge_trampoline_ = reinterpret_cast<void*>(art_quick_to_interpreter_bridge); |
| 480 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 481 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 482 | // 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] | 483 | java_lang_Object->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 484 | mirror::Class* Object_class = FindSystemClass(self, "Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 485 | CHECK_EQ(java_lang_Object.Get(), Object_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 486 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 487 | java_lang_String->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 488 | mirror::Class* String_class = FindSystemClass(self, "Ljava/lang/String;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 489 | std::ostringstream os1, os2; |
| 490 | java_lang_String->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 491 | String_class->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 492 | CHECK_EQ(java_lang_String.Get(), String_class) << os1.str() << "\n\n" << os2.str(); |
| 493 | CHECK_EQ(java_lang_String->GetObjectSize(), mirror::String::InstanceSize()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 494 | java_lang_DexCache->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 495 | mirror::Class* DexCache_class = FindSystemClass(self, "Ljava/lang/DexCache;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 496 | CHECK_EQ(java_lang_String.Get(), String_class); |
| 497 | CHECK_EQ(java_lang_DexCache.Get(), DexCache_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 498 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 499 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 500 | // 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] | 501 | SetClassRoot(kBooleanArrayClass, FindSystemClass(self, "[Z")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 502 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 503 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 504 | SetClassRoot(kByteArrayClass, FindSystemClass(self, "[B")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 505 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 506 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 507 | mirror::Class* found_char_array_class = FindSystemClass(self, "[C"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 508 | CHECK_EQ(char_array_class.Get(), found_char_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 509 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 510 | SetClassRoot(kShortArrayClass, FindSystemClass(self, "[S")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 511 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 512 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 513 | mirror::Class* found_int_array_class = FindSystemClass(self, "[I"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 514 | CHECK_EQ(int_array_class.Get(), found_int_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 515 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 516 | SetClassRoot(kLongArrayClass, FindSystemClass(self, "[J")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 517 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 518 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 519 | SetClassRoot(kFloatArrayClass, FindSystemClass(self, "[F")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 520 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 521 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 522 | SetClassRoot(kDoubleArrayClass, FindSystemClass(self, "[D")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 523 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 524 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 525 | mirror::Class* found_class_array_class = FindSystemClass(self, "[Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 526 | CHECK_EQ(class_array_class.Get(), found_class_array_class); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 527 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 528 | mirror::Class* found_object_array_class = FindSystemClass(self, "[Ljava/lang/Object;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 529 | CHECK_EQ(object_array_class.Get(), found_object_array_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 530 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 531 | // Setup the single, global copy of "iftable". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 532 | mirror::Class* java_lang_Cloneable = FindSystemClass(self, "Ljava/lang/Cloneable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 533 | CHECK(java_lang_Cloneable != nullptr); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 534 | mirror::Class* java_io_Serializable = FindSystemClass(self, "Ljava/io/Serializable;"); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 535 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 536 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 537 | // crawl up and explicitly list all of the supers as well. |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 538 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 539 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 540 | array_iftable->SetInterface(0, java_lang_Cloneable); |
| 541 | array_iftable->SetInterface(1, java_io_Serializable); |
| 542 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 543 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 544 | // Sanity check Class[] and Object[]'s interfaces. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 545 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, class_array_class, 0)); |
| 546 | CHECK_EQ(java_io_Serializable, mirror::Class::GetDirectInterface(self, class_array_class, 1)); |
| 547 | CHECK_EQ(java_lang_Cloneable, mirror::Class::GetDirectInterface(self, object_array_class, 0)); |
| 548 | 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] | 549 | // Run Class, ArtField, and ArtMethod through FindSystemClass. This initializes their |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 550 | // dex_cache_ fields and register them in class_table_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 551 | mirror::Class* Class_class = FindSystemClass(self, "Ljava/lang/Class;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 552 | CHECK_EQ(java_lang_Class.Get(), Class_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 553 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 554 | java_lang_reflect_ArtMethod->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 555 | mirror::Class* Art_method_class = FindSystemClass(self, "Ljava/lang/reflect/ArtMethod;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 556 | CHECK_EQ(java_lang_reflect_ArtMethod.Get(), Art_method_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 557 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 558 | java_lang_reflect_ArtField->SetStatus(mirror::Class::kStatusNotReady, self); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 559 | mirror::Class* Art_field_class = FindSystemClass(self, "Ljava/lang/reflect/ArtField;"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 560 | CHECK_EQ(java_lang_reflect_ArtField.Get(), Art_field_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 561 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 562 | mirror::Class* String_array_class = |
| 563 | FindSystemClass(self, class_roots_descriptors_[kJavaLangStringArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 564 | CHECK_EQ(object_array_string.Get(), String_array_class); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 565 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 566 | mirror::Class* Art_method_array_class = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 567 | FindSystemClass(self, class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 568 | CHECK_EQ(object_array_art_method.Get(), Art_method_array_class); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 569 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 570 | mirror::Class* Art_field_array_class = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 571 | FindSystemClass(self, class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 572 | CHECK_EQ(object_array_art_field.Get(), Art_field_array_class); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 573 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 574 | // End of special init trickery, subsequent classes may be loaded via FindSystemClass. |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 575 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 576 | // Create java.lang.reflect.Proxy root. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 577 | mirror::Class* java_lang_reflect_Proxy = FindSystemClass(self, "Ljava/lang/reflect/Proxy;"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 578 | SetClassRoot(kJavaLangReflectProxy, java_lang_reflect_Proxy); |
| 579 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 580 | // 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] | 581 | // finish initializing Reference class |
| 582 | java_lang_ref_Reference->SetStatus(mirror::Class::kStatusNotReady, self); |
| 583 | mirror::Class* Reference_class = FindSystemClass(self, "Ljava/lang/ref/Reference;"); |
| 584 | CHECK_EQ(java_lang_ref_Reference.Get(), Reference_class); |
| 585 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
| 586 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), mirror::Reference::ClassSize()); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 587 | mirror::Class* java_lang_ref_FinalizerReference = |
| 588 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 589 | java_lang_ref_FinalizerReference->SetAccessFlags( |
| 590 | java_lang_ref_FinalizerReference->GetAccessFlags() | |
| 591 | kAccClassIsReference | kAccClassIsFinalizerReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 592 | mirror::Class* java_lang_ref_PhantomReference = |
| 593 | FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 594 | java_lang_ref_PhantomReference->SetAccessFlags( |
| 595 | java_lang_ref_PhantomReference->GetAccessFlags() | |
| 596 | kAccClassIsReference | kAccClassIsPhantomReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 597 | mirror::Class* java_lang_ref_SoftReference = |
| 598 | FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 599 | java_lang_ref_SoftReference->SetAccessFlags( |
| 600 | java_lang_ref_SoftReference->GetAccessFlags() | kAccClassIsReference); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 601 | mirror::Class* java_lang_ref_WeakReference = |
| 602 | FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 603 | java_lang_ref_WeakReference->SetAccessFlags( |
| 604 | java_lang_ref_WeakReference->GetAccessFlags() | |
| 605 | kAccClassIsReference | kAccClassIsWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 606 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 607 | // Setup the ClassLoader, verifying the object_size_. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 608 | mirror::Class* java_lang_ClassLoader = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 609 | CHECK_EQ(java_lang_ClassLoader->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 610 | SetClassRoot(kJavaLangClassLoader, java_lang_ClassLoader); |
| 611 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 612 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 613 | // java.lang.StackTraceElement as a convenience. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 614 | SetClassRoot(kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 615 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 616 | SetClassRoot(kJavaLangClassNotFoundException, |
| 617 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 618 | SetClassRoot(kJavaLangStackTraceElement, FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 619 | SetClassRoot(kJavaLangStackTraceElementArrayClass, |
| 620 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 621 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 622 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 623 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 624 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 625 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 628 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 629 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 630 | |
| 631 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 632 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 633 | // as the types of the field can't be resolved prior to the runtime being |
| 634 | // fully initialized |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 635 | mirror::Class* java_lang_ref_Reference = GetClassRoot(kJavaLangRefReference); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 636 | mirror::Class* java_lang_ref_FinalizerReference = |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 637 | FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 638 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 639 | mirror::ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 640 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 641 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 642 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 643 | mirror::ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 644 | CHECK_STREQ(queue->GetName(), "queue"); |
| 645 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 646 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 647 | mirror::ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 648 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 649 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 650 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 651 | mirror::ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 652 | CHECK_STREQ(referent->GetName(), "referent"); |
| 653 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 654 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 655 | mirror::ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 656 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 657 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 658 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 659 | // ensure all class_roots_ are initialized |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 660 | for (size_t i = 0; i < kClassRootsMax; i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 661 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 662 | mirror::Class* klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 663 | CHECK(klass != nullptr); |
| 664 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 665 | // note SetClassRoot does additional validation. |
| 666 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 669 | CHECK(!array_iftable_.IsNull()); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 670 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 671 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 672 | // that Object, Class, and Object[] are setup |
| 673 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 674 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 675 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 678 | void ClassLinker::RunRootClinits() { |
| 679 | Thread* self = Thread::Current(); |
| 680 | for (size_t i = 0; i < ClassLinker::kClassRootsMax; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 681 | mirror::Class* c = GetClassRoot(ClassRoot(i)); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 682 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 683 | StackHandleScope<1> hs(self); |
| 684 | Handle<mirror::Class> h_class(hs.NewHandle(GetClassRoot(ClassRoot(i)))); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 685 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 686 | self->AssertNoPendingException(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 691 | bool ClassLinker::GenerateOatFile(const char* dex_filename, |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 692 | int oat_fd, |
Vladimir Marko | 60836d5 | 2014-01-16 15:53:38 +0000 | [diff] [blame] | 693 | const char* oat_cache_filename, |
| 694 | std::string* error_msg) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 695 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 696 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 697 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 698 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 699 | std::string boot_image_option("--boot-image="); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 700 | if (heap->GetImageSpace() == nullptr) { |
| 701 | // TODO If we get a dex2dex compiler working we could maybe use that, OTOH since we are likely |
| 702 | // out of space anyway it might not matter. |
| 703 | *error_msg = StringPrintf("Cannot create oat file for '%s' because we are running " |
| 704 | "without an image.", dex_filename); |
| 705 | return false; |
| 706 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 707 | boot_image_option += heap->GetImageSpace()->GetImageLocation(); |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 708 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 709 | std::string dex_file_option("--dex-file="); |
| 710 | dex_file_option += dex_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 711 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 712 | std::string oat_fd_option("--oat-fd="); |
| 713 | StringAppendF(&oat_fd_option, "%d", oat_fd); |
Brian Carlstrom | d601af8 | 2012-01-06 10:15:19 -0800 | [diff] [blame] | 714 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 715 | std::string oat_location_option("--oat-location="); |
| 716 | oat_location_option += oat_cache_filename; |
Brian Carlstrom | 29e7ac7 | 2011-12-05 23:42:57 -0800 | [diff] [blame] | 717 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 718 | std::vector<std::string> argv; |
| 719 | argv.push_back(dex2oat); |
| 720 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 721 | argv.push_back("-classpath"); |
| 722 | argv.push_back("--runtime-arg"); |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 723 | argv.push_back(Runtime::Current()->GetClassPathString()); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 724 | |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 725 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 726 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 727 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 728 | argv.push_back("--compiler-filter=verify-none"); |
| 729 | } |
| 730 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 731 | if (Runtime::Current()->MustRelocateIfPossible()) { |
| 732 | argv.push_back("--runtime-arg"); |
| 733 | argv.push_back("-Xrelocate"); |
| 734 | } else { |
| 735 | argv.push_back("--runtime-arg"); |
| 736 | argv.push_back("-Xnorelocate"); |
| 737 | } |
| 738 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 739 | if (!kIsTargetBuild) { |
| 740 | argv.push_back("--host"); |
buzbee | a024a06 | 2013-07-31 10:47:37 -0700 | [diff] [blame] | 741 | } |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 742 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 743 | argv.push_back(boot_image_option); |
| 744 | argv.push_back(dex_file_option); |
| 745 | argv.push_back(oat_fd_option); |
| 746 | argv.push_back(oat_location_option); |
| 747 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 748 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 749 | argv.push_back(compiler_options[i].c_str()); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 750 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 751 | |
| 752 | return Exec(argv, error_msg); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 755 | const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 756 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0d6adac | 2014-02-05 17:39:16 -0800 | [diff] [blame] | 757 | if (kIsDebugBuild) { |
| 758 | for (size_t i = 0; i < oat_files_.size(); ++i) { |
| 759 | CHECK_NE(oat_file, oat_files_[i]) << oat_file->GetLocation(); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 760 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 761 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 762 | VLOG(class_linker) << "Registering " << oat_file->GetLocation(); |
| 763 | oat_files_.push_back(oat_file); |
| 764 | return oat_file; |
Brian Carlstrom | 866c862 | 2012-01-06 16:35:13 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 767 | OatFile& ClassLinker::GetImageOatFile(gc::space::ImageSpace* space) { |
| 768 | VLOG(startup) << "ClassLinker::GetImageOatFile entering"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 769 | OatFile* oat_file = space->ReleaseOatFile(); |
| 770 | CHECK_EQ(RegisterOatFile(oat_file), oat_file); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 771 | VLOG(startup) << "ClassLinker::GetImageOatFile exiting"; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 772 | return *oat_file; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 775 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFileForDexFile(const DexFile& dex_file) { |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 776 | const char* dex_location = dex_file.GetLocation().c_str(); |
| 777 | uint32_t dex_location_checksum = dex_file.GetLocationChecksum(); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 778 | return FindOpenedOatDexFile(nullptr, dex_location, &dex_location_checksum); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 781 | const OatFile::OatDexFile* ClassLinker::FindOpenedOatDexFile(const char* oat_location, |
| 782 | const char* dex_location, |
| 783 | const uint32_t* dex_location_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 784 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 785 | for (const OatFile* oat_file : oat_files_) { |
| 786 | DCHECK(oat_file != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 787 | |
| 788 | if (oat_location != nullptr) { |
| 789 | if (oat_file->GetLocation() != oat_location) { |
| 790 | continue; |
| 791 | } |
| 792 | } |
| 793 | |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 794 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 795 | dex_location_checksum, |
Brian Carlstrom | 756ee4e | 2013-10-03 15:46:12 -0700 | [diff] [blame] | 796 | false); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 797 | if (oat_dex_file != nullptr) { |
| 798 | return oat_dex_file; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 801 | return nullptr; |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 804 | |
| 805 | // Loads all multi dex files from the given oat file returning true on success. |
| 806 | // |
| 807 | // Parameters: |
| 808 | // oat_file - the oat file to load from |
| 809 | // dex_location - the dex location used to generate the oat file |
| 810 | // dex_location_checksum - the checksum of the dex_location (may be null for pre-opted files) |
| 811 | // generated - whether or not the oat_file existed before or was just (re)generated |
| 812 | // error_msgs - any error messages will be appended here |
| 813 | // dex_files - the loaded dex_files will be appended here (only if the loading succeeds) |
| 814 | static bool LoadMultiDexFilesFromOatFile(const OatFile* oat_file, |
| 815 | const char* dex_location, |
| 816 | const uint32_t* dex_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 817 | bool generated, |
| 818 | std::vector<std::string>* error_msgs, |
| 819 | std::vector<const DexFile*>* dex_files) { |
| 820 | if (oat_file == nullptr) { |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | size_t old_size = dex_files->size(); // To rollback on error. |
| 825 | |
| 826 | bool success = true; |
| 827 | for (size_t i = 0; success; ++i) { |
Calin Juravle | 4e1d579 | 2014-07-15 23:56:47 +0100 | [diff] [blame] | 828 | std::string next_name_str = DexFile::GetMultiDexClassesDexName(i, dex_location); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 829 | const char* next_name = next_name_str.c_str(); |
| 830 | |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 831 | uint32_t next_location_checksum; |
| 832 | uint32_t* next_location_checksum_pointer = &next_location_checksum; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 833 | std::string error_msg; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 834 | if ((i == 0) && (strcmp(next_name, dex_location) == 0)) { |
| 835 | // When i=0 the multidex name should be the same as the location name. We already have the |
| 836 | // checksum it so we don't need to recompute it. |
| 837 | if (dex_location_checksum == nullptr) { |
| 838 | next_location_checksum_pointer = nullptr; |
| 839 | } else { |
| 840 | next_location_checksum = *dex_location_checksum; |
| 841 | } |
| 842 | } else if (!DexFile::GetChecksum(next_name, next_location_checksum_pointer, &error_msg)) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 843 | DCHECK_EQ(false, i == 0 && generated); |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 844 | next_location_checksum_pointer = nullptr; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(next_name, nullptr, false); |
| 848 | |
| 849 | if (oat_dex_file == nullptr) { |
| 850 | if (i == 0 && generated) { |
| 851 | std::string error_msg; |
| 852 | 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] | 853 | " file'%s'", dex_location, next_location_checksum, |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 854 | oat_file->GetLocation().c_str()); |
| 855 | error_msgs->push_back(error_msg); |
| 856 | } |
| 857 | break; // Not found, done. |
| 858 | } |
| 859 | |
| 860 | // Checksum test. Test must succeed when generated. |
| 861 | success = !generated; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 862 | if (next_location_checksum_pointer != nullptr) { |
| 863 | success = next_location_checksum == oat_dex_file->GetDexFileLocationChecksum(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (success) { |
| 867 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
| 868 | if (dex_file == nullptr) { |
| 869 | success = false; |
| 870 | error_msgs->push_back(error_msg); |
| 871 | } else { |
| 872 | dex_files->push_back(dex_file); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // When we generated the file, we expect success, or something is terribly wrong. |
| 877 | CHECK_EQ(false, generated && !success) |
| 878 | << "dex_location=" << next_name << " oat_location=" << oat_file->GetLocation().c_str() |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 879 | << std::hex << " dex_location_checksum=" << next_location_checksum |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 880 | << " OatDexFile::GetLocationChecksum()=" << oat_dex_file->GetDexFileLocationChecksum(); |
| 881 | } |
| 882 | |
| 883 | if (dex_files->size() == old_size) { |
| 884 | success = false; // We did not even find classes.dex |
| 885 | } |
| 886 | |
| 887 | if (success) { |
| 888 | return true; |
| 889 | } else { |
| 890 | // Free all the dex files we have loaded. |
| 891 | auto it = dex_files->begin() + old_size; |
| 892 | auto it_end = dex_files->end(); |
| 893 | for (; it != it_end; it++) { |
| 894 | delete *it; |
| 895 | } |
| 896 | dex_files->erase(dex_files->begin() + old_size, it_end); |
| 897 | |
| 898 | return false; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // Multidex files make it possible that some, but not all, dex files can be broken/outdated. This |
| 903 | // complicates the loading process, as we should not use an iterative loading process, because that |
| 904 | // would register the oat file and dex files that come before the broken one. Instead, check all |
| 905 | // multidex ahead of time. |
| 906 | bool ClassLinker::OpenDexFilesFromOat(const char* dex_location, const char* oat_location, |
| 907 | std::vector<std::string>* error_msgs, |
| 908 | std::vector<const DexFile*>* dex_files) { |
| 909 | // 1) Check whether we have an open oat file. |
| 910 | // This requires a dex checksum, use the "primary" one. |
| 911 | uint32_t dex_location_checksum; |
| 912 | uint32_t* dex_location_checksum_pointer = &dex_location_checksum; |
| 913 | bool have_checksum = true; |
| 914 | std::string checksum_error_msg; |
| 915 | if (!DexFile::GetChecksum(dex_location, dex_location_checksum_pointer, &checksum_error_msg)) { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 916 | // 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] | 917 | dex_location_checksum_pointer = nullptr; |
| 918 | have_checksum = false; |
| 919 | } |
| 920 | |
| 921 | bool needs_registering = false; |
| 922 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 923 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFile(oat_location, dex_location, |
| 924 | dex_location_checksum_pointer); |
| 925 | std::unique_ptr<const OatFile> open_oat_file( |
| 926 | oat_dex_file != nullptr ? oat_dex_file->GetOatFile() : nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 927 | |
| 928 | // 2) If we do not have an open one, maybe there's one on disk already. |
| 929 | |
| 930 | // In case the oat file is not open, we play a locking game here so |
| 931 | // that if two different processes race to load and register or generate |
| 932 | // (or worse, one tries to open a partial generated file) we will be okay. |
| 933 | // This is actually common with apps that use DexClassLoader to work |
| 934 | // around the dex method reference limit and that have a background |
| 935 | // service running in a separate process. |
| 936 | ScopedFlock scoped_flock; |
| 937 | |
| 938 | if (open_oat_file.get() == nullptr) { |
| 939 | if (oat_location != nullptr) { |
| 940 | // Can only do this if we have a checksum, else error. |
| 941 | if (!have_checksum) { |
| 942 | error_msgs->push_back(checksum_error_msg); |
| 943 | return false; |
| 944 | } |
| 945 | |
| 946 | std::string error_msg; |
| 947 | |
| 948 | // We are loading or creating one in the future. Time to set up the file lock. |
| 949 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 950 | error_msgs->push_back(error_msg); |
| 951 | return false; |
| 952 | } |
| 953 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 954 | // 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] | 955 | open_oat_file.reset(FindOatFileInOatLocationForDexFile(dex_location, dex_location_checksum, |
| 956 | oat_location, &error_msg)); |
| 957 | |
| 958 | if (open_oat_file.get() == nullptr) { |
| 959 | std::string compound_msg = StringPrintf("Failed to find dex file '%s' in oat location '%s': %s", |
| 960 | dex_location, oat_location, error_msg.c_str()); |
| 961 | VLOG(class_linker) << compound_msg; |
| 962 | error_msgs->push_back(compound_msg); |
| 963 | } |
| 964 | } else { |
| 965 | // TODO: What to lock here? |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 966 | bool obsolete_file_cleanup_failed; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 967 | open_oat_file.reset(FindOatFileContainingDexFileFromDexLocation(dex_location, |
| 968 | dex_location_checksum_pointer, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 969 | kRuntimeISA, error_msgs, |
| 970 | &obsolete_file_cleanup_failed)); |
| 971 | // There's no point in going forward and eventually try to regenerate the |
| 972 | // file if we couldn't remove the obsolete one. Mostly likely we will fail |
| 973 | // with the same error when trying to write the new file. |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 974 | // TODO: should we maybe do this only when we get permission issues? (i.e. EACCESS). |
| 975 | if (obsolete_file_cleanup_failed) { |
| 976 | return false; |
| 977 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 978 | } |
| 979 | needs_registering = true; |
| 980 | } |
| 981 | |
| 982 | // 3) If we have an oat file, check all contained multidex files for our dex_location. |
| 983 | // Note: LoadMultiDexFilesFromOatFile will check for nullptr in the first argument. |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 984 | bool success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 985 | dex_location_checksum_pointer, |
| 986 | false, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 987 | if (success) { |
| 988 | const OatFile* oat_file = open_oat_file.release(); // Avoid deleting it. |
| 989 | if (needs_registering) { |
| 990 | // We opened the oat file, so we must register it. |
| 991 | RegisterOatFile(oat_file); |
| 992 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 993 | // If the file isn't executable we failed patchoat but did manage to get the dex files. |
| 994 | return oat_file->IsExecutable(); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 995 | } else { |
| 996 | if (needs_registering) { |
| 997 | // We opened it, delete it. |
| 998 | open_oat_file.reset(); |
| 999 | } else { |
| 1000 | open_oat_file.release(); // Do not delete open oat files. |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // 4) If it's not the case (either no oat file or mismatches), regenerate and load. |
| 1005 | |
| 1006 | // Need a checksum, fail else. |
| 1007 | if (!have_checksum) { |
| 1008 | error_msgs->push_back(checksum_error_msg); |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | // Look in cache location if no oat_location is given. |
| 1013 | std::string cache_location; |
| 1014 | if (oat_location == nullptr) { |
| 1015 | // Use the dalvik cache. |
| 1016 | const std::string dalvik_cache(GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA))); |
| 1017 | cache_location = GetDalvikCacheFilenameOrDie(dex_location, dalvik_cache.c_str()); |
| 1018 | oat_location = cache_location.c_str(); |
| 1019 | } |
| 1020 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1021 | bool has_flock = true; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1022 | // Definitely need to lock now. |
| 1023 | if (!scoped_flock.HasFile()) { |
| 1024 | std::string error_msg; |
| 1025 | if (!scoped_flock.Init(oat_location, &error_msg)) { |
| 1026 | error_msgs->push_back(error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1027 | has_flock = false; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1031 | if (Runtime::Current()->IsDex2OatEnabled() && has_flock && scoped_flock.HasFile()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1032 | // Create the oat file. |
| 1033 | open_oat_file.reset(CreateOatFileForDexLocation(dex_location, scoped_flock.GetFile()->Fd(), |
| 1034 | oat_location, error_msgs)); |
| 1035 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1036 | |
| 1037 | // Failed, bail. |
| 1038 | if (open_oat_file.get() == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 1039 | std::string error_msg; |
| 1040 | // dex2oat was disabled or crashed. Add the dex file in the list of dex_files to make progress. |
| 1041 | DexFile::Open(dex_location, dex_location, &error_msg, dex_files); |
| 1042 | error_msgs->push_back(error_msg); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1043 | return false; |
| 1044 | } |
| 1045 | |
| 1046 | // Try to load again, but stronger checks. |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1047 | success = LoadMultiDexFilesFromOatFile(open_oat_file.get(), dex_location, |
| 1048 | dex_location_checksum_pointer, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1049 | true, error_msgs, dex_files); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1050 | if (success) { |
| 1051 | RegisterOatFile(open_oat_file.release()); |
| 1052 | return true; |
| 1053 | } else { |
| 1054 | return false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | const OatFile* ClassLinker::FindOatFileInOatLocationForDexFile(const char* dex_location, |
| 1059 | uint32_t dex_location_checksum, |
| 1060 | const char* oat_location, |
| 1061 | std::string* error_msg) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1062 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1063 | !Runtime::Current()->IsCompiler(), |
| 1064 | error_msg)); |
| 1065 | if (oat_file.get() == nullptr) { |
| 1066 | *error_msg = StringPrintf("Failed to find existing oat file at %s: %s", oat_location, |
| 1067 | error_msg->c_str()); |
| 1068 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1069 | } |
Brian Carlstrom | 81f3ca1 | 2012-03-17 00:27:35 -0700 | [diff] [blame] | 1070 | Runtime* runtime = Runtime::Current(); |
Alex Light | 7adb7ac | 2014-08-29 10:28:25 -0700 | [diff] [blame] | 1071 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
| 1072 | if (image_space != nullptr) { |
| 1073 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1074 | uint32_t expected_image_oat_checksum = image_header.GetOatChecksum(); |
| 1075 | uint32_t actual_image_oat_checksum = oat_file->GetOatHeader().GetImageFileLocationOatChecksum(); |
| 1076 | if (expected_image_oat_checksum != actual_image_oat_checksum) { |
| 1077 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat checksum of " |
| 1078 | "0x%x, found 0x%x", oat_location, expected_image_oat_checksum, |
| 1079 | actual_image_oat_checksum); |
| 1080 | return nullptr; |
| 1081 | } |
| 1082 | |
| 1083 | uintptr_t expected_image_oat_offset = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
| 1084 | uint32_t actual_image_oat_offset = oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(); |
| 1085 | if (expected_image_oat_offset != actual_image_oat_offset) { |
| 1086 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected image oat offset %" |
| 1087 | PRIuPTR ", found %ud", oat_location, expected_image_oat_offset, |
| 1088 | actual_image_oat_offset); |
| 1089 | return nullptr; |
| 1090 | } |
| 1091 | int32_t expected_patch_delta = image_header.GetPatchDelta(); |
| 1092 | int32_t actual_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
| 1093 | if (expected_patch_delta != actual_patch_delta) { |
| 1094 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected patch delta %d, " |
| 1095 | " found %d", oat_location, expected_patch_delta, actual_patch_delta); |
| 1096 | return nullptr; |
| 1097 | } |
Brian Carlstrom | 28db012 | 2012-10-18 16:20:41 -0700 | [diff] [blame] | 1098 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1099 | |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1100 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1101 | &dex_location_checksum); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1102 | if (oat_dex_file == nullptr) { |
| 1103 | *error_msg = StringPrintf("Failed to find oat file at '%s' containing '%s'", oat_location, |
| 1104 | dex_location); |
| 1105 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1106 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1107 | uint32_t expected_dex_checksum = dex_location_checksum; |
| 1108 | uint32_t actual_dex_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 1109 | if (expected_dex_checksum != actual_dex_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1110 | *error_msg = StringPrintf("Failed to find oat file at '%s' with expected dex checksum of 0x%x, " |
| 1111 | "found 0x%x", oat_location, expected_dex_checksum, |
| 1112 | actual_dex_checksum); |
| 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 | std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(error_msg)); |
| 1116 | if (dex_file.get() != nullptr) { |
| 1117 | return oat_file.release(); |
| 1118 | } else { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1119 | return nullptr; |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1120 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1121 | } |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1122 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1123 | const OatFile* ClassLinker::CreateOatFileForDexLocation(const char* dex_location, |
| 1124 | int fd, const char* oat_location, |
| 1125 | std::vector<std::string>* error_msgs) { |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1126 | // Generate the output oat file for the dex file |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 1127 | VLOG(class_linker) << "Generating oat file " << oat_location << " for " << dex_location; |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1128 | std::string error_msg; |
| 1129 | if (!GenerateOatFile(dex_location, fd, oat_location, &error_msg)) { |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1130 | CHECK(!error_msg.empty()); |
| 1131 | error_msgs->push_back(error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1132 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1133 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1134 | std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_location, oat_location, nullptr, |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1135 | !Runtime::Current()->IsCompiler(), |
| 1136 | &error_msg)); |
| 1137 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1138 | std::string compound_msg = StringPrintf("\nFailed to open generated oat file '%s': %s", |
| 1139 | oat_location, error_msg.c_str()); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1140 | error_msgs->push_back(compound_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1141 | return nullptr; |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1142 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1143 | |
| 1144 | return oat_file.release(); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1147 | bool ClassLinker::VerifyOatImageChecksum(const OatFile* oat_file, |
| 1148 | const InstructionSet instruction_set) { |
| 1149 | Runtime* runtime = Runtime::Current(); |
| 1150 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1151 | if (image_space == nullptr) { |
| 1152 | return false; |
| 1153 | } |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1154 | uint32_t image_oat_checksum = 0; |
| 1155 | if (instruction_set == kRuntimeISA) { |
| 1156 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1157 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1158 | } else { |
| 1159 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1160 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1161 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1162 | } |
| 1163 | return oat_file->GetOatHeader().GetImageFileLocationOatChecksum() == image_oat_checksum; |
| 1164 | } |
| 1165 | |
| 1166 | bool ClassLinker::VerifyOatChecksums(const OatFile* oat_file, |
| 1167 | const InstructionSet instruction_set, |
| 1168 | std::string* error_msg) { |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1169 | Runtime* runtime = Runtime::Current(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1170 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1171 | if (image_space == nullptr) { |
| 1172 | *error_msg = "No image space for verification against"; |
| 1173 | return false; |
| 1174 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1175 | |
| 1176 | // If the requested instruction set is the same as the current runtime, |
| 1177 | // we can use the checksums directly. If it isn't, we'll have to read the |
| 1178 | // image header from the image for the right instruction set. |
| 1179 | uint32_t image_oat_checksum = 0; |
| 1180 | uintptr_t image_oat_data_begin = 0; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1181 | int32_t image_patch_delta = 0; |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 1182 | if (instruction_set == runtime->GetInstructionSet()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1183 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1184 | image_oat_checksum = image_header.GetOatChecksum(); |
| 1185 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1186 | image_patch_delta = image_header.GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1187 | } else { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1188 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1189 | image_space->GetImageLocation().c_str(), instruction_set)); |
| 1190 | image_oat_checksum = image_header->GetOatChecksum(); |
| 1191 | image_oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1192 | image_patch_delta = image_header->GetPatchDelta(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1193 | } |
| 1194 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1195 | bool ret = ((oat_header.GetImageFileLocationOatChecksum() == image_oat_checksum) |
| 1196 | && (oat_header.GetImagePatchDelta() == image_patch_delta) |
| 1197 | && (oat_header.GetImageFileLocationOatDataBegin() == image_oat_data_begin)); |
| 1198 | if (!ret) { |
| 1199 | *error_msg = StringPrintf("oat file '%s' mismatch (0x%x, %d, %d) with (0x%x, %" PRIdPTR ", %d)", |
| 1200 | oat_file->GetLocation().c_str(), |
| 1201 | oat_file->GetOatHeader().GetImageFileLocationOatChecksum(), |
| 1202 | oat_file->GetOatHeader().GetImageFileLocationOatDataBegin(), |
| 1203 | oat_file->GetOatHeader().GetImagePatchDelta(), |
| 1204 | image_oat_checksum, image_oat_data_begin, image_patch_delta); |
| 1205 | } |
| 1206 | return ret; |
| 1207 | } |
| 1208 | |
| 1209 | bool ClassLinker::VerifyOatAndDexFileChecksums(const OatFile* oat_file, |
| 1210 | const char* dex_location, |
| 1211 | uint32_t dex_location_checksum, |
| 1212 | const InstructionSet instruction_set, |
| 1213 | std::string* error_msg) { |
| 1214 | if (!VerifyOatChecksums(oat_file, instruction_set, error_msg)) { |
| 1215 | return false; |
| 1216 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1217 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1218 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, |
| 1219 | &dex_location_checksum); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1220 | if (oat_dex_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1221 | *error_msg = StringPrintf("oat file '%s' does not contain contents for '%s' with checksum 0x%x", |
| 1222 | oat_file->GetLocation().c_str(), dex_location, dex_location_checksum); |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1223 | for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { |
| 1224 | *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] | 1225 | oat_file->GetLocation().c_str(), |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1226 | oat_dex_file->GetDexFileLocation().c_str(), |
| 1227 | oat_dex_file->GetDexFileLocationChecksum()); |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1228 | } |
| 1229 | return false; |
| 1230 | } |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1231 | |
Calin Juravle | a8c55ae | 2014-09-05 16:14:19 +0100 | [diff] [blame] | 1232 | DCHECK_EQ(dex_location_checksum, oat_dex_file->GetDexFileLocationChecksum()); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1233 | return true; |
Brian Carlstrom | afe2551 | 2012-06-27 17:02:58 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1236 | bool ClassLinker::VerifyOatWithDexFile(const OatFile* oat_file, |
| 1237 | const char* dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1238 | const uint32_t* dex_location_checksum, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1239 | std::string* error_msg) { |
| 1240 | CHECK(oat_file != nullptr); |
| 1241 | CHECK(dex_location != nullptr); |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1242 | std::unique_ptr<const DexFile> dex_file; |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1243 | if (dex_location_checksum == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1244 | // If no classes.dex found in dex_location, it has been stripped or is corrupt, assume oat is |
| 1245 | // up-to-date. This is the common case in user builds for jar's and apk's in the /system |
| 1246 | // directory. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1247 | const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location, nullptr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1248 | if (oat_dex_file == nullptr) { |
| 1249 | *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] | 1250 | "dex file '%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1251 | error_msg->c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1252 | return false; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1253 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1254 | dex_file.reset(oat_dex_file->OpenDexFile(error_msg)); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1255 | } else { |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1256 | bool verified = VerifyOatAndDexFileChecksums(oat_file, dex_location, *dex_location_checksum, |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1257 | kRuntimeISA, error_msg); |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1258 | if (!verified) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1259 | return false; |
Vladimir Marko | 64e7ac0 | 2014-05-01 15:43:47 +0100 | [diff] [blame] | 1260 | } |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1261 | dex_file.reset(oat_file->GetOatDexFile(dex_location, |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1262 | dex_location_checksum)->OpenDexFile(error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1263 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1264 | return dex_file.get() != nullptr; |
Brian Carlstrom | 46b8a62 | 2012-06-19 23:13:22 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 1267 | const OatFile* ClassLinker::FindOatFileContainingDexFileFromDexLocation( |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1268 | const char* dex_location, |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 1269 | const uint32_t* dex_location_checksum, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1270 | InstructionSet isa, |
Calin Juravle | c54aea7 | 2014-07-16 14:45:03 +0100 | [diff] [blame] | 1271 | std::vector<std::string>* error_msgs, |
| 1272 | bool* obsolete_file_cleanup_failed) { |
| 1273 | *obsolete_file_cleanup_failed = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1274 | bool already_opened = false; |
| 1275 | std::string dex_location_str(dex_location); |
| 1276 | std::unique_ptr<const OatFile> oat_file(OpenOatFileFromDexLocation(dex_location_str, isa, |
| 1277 | &already_opened, |
| 1278 | obsolete_file_cleanup_failed, |
| 1279 | error_msgs)); |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1280 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1281 | if (oat_file.get() == nullptr) { |
| 1282 | error_msgs->push_back(StringPrintf("Failed to open oat file from dex location '%s'", |
| 1283 | dex_location)); |
| 1284 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1285 | } else if (oat_file->IsExecutable() && |
Calin Juravle | 621962a | 2014-09-02 15:53:55 +0100 | [diff] [blame] | 1286 | !VerifyOatWithDexFile(oat_file.get(), dex_location, |
| 1287 | dex_location_checksum, &error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1288 | error_msgs->push_back(StringPrintf("Failed to verify oat file '%s' found for dex location " |
| 1289 | "'%s': %s", oat_file->GetLocation().c_str(), dex_location, |
Andreas Gampe | 329d188 | 2014-04-08 10:32:19 -0700 | [diff] [blame] | 1290 | error_msg.c_str())); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1291 | return nullptr; |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1292 | } else if (!oat_file->IsExecutable() && |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1293 | Runtime::Current()->GetHeap()->HasImageSpace() && |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1294 | !VerifyOatImageChecksum(oat_file.get(), isa)) { |
| 1295 | error_msgs->push_back(StringPrintf("Failed to verify non-executable oat file '%s' found for " |
| 1296 | "dex location '%s'. Image checksum incorrect.", |
| 1297 | oat_file->GetLocation().c_str(), dex_location)); |
| 1298 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1299 | } else { |
| 1300 | return oat_file.release(); |
Brian Carlstrom | 08cbf66 | 2013-12-10 16:52:57 -0800 | [diff] [blame] | 1301 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1304 | const OatFile* ClassLinker::FindOpenedOatFileFromOatLocation(const std::string& oat_location) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1305 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1306 | for (size_t i = 0; i < oat_files_.size(); i++) { |
| 1307 | const OatFile* oat_file = oat_files_[i]; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1308 | DCHECK(oat_file != nullptr); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 1309 | if (oat_file->GetLocation() == oat_location) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1310 | return oat_file; |
| 1311 | } |
| 1312 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1313 | return nullptr; |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 1314 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1315 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1316 | const OatFile* ClassLinker::OpenOatFileFromDexLocation(const std::string& dex_location, |
| 1317 | InstructionSet isa, |
| 1318 | bool *already_opened, |
| 1319 | bool *obsolete_file_cleanup_failed, |
| 1320 | std::vector<std::string>* error_msgs) { |
| 1321 | // Find out if we've already opened the file |
| 1322 | const OatFile* ret = nullptr; |
| 1323 | std::string odex_filename(DexFilenameToOdexFilename(dex_location, isa)); |
| 1324 | ret = FindOpenedOatFileFromOatLocation(odex_filename); |
| 1325 | if (ret != nullptr) { |
| 1326 | *already_opened = true; |
| 1327 | return ret; |
| 1328 | } |
| 1329 | |
| 1330 | std::string dalvik_cache; |
| 1331 | bool have_android_data = false; |
| 1332 | bool have_dalvik_cache = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1333 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1334 | GetDalvikCache(GetInstructionSetString(kRuntimeISA), false, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1335 | &have_android_data, &have_dalvik_cache, &is_global_cache); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1336 | std::string cache_filename; |
| 1337 | if (have_dalvik_cache) { |
| 1338 | cache_filename = GetDalvikCacheFilenameOrDie(dex_location.c_str(), dalvik_cache.c_str()); |
| 1339 | ret = FindOpenedOatFileFromOatLocation(cache_filename); |
| 1340 | if (ret != nullptr) { |
| 1341 | *already_opened = true; |
| 1342 | return ret; |
| 1343 | } |
| 1344 | } else { |
| 1345 | // If we need to relocate we should just place odex back where it started. |
| 1346 | cache_filename = odex_filename; |
| 1347 | } |
| 1348 | |
| 1349 | ret = nullptr; |
| 1350 | |
| 1351 | // We know that neither the odex nor the cache'd version is already in use, if it even exists. |
| 1352 | // |
| 1353 | // Now we do the following: |
| 1354 | // 1) Try and open the odex version |
| 1355 | // 2) If present, checksum-verified & relocated correctly return it |
| 1356 | // 3) Close the odex version to free up its address space. |
| 1357 | // 4) Try and open the cache version |
| 1358 | // 5) If present, checksum-verified & relocated correctly return it |
| 1359 | // 6) Close the cache version to free up its address space. |
| 1360 | // 7) If we should relocate: |
| 1361 | // a) If we have opened and checksum-verified the odex version relocate it to |
| 1362 | // 'cache_filename' and return it |
| 1363 | // b) If we have opened and checksum-verified the cache version relocate it in place and return |
| 1364 | // it. This should not happen often (I think only the run-test's will hit this case). |
| 1365 | // 8) If the cache-version was present we should delete it since it must be obsolete if we get to |
| 1366 | // this point. |
| 1367 | // 9) Return nullptr |
| 1368 | |
| 1369 | *already_opened = false; |
| 1370 | const Runtime* runtime = Runtime::Current(); |
| 1371 | CHECK(runtime != nullptr); |
| 1372 | bool executable = !runtime->IsCompiler(); |
| 1373 | |
| 1374 | std::string odex_error_msg; |
| 1375 | bool should_patch_system = false; |
| 1376 | bool odex_checksum_verified = false; |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1377 | bool have_system_odex = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1378 | { |
| 1379 | // There is a high probability that these both these oat files map similar/the same address |
| 1380 | // 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] | 1381 | std::unique_ptr<OatFile> odex_oat_file(OatFile::Open(odex_filename, odex_filename, nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1382 | executable, &odex_error_msg)); |
| 1383 | if (odex_oat_file.get() != nullptr && CheckOatFile(odex_oat_file.get(), isa, |
| 1384 | &odex_checksum_verified, |
| 1385 | &odex_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1386 | return odex_oat_file.release(); |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1387 | } else { |
| 1388 | if (odex_checksum_verified) { |
| 1389 | // We can just relocate |
| 1390 | should_patch_system = true; |
| 1391 | odex_error_msg = "Image Patches are incorrect"; |
| 1392 | } |
| 1393 | if (odex_oat_file.get() != nullptr) { |
| 1394 | have_system_odex = true; |
| 1395 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1399 | std::string cache_error_msg; |
| 1400 | bool should_patch_cache = false; |
| 1401 | bool cache_checksum_verified = false; |
| 1402 | if (have_dalvik_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1403 | std::unique_ptr<OatFile> cache_oat_file(OatFile::Open(cache_filename, cache_filename, nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1404 | executable, &cache_error_msg)); |
| 1405 | if (cache_oat_file.get() != nullptr && CheckOatFile(cache_oat_file.get(), isa, |
| 1406 | &cache_checksum_verified, |
| 1407 | &cache_error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1408 | return cache_oat_file.release(); |
| 1409 | } else if (cache_checksum_verified) { |
| 1410 | // We can just relocate |
| 1411 | should_patch_cache = true; |
| 1412 | cache_error_msg = "Image Patches are incorrect"; |
| 1413 | } |
| 1414 | } else if (have_android_data) { |
| 1415 | // dalvik_cache does not exist but android data does. This means we should be able to create |
| 1416 | // it, so we should try. |
| 1417 | GetDalvikCacheOrDie(GetInstructionSetString(kRuntimeISA), true); |
| 1418 | } |
| 1419 | |
| 1420 | ret = nullptr; |
| 1421 | std::string error_msg; |
| 1422 | if (runtime->CanRelocate()) { |
| 1423 | // Run relocation |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1424 | gc::space::ImageSpace* space = Runtime::Current()->GetHeap()->GetImageSpace(); |
| 1425 | if (space != nullptr) { |
| 1426 | const std::string& image_location = space->GetImageLocation(); |
| 1427 | if (odex_checksum_verified && should_patch_system) { |
| 1428 | ret = PatchAndRetrieveOat(odex_filename, cache_filename, image_location, isa, &error_msg); |
| 1429 | } else if (cache_checksum_verified && should_patch_cache) { |
| 1430 | CHECK(have_dalvik_cache); |
| 1431 | ret = PatchAndRetrieveOat(cache_filename, cache_filename, image_location, isa, &error_msg); |
| 1432 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1433 | } else if (have_system_odex) { |
| 1434 | ret = GetInterpretedOnlyOat(odex_filename, isa, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | if (ret == nullptr && have_dalvik_cache && OS::FileExists(cache_filename.c_str())) { |
| 1438 | // implicitly: were able to fine where the cached version is but we were unable to use it, |
| 1439 | // either as a destination for relocation or to open a file. We should delete it if it is |
| 1440 | // there. |
| 1441 | if (TEMP_FAILURE_RETRY(unlink(cache_filename.c_str())) != 0) { |
| 1442 | std::string rm_error_msg = StringPrintf("Failed to remove obsolete file from %s when " |
| 1443 | "searching for dex file %s: %s", |
| 1444 | cache_filename.c_str(), dex_location.c_str(), |
| 1445 | strerror(errno)); |
| 1446 | error_msgs->push_back(rm_error_msg); |
| 1447 | VLOG(class_linker) << rm_error_msg; |
| 1448 | // Let the caller know that we couldn't remove the obsolete file. |
| 1449 | // This is a good indication that further writes may fail as well. |
| 1450 | *obsolete_file_cleanup_failed = true; |
| 1451 | } |
| 1452 | } |
| 1453 | if (ret == nullptr) { |
| 1454 | VLOG(class_linker) << error_msg; |
| 1455 | error_msgs->push_back(error_msg); |
| 1456 | std::string relocation_msg; |
| 1457 | if (runtime->CanRelocate()) { |
| 1458 | relocation_msg = StringPrintf(" and relocation failed"); |
| 1459 | } |
| 1460 | if (have_dalvik_cache && cache_checksum_verified) { |
| 1461 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) or %s " |
| 1462 | "(error %s)%s.", odex_filename.c_str(), odex_error_msg.c_str(), |
| 1463 | cache_filename.c_str(), cache_error_msg.c_str(), |
| 1464 | relocation_msg.c_str()); |
| 1465 | } else { |
| 1466 | error_msg = StringPrintf("Failed to open oat file from %s (error %s) (no " |
| 1467 | "dalvik_cache availible)%s.", odex_filename.c_str(), |
| 1468 | odex_error_msg.c_str(), relocation_msg.c_str()); |
| 1469 | } |
| 1470 | VLOG(class_linker) << error_msg; |
| 1471 | error_msgs->push_back(error_msg); |
| 1472 | } |
| 1473 | return ret; |
| 1474 | } |
| 1475 | |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1476 | const OatFile* ClassLinker::GetInterpretedOnlyOat(const std::string& oat_path, |
| 1477 | InstructionSet isa, |
| 1478 | std::string* error_msg) { |
| 1479 | // We open it non-executable |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1480 | std::unique_ptr<OatFile> output(OatFile::Open(oat_path, oat_path, nullptr, false, error_msg)); |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1481 | if (output.get() == nullptr) { |
| 1482 | return nullptr; |
| 1483 | } |
Alex Light | 84d7605 | 2014-08-22 17:49:35 -0700 | [diff] [blame] | 1484 | if (!Runtime::Current()->GetHeap()->HasImageSpace() || |
| 1485 | VerifyOatImageChecksum(output.get(), isa)) { |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1486 | return output.release(); |
| 1487 | } else { |
| 1488 | *error_msg = StringPrintf("Could not use oat file '%s', image checksum failed to verify.", |
| 1489 | oat_path.c_str()); |
| 1490 | return nullptr; |
| 1491 | } |
| 1492 | } |
| 1493 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1494 | const OatFile* ClassLinker::PatchAndRetrieveOat(const std::string& input_oat, |
| 1495 | const std::string& output_oat, |
| 1496 | const std::string& image_location, |
| 1497 | InstructionSet isa, |
| 1498 | std::string* error_msg) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1499 | if (!Runtime::Current()->GetHeap()->HasImageSpace()) { |
| 1500 | // We don't have an image space so there is no point in trying to patchoat. |
| 1501 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted because we are " |
| 1502 | << "running without an image. Attempting to use oat file for interpretation."; |
| 1503 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1504 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1505 | if (!Runtime::Current()->IsDex2OatEnabled()) { |
| 1506 | // We don't have dex2oat so we can assume we don't have patchoat either. We should just use the |
| 1507 | // input_oat but make sure we only do interpretation on it's dex files. |
| 1508 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' not attempted due to dex2oat being " |
| 1509 | << "disabled. Attempting to use oat file for interpretation"; |
| 1510 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
| 1511 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1512 | Locks::mutator_lock_->AssertNotHeld(Thread::Current()); // Avoid starving GC. |
| 1513 | std::string patchoat(Runtime::Current()->GetPatchoatExecutable()); |
| 1514 | |
| 1515 | std::string isa_arg("--instruction-set="); |
| 1516 | isa_arg += GetInstructionSetString(isa); |
| 1517 | std::string input_oat_filename_arg("--input-oat-file="); |
| 1518 | input_oat_filename_arg += input_oat; |
| 1519 | std::string output_oat_filename_arg("--output-oat-file="); |
| 1520 | output_oat_filename_arg += output_oat; |
| 1521 | std::string patched_image_arg("--patched-image-location="); |
| 1522 | patched_image_arg += image_location; |
| 1523 | |
| 1524 | std::vector<std::string> argv; |
| 1525 | argv.push_back(patchoat); |
| 1526 | argv.push_back(isa_arg); |
| 1527 | argv.push_back(input_oat_filename_arg); |
| 1528 | argv.push_back(output_oat_filename_arg); |
| 1529 | argv.push_back(patched_image_arg); |
| 1530 | |
| 1531 | std::string command_line(Join(argv, ' ')); |
| 1532 | LOG(INFO) << "Relocate Oat File: " << command_line; |
| 1533 | bool success = Exec(argv, error_msg); |
| 1534 | if (success) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1535 | std::unique_ptr<OatFile> output(OatFile::Open(output_oat, output_oat, nullptr, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1536 | !Runtime::Current()->IsCompiler(), error_msg)); |
| 1537 | bool checksum_verified = false; |
| 1538 | if (output.get() != nullptr && CheckOatFile(output.get(), isa, &checksum_verified, error_msg)) { |
| 1539 | return output.release(); |
| 1540 | } else if (output.get() != nullptr) { |
| 1541 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1542 | "but output file '%s' failed verifcation: %s", |
| 1543 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1544 | } else { |
| 1545 | *error_msg = StringPrintf("Patching of oat file '%s' succeeded " |
| 1546 | "but was unable to open output file '%s': %s", |
| 1547 | input_oat.c_str(), output_oat.c_str(), error_msg->c_str()); |
| 1548 | } |
Alex Light | 9dcc457 | 2014-08-14 14:16:26 -0700 | [diff] [blame] | 1549 | } else if (!Runtime::Current()->IsCompiler()) { |
| 1550 | // patchoat failed which means we probably don't have enough room to place the output oat file, |
| 1551 | // instead of failing we should just run the interpreter from the dex files in the input oat. |
| 1552 | LOG(WARNING) << "Patching of oat file '" << input_oat << "' failed. Attempting to use oat file " |
| 1553 | << "for interpretation. patchoat failure was: " << *error_msg; |
| 1554 | return GetInterpretedOnlyOat(input_oat, isa, error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1555 | } else { |
| 1556 | *error_msg = StringPrintf("Patching of oat file '%s to '%s' " |
| 1557 | "failed: %s", input_oat.c_str(), output_oat.c_str(), |
| 1558 | error_msg->c_str()); |
| 1559 | } |
| 1560 | return nullptr; |
| 1561 | } |
| 1562 | |
| 1563 | int32_t ClassLinker::GetRequiredDelta(const OatFile* oat_file, InstructionSet isa) { |
| 1564 | Runtime* runtime = Runtime::Current(); |
| 1565 | int32_t real_patch_delta; |
| 1566 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1567 | CHECK(image_space != nullptr); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1568 | if (isa == Runtime::Current()->GetInstructionSet()) { |
| 1569 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1570 | real_patch_delta = image_header.GetPatchDelta(); |
| 1571 | } else { |
| 1572 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1573 | image_space->GetImageLocation().c_str(), isa)); |
| 1574 | real_patch_delta = image_header->GetPatchDelta(); |
| 1575 | } |
| 1576 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
| 1577 | return real_patch_delta - oat_header.GetImagePatchDelta(); |
| 1578 | } |
| 1579 | |
| 1580 | bool ClassLinker::CheckOatFile(const OatFile* oat_file, InstructionSet isa, |
| 1581 | bool* checksum_verified, |
| 1582 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1583 | Runtime* runtime = Runtime::Current(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1584 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetImageSpace(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 1585 | if (image_space == nullptr) { |
| 1586 | *error_msg = "No image space present"; |
| 1587 | return false; |
| 1588 | } |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1589 | uint32_t real_image_checksum; |
| 1590 | void* real_image_oat_offset; |
| 1591 | int32_t real_patch_delta; |
| 1592 | if (isa == runtime->GetInstructionSet()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1593 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 1594 | real_image_checksum = image_header.GetOatChecksum(); |
| 1595 | real_image_oat_offset = image_header.GetOatDataBegin(); |
| 1596 | real_patch_delta = image_header.GetPatchDelta(); |
| 1597 | } else { |
| 1598 | std::unique_ptr<ImageHeader> image_header(gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 1599 | image_space->GetImageLocation().c_str(), isa)); |
| 1600 | real_image_checksum = image_header->GetOatChecksum(); |
| 1601 | real_image_oat_offset = image_header->GetOatDataBegin(); |
| 1602 | real_patch_delta = image_header->GetPatchDelta(); |
| 1603 | } |
| 1604 | |
| 1605 | const OatHeader& oat_header = oat_file->GetOatHeader(); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1606 | std::string compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1607 | |
| 1608 | uint32_t oat_image_checksum = oat_header.GetImageFileLocationOatChecksum(); |
| 1609 | *checksum_verified = oat_image_checksum == real_image_checksum; |
| 1610 | if (!*checksum_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1611 | StringAppendF(&compound_msg, " Oat Image Checksum Incorrect (expected 0x%x, received 0x%x)", |
| 1612 | real_image_checksum, oat_image_checksum); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | void* oat_image_oat_offset = |
| 1616 | reinterpret_cast<void*>(oat_header.GetImageFileLocationOatDataBegin()); |
| 1617 | bool offset_verified = oat_image_oat_offset == real_image_oat_offset; |
| 1618 | if (!offset_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1619 | StringAppendF(&compound_msg, " Oat Image oat offset incorrect (expected 0x%p, received 0x%p)", |
| 1620 | real_image_oat_offset, oat_image_oat_offset); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 1624 | bool patch_delta_verified = oat_patch_delta == real_patch_delta; |
| 1625 | if (!patch_delta_verified) { |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1626 | StringAppendF(&compound_msg, " Oat image patch delta incorrect (expected 0x%x, received 0x%x)", |
| 1627 | real_patch_delta, oat_patch_delta); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | bool ret = (*checksum_verified && offset_verified && patch_delta_verified); |
Vladimir Marko | 30c2e1b | 2014-09-08 09:55:29 +0100 | [diff] [blame] | 1631 | if (!ret) { |
| 1632 | *error_msg = "Oat file failed to verify:" + compound_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1633 | } |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1637 | const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location, |
| 1638 | std::string* error_msg) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1639 | const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1640 | if (oat_file != nullptr) { |
jeffhao | f6174e8 | 2012-01-31 16:14:17 -0800 | [diff] [blame] | 1641 | return oat_file; |
| 1642 | } |
| 1643 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1644 | return OatFile::Open(oat_location, oat_location, nullptr, !Runtime::Current()->IsCompiler(), |
| 1645 | error_msg); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1648 | static void InitFromImageInterpretOnlyCallback(mirror::Object* obj, void* arg) |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1649 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1650 | ClassLinker* class_linker = reinterpret_cast<ClassLinker*>(arg); |
| 1651 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1652 | DCHECK(obj != nullptr); |
| 1653 | DCHECK(class_linker != nullptr); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1654 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1655 | if (obj->IsArtMethod()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1656 | mirror::ArtMethod* method = obj->AsArtMethod(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1657 | if (!method->IsNative()) { |
| 1658 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
| 1659 | if (method != Runtime::Current()->GetResolutionMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1660 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 1661 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1667 | void ClassLinker::InitFromImage() { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1668 | VLOG(startup) << "ClassLinker::InitFromImage entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1669 | CHECK(!init_done_); |
| 1670 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1671 | Thread* self = Thread::Current(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1672 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 1673 | gc::space::ImageSpace* space = heap->GetImageSpace(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1674 | dex_cache_image_class_lookup_required_ = true; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1675 | CHECK(space != nullptr); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1676 | OatFile& oat_file = GetImageOatFile(space); |
| 1677 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatChecksum(), 0U); |
| 1678 | CHECK_EQ(oat_file.GetOatHeader().GetImageFileLocationOatDataBegin(), 0U); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1679 | const char* image_file_location = oat_file.GetOatHeader(). |
| 1680 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 1681 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1682 | portable_resolution_trampoline_ = oat_file.GetOatHeader().GetPortableResolutionTrampoline(); |
| 1683 | quick_resolution_trampoline_ = oat_file.GetOatHeader().GetQuickResolutionTrampoline(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 1684 | portable_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetPortableImtConflictTrampoline(); |
| 1685 | quick_imt_conflict_trampoline_ = oat_file.GetOatHeader().GetQuickImtConflictTrampoline(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 1686 | quick_generic_jni_trampoline_ = oat_file.GetOatHeader().GetQuickGenericJniTrampoline(); |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 1687 | quick_to_interpreter_bridge_trampoline_ = oat_file.GetOatHeader().GetQuickToInterpreterBridge(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1688 | mirror::Object* dex_caches_object = space->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 1689 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1690 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1691 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1692 | StackHandleScope<1> hs(self); |
| 1693 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1694 | space->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)-> |
| 1695 | AsObjectArray<mirror::Class>())); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1696 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1697 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1698 | // Special case of setting up the String class early so that we can test arbitrary objects |
| 1699 | // as being Strings or not |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1700 | mirror::String::SetClass(GetClassRoot(kJavaLangString)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1701 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1702 | CHECK_EQ(oat_file.GetOatHeader().GetDexFileCount(), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1703 | static_cast<uint32_t>(dex_caches->GetLength())); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1704 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1705 | StackHandleScope<1> hs(self); |
| 1706 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(dex_caches->Get(i))); |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1707 | const std::string& dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1708 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_file_location.c_str(), |
| 1709 | nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1710 | CHECK(oat_dex_file != nullptr) << oat_file.GetLocation() << " " << dex_file_location; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1711 | std::string error_msg; |
| 1712 | const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1713 | if (dex_file == nullptr) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1714 | LOG(FATAL) << "Failed to open dex file " << dex_file_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1715 | << " from within oat file " << oat_file.GetLocation() |
| 1716 | << " error '" << error_msg << "'"; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1717 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 1718 | |
| 1719 | CHECK_EQ(dex_file->GetLocationChecksum(), oat_dex_file->GetDexFileLocationChecksum()); |
| 1720 | |
| 1721 | AppendToBootClassPath(*dex_file, dex_cache); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1724 | // Set classes on AbstractMethod early so that IsMethod tests can be performed during the live |
| 1725 | // bitmap walk. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1726 | mirror::ArtMethod::SetClass(GetClassRoot(kJavaLangReflectArtMethod)); |
Anwar Ghuloum | c4f105d | 2013-04-10 16:12:11 -0700 | [diff] [blame] | 1727 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1728 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1729 | if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1730 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1731 | heap->VisitObjects(InitFromImageInterpretOnlyCallback, this); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1732 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1733 | |
| 1734 | // reinit class_roots_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1735 | mirror::Class::SetClassClass(class_roots->Get(kJavaLangClass)); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1736 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>(class_roots.Get()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1737 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1738 | // reinit array_iftable_ from any array class instance, they should be == |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1739 | array_iftable_ = GcRoot<mirror::IfTable>(GetClassRoot(kObjectArrayClass)->GetIfTable()); |
| 1740 | DCHECK(array_iftable_.Read() == GetClassRoot(kBooleanArrayClass)->GetIfTable()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1741 | // String class root was set above |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1742 | mirror::Reference::SetClass(GetClassRoot(kJavaLangRefReference)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1743 | mirror::ArtField::SetClass(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1744 | mirror::BooleanArray::SetArrayClass(GetClassRoot(kBooleanArrayClass)); |
| 1745 | mirror::ByteArray::SetArrayClass(GetClassRoot(kByteArrayClass)); |
| 1746 | mirror::CharArray::SetArrayClass(GetClassRoot(kCharArrayClass)); |
| 1747 | mirror::DoubleArray::SetArrayClass(GetClassRoot(kDoubleArrayClass)); |
| 1748 | mirror::FloatArray::SetArrayClass(GetClassRoot(kFloatArrayClass)); |
| 1749 | mirror::IntArray::SetArrayClass(GetClassRoot(kIntArrayClass)); |
| 1750 | mirror::LongArray::SetArrayClass(GetClassRoot(kLongArrayClass)); |
| 1751 | mirror::ShortArray::SetArrayClass(GetClassRoot(kShortArrayClass)); |
| 1752 | mirror::Throwable::SetClass(GetClassRoot(kJavaLangThrowable)); |
| 1753 | mirror::StackTraceElement::SetClass(GetClassRoot(kJavaLangStackTraceElement)); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1754 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1755 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1756 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1757 | VLOG(startup) << "ClassLinker::InitFromImage exiting"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1760 | void ClassLinker::VisitClassRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
| 1761 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1762 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1763 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 1764 | it.second.VisitRoot(callback, arg, 0, kRootStickyClass); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1765 | } |
| 1766 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1767 | for (auto& pair : new_class_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1768 | mirror::Class* old_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 1769 | pair.second.VisitRoot(callback, arg, 0, kRootStickyClass); |
| 1770 | mirror::Class* new_ref = pair.second.Read<kWithoutReadBarrier>(); |
| 1771 | if (UNLIKELY(new_ref != old_ref)) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1772 | // Uh ohes, GC moved a root in the log. Need to search the class_table and update the |
| 1773 | // corresponding object. This is slow, but luckily for us, this may only happen with a |
| 1774 | // concurrent moving GC. |
| 1775 | for (auto it = class_table_.lower_bound(pair.first), end = class_table_.end(); |
| 1776 | it != end && it->first == pair.first; ++it) { |
| 1777 | // If the class stored matches the old class, update it to the new value. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1778 | if (old_ref == it->second.Read<kWithoutReadBarrier>()) { |
| 1779 | it->second = GcRoot<mirror::Class>(new_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1786 | new_class_roots_.clear(); |
| 1787 | } |
| 1788 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1789 | log_new_class_table_roots_ = true; |
| 1790 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1791 | log_new_class_table_roots_ = false; |
| 1792 | } |
| 1793 | // We deliberately ignore the class roots in the image since we |
| 1794 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1795 | } |
| 1796 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1797 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1798 | // reinit references to when reinitializing a ClassLinker from a |
| 1799 | // mapped image. |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1800 | void ClassLinker::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1801 | class_roots_.VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1802 | Thread* self = Thread::Current(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1803 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 1804 | ReaderMutexLock mu(self, dex_lock_); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1805 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1806 | for (GcRoot<mirror::DexCache>& dex_cache : dex_caches_) { |
| 1807 | dex_cache.VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1808 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1809 | } else if ((flags & kVisitRootFlagNewRoots) != 0) { |
| 1810 | for (size_t index : new_dex_cache_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1811 | dex_caches_[index].VisitRoot(callback, arg, 0, kRootVMInternal); |
Mathieu Chartier | c462198 | 2013-09-16 19:43:47 -0700 | [diff] [blame] | 1812 | } |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 1813 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 1814 | if ((flags & kVisitRootFlagClearRootLog) != 0) { |
| 1815 | new_dex_cache_roots_.clear(); |
| 1816 | } |
| 1817 | if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
| 1818 | log_new_dex_caches_roots_ = true; |
| 1819 | } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
| 1820 | log_new_dex_caches_roots_ = false; |
| 1821 | } |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1822 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1823 | VisitClassRoots(callback, arg, flags); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1824 | array_iftable_.VisitRoot(callback, arg, 0, kRootVMInternal); |
| 1825 | DCHECK(!array_iftable_.IsNull()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1826 | for (size_t i = 0; i < kFindArrayCacheSize; ++i) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1827 | if (!find_array_class_cache_[i].IsNull()) { |
| 1828 | find_array_class_cache_[i].VisitRoot(callback, arg, 0, kRootVMInternal); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1829 | } |
| 1830 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1833 | void ClassLinker::VisitClasses(ClassVisitor* visitor, void* arg) { |
| 1834 | if (dex_cache_image_class_lookup_required_) { |
| 1835 | MoveImageClassesToClassTable(); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1836 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1837 | // TODO: why isn't this a ReaderMutexLock? |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 1838 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 1839 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 1840 | mirror::Class* c = it.second.Read(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 1841 | if (!visitor(c, arg)) { |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 1842 | return; |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1847 | static bool GetClassesVisitorSet(mirror::Class* c, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1848 | std::set<mirror::Class*>* classes = reinterpret_cast<std::set<mirror::Class*>*>(arg); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1849 | classes->insert(c); |
| 1850 | return true; |
| 1851 | } |
| 1852 | |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1853 | struct GetClassesVisitorArrayArg { |
| 1854 | Handle<mirror::ObjectArray<mirror::Class>>* classes; |
| 1855 | int32_t index; |
| 1856 | bool success; |
| 1857 | }; |
| 1858 | |
| 1859 | static bool GetClassesVisitorArray(mirror::Class* c, void* varg) |
| 1860 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 1861 | GetClassesVisitorArrayArg* arg = reinterpret_cast<GetClassesVisitorArrayArg*>(varg); |
| 1862 | if (arg->index < (*arg->classes)->GetLength()) { |
| 1863 | (*arg->classes)->Set(arg->index, c); |
| 1864 | arg->index++; |
| 1865 | return true; |
| 1866 | } else { |
| 1867 | arg->success = false; |
| 1868 | return false; |
| 1869 | } |
| 1870 | } |
| 1871 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 1872 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor, void* arg) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1873 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 1874 | // is avoiding duplicates. |
| 1875 | if (!kMovingClasses) { |
| 1876 | std::set<mirror::Class*> classes; |
| 1877 | VisitClasses(GetClassesVisitorSet, &classes); |
| 1878 | for (mirror::Class* klass : classes) { |
| 1879 | if (!visitor(klass, arg)) { |
| 1880 | return; |
| 1881 | } |
| 1882 | } |
| 1883 | } else { |
| 1884 | Thread* self = Thread::Current(); |
| 1885 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 1886 | MutableHandle<mirror::ObjectArray<mirror::Class>> classes = |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1887 | hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
| 1888 | GetClassesVisitorArrayArg local_arg; |
| 1889 | local_arg.classes = &classes; |
| 1890 | local_arg.success = false; |
| 1891 | // We size the array assuming classes won't be added to the class table during the visit. |
| 1892 | // If this assumption fails we iterate again. |
| 1893 | while (!local_arg.success) { |
| 1894 | size_t class_table_size; |
| 1895 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1896 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 1897 | class_table_size = class_table_.size(); |
| 1898 | } |
| 1899 | mirror::Class* class_type = mirror::Class::GetJavaLangClass(); |
| 1900 | mirror::Class* array_of_class = FindArrayClass(self, &class_type); |
| 1901 | classes.Assign( |
| 1902 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
| 1903 | CHECK(classes.Get() != nullptr); // OOME. |
| 1904 | local_arg.index = 0; |
| 1905 | local_arg.success = true; |
| 1906 | VisitClasses(GetClassesVisitorArray, &local_arg); |
| 1907 | } |
| 1908 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 1909 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 1910 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 1911 | // finished then we don't visit. |
| 1912 | mirror::Class* klass = classes->Get(i); |
| 1913 | if (klass != nullptr && !visitor(klass, arg)) { |
| 1914 | return; |
| 1915 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1920 | ClassLinker::~ClassLinker() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1921 | mirror::Class::ResetClass(); |
| 1922 | mirror::String::ResetClass(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 1923 | mirror::Reference::ResetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1924 | mirror::ArtField::ResetClass(); |
| 1925 | mirror::ArtMethod::ResetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1926 | mirror::BooleanArray::ResetArrayClass(); |
| 1927 | mirror::ByteArray::ResetArrayClass(); |
| 1928 | mirror::CharArray::ResetArrayClass(); |
| 1929 | mirror::DoubleArray::ResetArrayClass(); |
| 1930 | mirror::FloatArray::ResetArrayClass(); |
| 1931 | mirror::IntArray::ResetArrayClass(); |
| 1932 | mirror::LongArray::ResetArrayClass(); |
| 1933 | mirror::ShortArray::ResetArrayClass(); |
| 1934 | mirror::Throwable::ResetClass(); |
| 1935 | mirror::StackTraceElement::ResetClass(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1936 | STLDeleteElements(&boot_class_path_); |
| 1937 | STLDeleteElements(&oat_files_); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1940 | mirror::DexCache* ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1941 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1942 | StackHandleScope<16> hs(self); |
| 1943 | Handle<mirror::Class> dex_cache_class(hs.NewHandle(GetClassRoot(kJavaLangDexCache))); |
| 1944 | Handle<mirror::DexCache> dex_cache( |
| 1945 | hs.NewHandle(down_cast<mirror::DexCache*>( |
| 1946 | heap->AllocObject<true>(self, dex_cache_class.Get(), dex_cache_class->GetObjectSize(), |
| 1947 | VoidFunctor())))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1948 | if (dex_cache.Get() == nullptr) { |
| 1949 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1950 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1951 | Handle<mirror::String> |
| 1952 | location(hs.NewHandle(intern_table_->InternStrong(dex_file.GetLocation().c_str()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1953 | if (location.Get() == nullptr) { |
| 1954 | return nullptr; |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 1955 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1956 | Handle<mirror::ObjectArray<mirror::String>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1957 | strings(hs.NewHandle(AllocStringArray(self, dex_file.NumStringIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1958 | if (strings.Get() == nullptr) { |
| 1959 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1960 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1961 | Handle<mirror::ObjectArray<mirror::Class>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1962 | types(hs.NewHandle(AllocClassArray(self, dex_file.NumTypeIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1963 | if (types.Get() == nullptr) { |
| 1964 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1965 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1966 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1967 | methods(hs.NewHandle(AllocArtMethodArray(self, dex_file.NumMethodIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1968 | if (methods.Get() == nullptr) { |
| 1969 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1970 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1971 | Handle<mirror::ObjectArray<mirror::ArtField>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1972 | fields(hs.NewHandle(AllocArtFieldArray(self, dex_file.NumFieldIds()))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1973 | if (fields.Get() == nullptr) { |
| 1974 | return nullptr; |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1975 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1976 | dex_cache->Init(&dex_file, location.Get(), strings.Get(), types.Get(), methods.Get(), |
| 1977 | fields.Get()); |
| 1978 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1981 | mirror::Class* ClassLinker::AllocClass(Thread* self, mirror::Class* java_lang_Class, |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1982 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1983 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1984 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1985 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1986 | mirror::Object* k = kMovingClasses ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1987 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 1988 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1989 | if (UNLIKELY(k == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1990 | CHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1991 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 1992 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1993 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 1996 | mirror::Class* ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 1997 | return AllocClass(self, GetClassRoot(kJavaLangClass), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 1998 | } |
| 1999 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2000 | mirror::ArtField* ClassLinker::AllocArtField(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2001 | return down_cast<mirror::ArtField*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2002 | GetClassRoot(kJavaLangReflectArtField)->AllocNonMovableObject(self)); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2005 | mirror::ArtMethod* ClassLinker::AllocArtMethod(Thread* self) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2006 | return down_cast<mirror::ArtMethod*>( |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 2007 | GetClassRoot(kJavaLangReflectArtMethod)->AllocNonMovableObject(self)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2010 | mirror::ObjectArray<mirror::StackTraceElement>* ClassLinker::AllocStackTraceElementArray( |
| 2011 | Thread* self, size_t length) { |
| 2012 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
| 2013 | self, GetClassRoot(kJavaLangStackTraceElementArrayClass), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2016 | mirror::Class* ClassLinker::EnsureResolved(Thread* self, const char* descriptor, |
| 2017 | mirror::Class* klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2018 | DCHECK(klass != nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2019 | |
| 2020 | // For temporary classes we must wait for them to be retired. |
| 2021 | if (init_done_ && klass->IsTemp()) { |
| 2022 | CHECK(!klass->IsResolved()); |
| 2023 | if (klass->IsErroneous()) { |
| 2024 | ThrowEarlierClassFailure(klass); |
| 2025 | return nullptr; |
| 2026 | } |
| 2027 | StackHandleScope<1> hs(self); |
| 2028 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2029 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2030 | // Loop and wait for the resolving thread to retire this class. |
| 2031 | while (!h_class->IsRetired() && !h_class->IsErroneous()) { |
| 2032 | lock.WaitIgnoringInterrupts(); |
| 2033 | } |
| 2034 | if (h_class->IsErroneous()) { |
| 2035 | ThrowEarlierClassFailure(h_class.Get()); |
| 2036 | return nullptr; |
| 2037 | } |
| 2038 | CHECK(h_class->IsRetired()); |
| 2039 | // Get the updated class from class table. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2040 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2043 | // Wait for the class if it has not already been linked. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 2044 | if (!klass->IsResolved() && !klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2045 | StackHandleScope<1> hs(self); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2046 | HandleWrapper<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
| 2047 | ObjectLock<mirror::Class> lock(self, h_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2048 | // Check for circular dependencies between classes. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2049 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2050 | ThrowClassCircularityError(h_class.Get()); |
| 2051 | h_class->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2052 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2053 | } |
| 2054 | // Wait for the pending initialization to complete. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2055 | while (!h_class->IsResolved() && !h_class->IsErroneous()) { |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 2056 | lock.WaitIgnoringInterrupts(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2057 | } |
| 2058 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2059 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2060 | if (klass->IsErroneous()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2061 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2062 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2063 | } |
| 2064 | // Return the loaded class. No exceptions should be pending. |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2065 | CHECK(klass->IsResolved()) << PrettyClass(klass); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2066 | self->AssertNoPendingException(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2067 | return klass; |
| 2068 | } |
| 2069 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2070 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
| 2071 | |
| 2072 | // Search a collection of DexFiles for a descriptor |
| 2073 | ClassPathEntry FindInClassPath(const char* descriptor, |
| 2074 | const std::vector<const DexFile*>& class_path) { |
| 2075 | for (size_t i = 0; i != class_path.size(); ++i) { |
| 2076 | const DexFile* dex_file = class_path[i]; |
| 2077 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2078 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2079 | return ClassPathEntry(dex_file, dex_class_def); |
| 2080 | } |
| 2081 | } |
| 2082 | // TODO: remove reinterpret_cast when issue with -std=gnu++0x host issue resolved |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2083 | return ClassPathEntry(static_cast<const DexFile*>(nullptr), |
| 2084 | static_cast<const DexFile::ClassDef*>(nullptr)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2087 | mirror::Class* ClassLinker::FindClassInPathClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2088 | Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2089 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2090 | if (class_loader->GetClass() != |
| 2091 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader) || |
| 2092 | class_loader->GetParent()->GetClass() != |
| 2093 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)) { |
| 2094 | return nullptr; |
| 2095 | } |
| 2096 | ClassPathEntry pair = FindInClassPath(descriptor, boot_class_path_); |
| 2097 | // Check if this would be found in the parent boot class loader. |
| 2098 | if (pair.second != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2099 | mirror::Class* klass = LookupClass(self, descriptor, nullptr); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2100 | if (klass != nullptr) { |
| 2101 | return EnsureResolved(self, descriptor, klass); |
| 2102 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2103 | klass = DefineClass(self, descriptor, NullHandle<mirror::ClassLoader>(), *pair.first, |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2104 | *pair.second); |
| 2105 | if (klass != nullptr) { |
| 2106 | return klass; |
| 2107 | } |
| 2108 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2109 | self->ClearException(); |
| 2110 | } else { |
| 2111 | // RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2112 | StackHandleScope<3> hs(self); |
| 2113 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 2114 | // We need to get the DexPathList and loop through it. |
| 2115 | Handle<mirror::ArtField> cookie_field = |
| 2116 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 2117 | Handle<mirror::ArtField> dex_file_field = |
| 2118 | hs.NewHandle( |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 2119 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2120 | mirror::Object* dex_path_list = |
| 2121 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 2122 | GetObject(class_loader.Get()); |
| 2123 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 2124 | cookie_field.Get() != nullptr) { |
| 2125 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 2126 | mirror::Object* dex_elements_obj = |
| 2127 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 2128 | GetObject(dex_path_list); |
| 2129 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 2130 | // at the mCookie which is a DexFile vector. |
| 2131 | if (dex_elements_obj != nullptr) { |
| 2132 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 2133 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 2134 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 2135 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 2136 | if (element == nullptr) { |
| 2137 | // Should never happen, fall back to java code to throw a NPE. |
| 2138 | break; |
| 2139 | } |
| 2140 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 2141 | if (dex_file != nullptr) { |
| 2142 | const uint64_t cookie = cookie_field->GetLong(dex_file); |
| 2143 | auto* dex_files = |
| 2144 | reinterpret_cast<std::vector<const DexFile*>*>(static_cast<uintptr_t>(cookie)); |
| 2145 | if (dex_files == nullptr) { |
| 2146 | // This should never happen so log a warning. |
| 2147 | LOG(WARNING) << "Null DexFile::mCookie for " << descriptor; |
| 2148 | break; |
| 2149 | } |
| 2150 | for (const DexFile* dex_file : *dex_files) { |
| 2151 | const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor); |
| 2152 | if (dex_class_def != nullptr) { |
| 2153 | RegisterDexFile(*dex_file); |
| 2154 | mirror::Class* klass = |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2155 | DefineClass(self, descriptor, class_loader, *dex_file, *dex_class_def); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2156 | if (klass == nullptr) { |
| 2157 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2158 | self->ClearException(); |
| 2159 | return nullptr; |
| 2160 | } |
| 2161 | return klass; |
| 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | } |
| 2169 | return nullptr; |
| 2170 | } |
| 2171 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2172 | mirror::Class* ClassLinker::FindClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2173 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2174 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2175 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2176 | self->AssertNoPendingException(); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2177 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2178 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2179 | // for primitive classes that aren't backed by dex files. |
| 2180 | return FindPrimitiveClass(descriptor[0]); |
| 2181 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2182 | // Find the class in the loaded classes table. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2183 | mirror::Class* klass = LookupClass(self, descriptor, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2184 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2185 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2186 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2187 | // Class is not yet loaded. |
| 2188 | if (descriptor[0] == '[') { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2189 | return CreateArrayClass(self, descriptor, class_loader); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2190 | } else if (class_loader.Get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2191 | // The boot class loader, search the boot class path. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2192 | ClassPathEntry pair = FindInClassPath(descriptor, boot_class_path_); |
| 2193 | if (pair.second != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2194 | return DefineClass(self, descriptor, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 2195 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2196 | } else { |
| 2197 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2198 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2199 | // trigger the chaining with a proper stack trace. |
| 2200 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2201 | self->SetException(ThrowLocation(), pre_allocated); |
| 2202 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2203 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 2204 | } else if (Runtime::Current()->UseCompileTimeClassPath()) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2205 | // First try with the bootstrap class loader. |
| 2206 | if (class_loader.Get() != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2207 | klass = LookupClass(self, descriptor, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2208 | if (klass != nullptr) { |
| 2209 | return EnsureResolved(self, descriptor, klass); |
| 2210 | } |
| 2211 | } |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2212 | // If the lookup failed search the boot class path. We don't perform a recursive call to avoid |
| 2213 | // a NoClassDefFoundError being allocated. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2214 | ClassPathEntry pair = FindInClassPath(descriptor, boot_class_path_); |
| 2215 | if (pair.second != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2216 | return DefineClass(self, descriptor, NullHandle<mirror::ClassLoader>(), *pair.first, |
| 2217 | *pair.second); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2218 | } |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2219 | // Next try the compile time class path. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2220 | const std::vector<const DexFile*>* class_path; |
| 2221 | { |
Ian Rogers | be7149f | 2013-08-20 09:29:39 -0700 | [diff] [blame] | 2222 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2223 | ScopedLocalRef<jobject> jclass_loader(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2224 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2225 | class_path = &Runtime::Current()->GetCompileTimeClassPath(jclass_loader.get()); |
| 2226 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2227 | pair = FindInClassPath(descriptor, *class_path); |
| 2228 | if (pair.second != nullptr) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2229 | return DefineClass(self, descriptor, class_loader, *pair.first, *pair.second); |
| 2230 | } else { |
| 2231 | // Use the pre-allocated NCDFE at compile time to avoid wasting time constructing exceptions. |
| 2232 | mirror::Throwable* pre_allocated = Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2233 | self->SetException(ThrowLocation(), pre_allocated); |
| 2234 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2235 | } |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2236 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2237 | ScopedObjectAccessUnchecked soa(self); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2238 | mirror::Class* klass = FindClassInPathClassLoader(soa, self, descriptor, class_loader); |
| 2239 | if (klass != nullptr) { |
| 2240 | return klass; |
Mathieu Chartier | 6bcae8f | 2014-09-04 18:33:17 -0700 | [diff] [blame] | 2241 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2242 | ScopedLocalRef<jobject> class_loader_object(soa.Env(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2243 | soa.AddLocalReference<jobject>(class_loader.Get())); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 2244 | std::string class_name_string(DescriptorToDot(descriptor)); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2245 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2246 | { |
| 2247 | ScopedThreadStateChange tsc(self, kNative); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2248 | ScopedLocalRef<jobject> class_name_object(soa.Env(), |
| 2249 | soa.Env()->NewStringUTF(class_name_string.c_str())); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2250 | if (class_name_object.get() == nullptr) { |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2251 | DCHECK(self->IsExceptionPending()); // OOME. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2252 | return nullptr; |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 2253 | } |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2254 | CHECK(class_loader_object.get() != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2255 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2256 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2257 | class_name_object.get())); |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2258 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2259 | if (self->IsExceptionPending()) { |
Elliott Hughes | 748382f | 2012-01-26 18:07:38 -0800 | [diff] [blame] | 2260 | // If the ClassLoader threw, pass that exception up. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2261 | return nullptr; |
| 2262 | } else if (result.get() == nullptr) { |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 2263 | // broken loader - throw NPE to be compatible with Dalvik |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2264 | ThrowNullPointerException(nullptr, StringPrintf("ClassLoader.loadClass returned null for %s", |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2265 | class_name_string.c_str()).c_str()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2266 | return nullptr; |
Ian Rogers | 761bfa8 | 2012-01-11 10:14:05 -0800 | [diff] [blame] | 2267 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2268 | // success, return mirror::Class* |
| 2269 | return soa.Decode<mirror::Class*>(result.get()); |
Ian Rogers | 6b0870d | 2011-12-15 19:38:12 -0800 | [diff] [blame] | 2270 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2271 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 2272 | UNREACHABLE(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2275 | mirror::Class* ClassLinker::DefineClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2276 | Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2277 | const DexFile& dex_file, |
| 2278 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2279 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2280 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2281 | bool should_allocate = false; |
| 2282 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2283 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2284 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2285 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2286 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2287 | klass.Assign(GetClassRoot(kJavaLangObject)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2288 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2289 | klass.Assign(GetClassRoot(kJavaLangClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2290 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2291 | klass.Assign(GetClassRoot(kJavaLangString)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2292 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
| 2293 | klass.Assign(GetClassRoot(kJavaLangRefReference)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2294 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2295 | klass.Assign(GetClassRoot(kJavaLangDexCache)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2296 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtField;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2297 | klass.Assign(GetClassRoot(kJavaLangReflectArtField)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2298 | } else if (strcmp(descriptor, "Ljava/lang/reflect/ArtMethod;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2299 | klass.Assign(GetClassRoot(kJavaLangReflectArtMethod)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2300 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2301 | should_allocate = true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2302 | } |
| 2303 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2304 | should_allocate = true; |
| 2305 | } |
| 2306 | |
| 2307 | if (should_allocate) { |
| 2308 | // Allocate a class with the status of not ready. |
| 2309 | // Interface object should get the right size here. Regular class will |
| 2310 | // figure out the right size later and be replaced with one of the right |
| 2311 | // size when the class becomes resolved. |
| 2312 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2313 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2314 | if (UNLIKELY(klass.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2315 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2316 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2317 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2318 | klass->SetDexCache(FindDexCache(dex_file)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2319 | LoadClass(self, dex_file, dex_class_def, klass, class_loader.Get()); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2320 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2321 | if (self->IsExceptionPending()) { |
| 2322 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2323 | // notification is necessary. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2324 | if (!klass->IsErroneous()) { |
| 2325 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2326 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2327 | return nullptr; |
| 2328 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2329 | klass->SetClinitThreadId(self->GetTid()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2330 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2331 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2332 | mirror::Class* existing = InsertClass(descriptor, klass.Get(), Hash(descriptor)); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2333 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2334 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2335 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2336 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2337 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2338 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2339 | // Finish loading (if necessary) by finding parents |
| 2340 | CHECK(!klass->IsLoaded()); |
| 2341 | if (!LoadSuperAndInterfaces(klass, dex_file)) { |
| 2342 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2343 | if (!klass->IsErroneous()) { |
| 2344 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2345 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2346 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2347 | } |
| 2348 | CHECK(klass->IsLoaded()); |
| 2349 | // Link the class (if necessary) |
| 2350 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2351 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2352 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2353 | |
| 2354 | mirror::Class* new_class = nullptr; |
| 2355 | if (!LinkClass(self, descriptor, klass, interfaces, &new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2356 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2357 | if (!klass->IsErroneous()) { |
| 2358 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 2359 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2360 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2361 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2362 | self->AssertNoPendingException(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2363 | CHECK(new_class != nullptr) << descriptor; |
| 2364 | CHECK(new_class->IsResolved()) << descriptor; |
| 2365 | |
| 2366 | Handle<mirror::Class> new_class_h(hs.NewHandle(new_class)); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2367 | |
| 2368 | /* |
| 2369 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2370 | * definition of "preparation" is creating the static fields for a |
| 2371 | * class and initializing them to the standard default values, but not |
| 2372 | * executing any code (that comes later, during "initialization"). |
| 2373 | * |
| 2374 | * We did the static preparation in LinkClass. |
| 2375 | * |
| 2376 | * The class has been prepared and resolved but possibly not yet verified |
| 2377 | * at this point. |
| 2378 | */ |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2379 | Dbg::PostClassPrepare(new_class_h.Get()); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2380 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2381 | return new_class_h.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2384 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2385 | const DexFile::ClassDef& dex_class_def) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 2386 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2387 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2388 | size_t num_8 = 0; |
| 2389 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2390 | size_t num_32 = 0; |
| 2391 | size_t num_64 = 0; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2392 | if (class_data != nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2393 | for (ClassDataItemIterator it(dex_file, class_data); it.HasNextStaticField(); it.Next()) { |
| 2394 | const DexFile::FieldId& field_id = dex_file.GetFieldId(it.GetMemberIndex()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 2395 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2396 | char c = descriptor[0]; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2397 | switch (c) { |
| 2398 | case 'L': |
| 2399 | case '[': |
| 2400 | num_ref++; |
| 2401 | break; |
| 2402 | case 'J': |
| 2403 | case 'D': |
| 2404 | num_64++; |
| 2405 | break; |
| 2406 | case 'I': |
| 2407 | case 'F': |
| 2408 | num_32++; |
| 2409 | break; |
| 2410 | case 'S': |
| 2411 | case 'C': |
| 2412 | num_16++; |
| 2413 | break; |
| 2414 | case 'B': |
| 2415 | case 'Z': |
| 2416 | num_8++; |
| 2417 | break; |
| 2418 | default: |
| 2419 | LOG(FATAL) << "Unknown descriptor: " << c; |
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(); |
| 2469 | return 0; |
| 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(); |
| 2485 | bool found = false; |
| 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()) { |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2490 | found = true; |
| 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 | } |
| 2495 | CHECK(found) << "Didn't find oat method index for virtual method: " << PrettyMethod(method); |
| 2496 | } |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2497 | DCHECK_EQ(oat_method_index, |
| 2498 | GetOatMethodIndexFromMethodIndex(*declaring_class->GetDexCache()->GetDexFile(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2499 | method->GetDeclaringClass()->GetDexClassDefIndex(), |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2500 | method->GetDexMethodIndex())); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2501 | OatFile::OatClass oat_class = FindOatClass(*declaring_class->GetDexCache()->GetDexFile(), |
| 2502 | declaring_class->GetDexClassDefIndex(), |
| 2503 | found); |
| 2504 | if (!found) { |
| 2505 | return OatFile::OatMethod::Invalid(); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2506 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2507 | *found = true; |
| 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; |
| 2518 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2519 | const void* result = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2520 | if (found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2521 | result = oat_method.GetQuickCode(); |
| 2522 | } |
| 2523 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2524 | if (result == nullptr) { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2525 | if (method->IsNative()) { |
| 2526 | // No code and native? Use generic trampoline. |
| 2527 | result = GetQuickGenericJniTrampoline(); |
| 2528 | } else if (method->IsPortableCompiled()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2529 | // No code? Do we expect portable code? |
| 2530 | result = GetQuickToPortableBridge(); |
| 2531 | } else { |
| 2532 | // No code? You must mean to go into the interpreter. |
| 2533 | result = GetQuickToInterpreterBridge(); |
| 2534 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2535 | } |
| 2536 | return result; |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2539 | const void* ClassLinker::GetPortableOatCodeFor(mirror::ArtMethod* method, |
| 2540 | bool* have_portable_code) { |
| 2541 | CHECK(!method->IsAbstract()) << PrettyMethod(method); |
| 2542 | *have_portable_code = false; |
| 2543 | if (method->IsProxyMethod()) { |
| 2544 | return GetPortableProxyInvokeHandler(); |
| 2545 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2546 | bool found; |
| 2547 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2548 | const void* result = nullptr; |
| 2549 | const void* quick_code = nullptr; |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2550 | if (found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2551 | result = oat_method.GetPortableCode(); |
| 2552 | quick_code = oat_method.GetQuickCode(); |
| 2553 | } |
| 2554 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2555 | if (result == nullptr) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2556 | if (quick_code == nullptr) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2557 | // No code? You must mean to go into the interpreter. |
| 2558 | result = GetPortableToInterpreterBridge(); |
| 2559 | } else { |
| 2560 | // No code? But there's quick code, so use a bridge. |
| 2561 | result = GetPortableToQuickBridge(); |
| 2562 | } |
| 2563 | } else { |
| 2564 | *have_portable_code = true; |
| 2565 | } |
| 2566 | return result; |
| 2567 | } |
| 2568 | |
Hiroshi Yamauchi | 9bdec88 | 2014-08-15 17:11:12 -0700 | [diff] [blame] | 2569 | const void* ClassLinker::GetOatMethodQuickCodeFor(mirror::ArtMethod* method) { |
| 2570 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2571 | return nullptr; |
| 2572 | } |
| 2573 | bool found; |
| 2574 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2575 | return found ? oat_method.GetQuickCode() : nullptr; |
| 2576 | } |
| 2577 | |
| 2578 | const void* ClassLinker::GetOatMethodPortableCodeFor(mirror::ArtMethod* method) { |
| 2579 | if (method->IsNative() || method->IsAbstract() || method->IsProxyMethod()) { |
| 2580 | return nullptr; |
| 2581 | } |
| 2582 | bool found; |
| 2583 | OatFile::OatMethod oat_method = FindOatMethodFor(method, &found); |
| 2584 | return found ? oat_method.GetPortableCode() : nullptr; |
| 2585 | } |
| 2586 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2587 | const void* ClassLinker::GetQuickOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2588 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2589 | bool found; |
| 2590 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2591 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2592 | return nullptr; |
| 2593 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2594 | 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] | 2595 | return oat_class.GetOatMethod(oat_method_idx).GetQuickCode(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | const void* ClassLinker::GetPortableOatCodeFor(const DexFile& dex_file, uint16_t class_def_idx, |
| 2599 | uint32_t method_idx) { |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2600 | bool found; |
| 2601 | OatFile::OatClass oat_class = FindOatClass(dex_file, class_def_idx, &found); |
| 2602 | if (!found) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2603 | return nullptr; |
| 2604 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2605 | 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] | 2606 | return oat_class.GetOatMethod(oat_method_idx).GetPortableCode(); |
Mathieu Chartier | e35517a | 2012-10-30 18:49:55 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2609 | // Returns true if the method must run with interpreter, false otherwise. |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2610 | static bool NeedsInterpreter( |
| 2611 | mirror::ArtMethod* method, const void* quick_code, const void* portable_code) |
| 2612 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2613 | if ((quick_code == nullptr) && (portable_code == nullptr)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2614 | // No code: need interpreter. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2615 | // May return true for native code, in the case of generic JNI |
| 2616 | // DCHECK(!method->IsNative()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2617 | return true; |
| 2618 | } |
Dragos Sbirlea | 90af14d | 2013-08-15 17:50:16 -0700 | [diff] [blame] | 2619 | #ifdef ART_SEA_IR_MODE |
| 2620 | ScopedObjectAccess soa(Thread::Current()); |
| 2621 | if (std::string::npos != PrettyMethod(method).find("fibonacci")) { |
| 2622 | LOG(INFO) << "Found " << PrettyMethod(method); |
| 2623 | return false; |
| 2624 | } |
| 2625 | #endif |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2626 | // If interpreter mode is enabled, every method (except native and proxy) must |
| 2627 | // be run with interpreter. |
| 2628 | return Runtime::Current()->GetInstrumentation()->InterpretOnly() && |
| 2629 | !method->IsNative() && !method->IsProxyMethod(); |
| 2630 | } |
| 2631 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2632 | void ClassLinker::FixupStaticTrampolines(mirror::Class* klass) { |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 2633 | DCHECK(klass->IsInitialized()) << PrettyDescriptor(klass); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2634 | if (klass->NumDirectMethods() == 0) { |
| 2635 | return; // No direct methods => no static methods. |
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 | Runtime* runtime = Runtime::Current(); |
| 2638 | if (!runtime->IsStarted() || runtime->UseCompileTimeClassPath()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2639 | if (runtime->IsCompiler() || runtime->GetHeap()->HasImageSpace()) { |
| 2640 | return; // OAT file unavailable. |
| 2641 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2642 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2643 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2644 | const DexFile& dex_file = klass->GetDexFile(); |
| 2645 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2646 | CHECK(dex_class_def != nullptr); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 2647 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2648 | // There should always be class data if there were direct methods. |
| 2649 | CHECK(class_data != nullptr) << PrettyDescriptor(klass); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2650 | ClassDataItemIterator it(dex_file, class_data); |
| 2651 | // Skip fields |
| 2652 | while (it.HasNextStaticField()) { |
| 2653 | it.Next(); |
| 2654 | } |
| 2655 | while (it.HasNextInstanceField()) { |
| 2656 | it.Next(); |
| 2657 | } |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2658 | bool has_oat_class; |
| 2659 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2660 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2661 | // Link the code of methods skipped by LinkCode. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2662 | for (size_t method_index = 0; it.HasNextDirectMethod(); ++method_index, it.Next()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2663 | mirror::ArtMethod* method = klass->GetDirectMethod(method_index); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2664 | if (!method->IsStatic()) { |
| 2665 | // Only update static methods. |
| 2666 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2667 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2668 | const void* portable_code = nullptr; |
| 2669 | const void* quick_code = nullptr; |
| 2670 | if (has_oat_class) { |
| 2671 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
| 2672 | portable_code = oat_method.GetPortableCode(); |
| 2673 | quick_code = oat_method.GetQuickCode(); |
| 2674 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2675 | const bool enter_interpreter = NeedsInterpreter(method, quick_code, portable_code); |
| 2676 | bool have_portable_code = false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2677 | if (enter_interpreter) { |
| 2678 | // Use interpreter entry point. |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2679 | // Check whether the method is native, in which case it's generic JNI. |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2680 | if (quick_code == nullptr && portable_code == nullptr && method->IsNative()) { |
| 2681 | quick_code = GetQuickGenericJniTrampoline(); |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2682 | portable_code = GetPortableToQuickBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2683 | } else { |
Ian Rogers | 1a57066 | 2014-03-12 01:02:21 -0700 | [diff] [blame] | 2684 | portable_code = GetPortableToInterpreterBridge(); |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 2685 | quick_code = GetQuickToInterpreterBridge(); |
| 2686 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2687 | } else { |
| 2688 | if (portable_code == nullptr) { |
| 2689 | portable_code = GetPortableToQuickBridge(); |
| 2690 | } else { |
| 2691 | have_portable_code = true; |
| 2692 | } |
| 2693 | if (quick_code == nullptr) { |
| 2694 | quick_code = GetQuickToPortableBridge(); |
| 2695 | } |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2696 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2697 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code, portable_code, |
| 2698 | have_portable_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2699 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2700 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2701 | } |
| 2702 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2703 | void ClassLinker::LinkCode(Handle<mirror::ArtMethod> method, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 2704 | const OatFile::OatClass* oat_class, |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2705 | const DexFile& dex_file, uint32_t dex_method_index, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 2706 | uint32_t method_index) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2707 | if (Runtime::Current()->IsCompiler()) { |
| 2708 | // The following code only applies to a non-compiler runtime. |
| 2709 | return; |
| 2710 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2711 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2712 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2713 | DCHECK(method->GetEntryPointFromPortableCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2714 | if (oat_class != nullptr) { |
| 2715 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2716 | // non-abstract methods also get their code pointers. |
| 2717 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(method_index); |
| 2718 | oat_method.LinkMethod(method.Get()); |
| 2719 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2720 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2721 | // Install entry point from interpreter. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2722 | bool enter_interpreter = NeedsInterpreter(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2723 | method->GetEntryPointFromQuickCompiledCode(), |
| 2724 | method->GetEntryPointFromPortableCompiledCode()); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2725 | if (enter_interpreter && !method->IsNative()) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2726 | method->SetEntryPointFromInterpreter(interpreter::artInterpreterToInterpreterBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2727 | } else { |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 2728 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2731 | if (method->IsAbstract()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2732 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2733 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2734 | return; |
| 2735 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2736 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2737 | bool have_portable_code = false; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2738 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2739 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2740 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2741 | // after initializing class (see ClassLinker::InitializeClass method). |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2742 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionTrampoline()); |
| 2743 | method->SetEntryPointFromPortableCompiledCode(GetPortableResolutionTrampoline()); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2744 | } else if (enter_interpreter) { |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 2745 | if (!method->IsNative()) { |
| 2746 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2747 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2748 | method->SetEntryPointFromPortableCompiledCode(GetPortableToInterpreterBridge()); |
| 2749 | } else { |
| 2750 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniTrampoline()); |
| 2751 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
| 2752 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2753 | } else if (method->GetEntryPointFromPortableCompiledCode() != nullptr) { |
| 2754 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
| 2755 | have_portable_code = true; |
| 2756 | method->SetEntryPointFromQuickCompiledCode(GetQuickToPortableBridge()); |
| 2757 | } else { |
| 2758 | DCHECK(method->GetEntryPointFromQuickCompiledCode() != nullptr); |
| 2759 | method->SetEntryPointFromPortableCompiledCode(GetPortableToQuickBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2760 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 2761 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2762 | if (method->IsNative()) { |
| 2763 | // Unregistering restores the dlsym lookup stub. |
| 2764 | method->UnregisterNative(Thread::Current()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2765 | |
| 2766 | if (enter_interpreter) { |
| 2767 | // We have a native method here without code. Then it should have either the GenericJni |
| 2768 | // trampoline as entrypoint (non-static), or the Resolution trampoline (static). |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 2769 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == GetQuickResolutionTrampoline() |
Andreas Gampe | 9a6a99a | 2014-03-14 07:52:20 -0700 | [diff] [blame] | 2770 | || method->GetEntryPointFromQuickCompiledCode() == GetQuickGenericJniTrampoline()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 2771 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2772 | } |
Ian Rogers | f3e9855 | 2013-03-20 15:49:49 -0700 | [diff] [blame] | 2773 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2774 | // Allow instrumentation its chance to hijack code. |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2775 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2776 | runtime->GetInstrumentation()->UpdateMethodsCode(method.Get(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2777 | method->GetEntryPointFromQuickCompiledCode(), |
| 2778 | method->GetEntryPointFromPortableCompiledCode(), |
| 2779 | have_portable_code); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2782 | |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2783 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2784 | void ClassLinker::LoadClass(Thread* self, const DexFile& dex_file, |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2785 | const DexFile::ClassDef& dex_class_def, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2786 | Handle<mirror::Class> klass, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2787 | mirror::ClassLoader* class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2788 | CHECK(klass.Get() != nullptr); |
| 2789 | CHECK(klass->GetDexCache() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2790 | CHECK_EQ(mirror::Class::kStatusNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 2791 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2792 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2793 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2794 | klass->SetClass(GetClassRoot(kJavaLangClass)); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 2795 | if (kUseBakerOrBrooksReadBarrier) { |
| 2796 | klass->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2797 | } |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2798 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 2799 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2800 | klass->SetAccessFlags(access_flags); |
| 2801 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 2802 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2803 | klass->SetStatus(mirror::Class::kStatusIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2804 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 2805 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2806 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2807 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 2808 | const uint8_t* class_data = dex_file.GetClassData(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2809 | if (class_data == nullptr) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2810 | return; // no fields or methods - for example a marker interface |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2811 | } |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2812 | |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2813 | |
| 2814 | bool has_oat_class = false; |
| 2815 | if (Runtime::Current()->IsStarted() && !Runtime::Current()->UseCompileTimeClassPath()) { |
| 2816 | OatFile::OatClass oat_class = FindOatClass(dex_file, klass->GetDexClassDefIndex(), |
| 2817 | &has_oat_class); |
| 2818 | if (has_oat_class) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2819 | LoadClassMembers(self, dex_file, class_data, klass, class_loader, &oat_class); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2820 | } |
| 2821 | } |
| 2822 | if (!has_oat_class) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2823 | LoadClassMembers(self, dex_file, class_data, klass, class_loader, nullptr); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2827 | void ClassLinker::LoadClassMembers(Thread* self, const DexFile& dex_file, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 2828 | const uint8_t* class_data, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2829 | Handle<mirror::Class> klass, |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 2830 | mirror::ClassLoader* class_loader, |
| 2831 | const OatFile::OatClass* oat_class) { |
| 2832 | // Load fields. |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2833 | ClassDataItemIterator it(dex_file, class_data); |
| 2834 | if (it.NumStaticFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2835 | mirror::ObjectArray<mirror::ArtField>* statics = AllocArtFieldArray(self, it.NumStaticFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2836 | if (UNLIKELY(statics == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2837 | CHECK(self->IsExceptionPending()); // OOME. |
| 2838 | return; |
| 2839 | } |
| 2840 | klass->SetSFields(statics); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2841 | } |
| 2842 | if (it.NumInstanceFields() != 0) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2843 | mirror::ObjectArray<mirror::ArtField>* fields = |
| 2844 | AllocArtFieldArray(self, it.NumInstanceFields()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2845 | if (UNLIKELY(fields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2846 | CHECK(self->IsExceptionPending()); // OOME. |
| 2847 | return; |
| 2848 | } |
| 2849 | klass->SetIFields(fields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2850 | } |
| 2851 | for (size_t i = 0; it.HasNextStaticField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2852 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2853 | StackHandleScope<1> hs(self); |
| 2854 | Handle<mirror::ArtField> sfield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2855 | if (UNLIKELY(sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2856 | CHECK(self->IsExceptionPending()); // OOME. |
| 2857 | return; |
| 2858 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2859 | klass->SetStaticField(i, sfield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2860 | LoadField(dex_file, it, klass, sfield); |
| 2861 | } |
| 2862 | for (size_t i = 0; it.HasNextInstanceField(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2863 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2864 | StackHandleScope<1> hs(self); |
| 2865 | Handle<mirror::ArtField> ifield(hs.NewHandle(AllocArtField(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2866 | if (UNLIKELY(ifield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2867 | CHECK(self->IsExceptionPending()); // OOME. |
| 2868 | return; |
| 2869 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2870 | klass->SetInstanceField(i, ifield.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2871 | LoadField(dex_file, it, klass, ifield); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2874 | // Load methods. |
| 2875 | if (it.NumDirectMethods() != 0) { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2876 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2877 | mirror::ObjectArray<mirror::ArtMethod>* directs = |
| 2878 | AllocArtMethodArray(self, it.NumDirectMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2879 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2880 | CHECK(self->IsExceptionPending()); // OOME. |
| 2881 | return; |
| 2882 | } |
| 2883 | klass->SetDirectMethods(directs); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2884 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2885 | if (it.NumVirtualMethods() != 0) { |
| 2886 | // TODO: append direct methods to class object |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2887 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = |
| 2888 | AllocArtMethodArray(self, it.NumVirtualMethods()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2889 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2890 | CHECK(self->IsExceptionPending()); // OOME. |
| 2891 | return; |
| 2892 | } |
| 2893 | klass->SetVirtualMethods(virtuals); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2894 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2895 | size_t class_def_method_index = 0; |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2896 | uint32_t last_dex_method_index = DexFile::kDexNoIndex; |
| 2897 | size_t last_class_def_method_index = 0; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2898 | for (size_t i = 0; it.HasNextDirectMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2899 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2900 | StackHandleScope<1> hs(self); |
| 2901 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2902 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2903 | CHECK(self->IsExceptionPending()); // OOME. |
| 2904 | return; |
| 2905 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2906 | klass->SetDirectMethod(i, method.Get()); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2907 | LinkCode(method, oat_class, dex_file, it.GetMemberIndex(), class_def_method_index); |
Jeff Hao | 68caf9e | 2014-09-03 13:48:16 -0700 | [diff] [blame] | 2908 | uint32_t it_method_index = it.GetMemberIndex(); |
| 2909 | if (last_dex_method_index == it_method_index) { |
| 2910 | // duplicate case |
| 2911 | method->SetMethodIndex(last_class_def_method_index); |
| 2912 | } else { |
| 2913 | method->SetMethodIndex(class_def_method_index); |
| 2914 | last_dex_method_index = it_method_index; |
| 2915 | last_class_def_method_index = class_def_method_index; |
| 2916 | } |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2917 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2918 | } |
| 2919 | for (size_t i = 0; it.HasNextVirtualMethod(); i++, it.Next()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2920 | self->AllowThreadSuspension(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2921 | StackHandleScope<1> hs(self); |
| 2922 | Handle<mirror::ArtMethod> method(hs.NewHandle(LoadMethod(self, dex_file, it, klass))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2923 | if (UNLIKELY(method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2924 | CHECK(self->IsExceptionPending()); // OOME. |
| 2925 | return; |
| 2926 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2927 | klass->SetVirtualMethod(i, method.Get()); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2928 | DCHECK_EQ(class_def_method_index, it.NumDirectMethods() + i); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2929 | LinkCode(method, oat_class, dex_file, it.GetMemberIndex(), class_def_method_index); |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 2930 | class_def_method_index++; |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2931 | } |
| 2932 | DCHECK(!it.HasNext()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 2935 | void ClassLinker::LoadField(const DexFile& /*dex_file*/, const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2936 | Handle<mirror::Class> klass, |
| 2937 | Handle<mirror::ArtField> dst) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2938 | uint32_t field_idx = it.GetMemberIndex(); |
| 2939 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2940 | dst->SetDeclaringClass(klass.Get()); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2941 | dst->SetAccessFlags(it.GetFieldAccessFlags()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2944 | mirror::ArtMethod* ClassLinker::LoadMethod(Thread* self, const DexFile& dex_file, |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2945 | const ClassDataItemIterator& it, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 2946 | Handle<mirror::Class> klass) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2947 | uint32_t dex_method_idx = it.GetMemberIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2948 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2949 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2950 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2951 | mirror::ArtMethod* dst = AllocArtMethod(self); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2952 | if (UNLIKELY(dst == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2953 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2954 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2955 | } |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 2956 | DCHECK(dst->IsArtMethod()) << PrettyDescriptor(dst->GetClass()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2957 | |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 2958 | ScopedAssertNoThreadSuspension ants(self, "LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2959 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2960 | dst->SetDeclaringClass(klass.Get()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 2961 | dst->SetCodeItemOffset(it.GetMethodCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 2962 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2963 | dst->SetDexCacheStrings(klass->GetDexCache()->GetStrings()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2964 | dst->SetDexCacheResolvedMethods(klass->GetDexCache()->GetResolvedMethods()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 2965 | dst->SetDexCacheResolvedTypes(klass->GetDexCache()->GetResolvedTypes()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 2966 | |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 2967 | uint32_t access_flags = it.GetMethodAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2968 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2969 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2970 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2971 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 2972 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2973 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2974 | klass->SetFinalizable(); |
| 2975 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2976 | std::string temp; |
| 2977 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2978 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 2979 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 2980 | // subclasses, so we exclude it here. |
| 2981 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 2982 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 2983 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 2984 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2985 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2986 | } |
| 2987 | } |
| 2988 | } |
| 2989 | } else if (method_name[0] == '<') { |
| 2990 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 2991 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 2992 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2993 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 2994 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 2995 | } else { |
| 2996 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 2997 | 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] | 2998 | << PrettyDescriptor(klass.Get()) << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 2999 | access_flags |= kAccConstructor; |
| 3000 | } |
| 3001 | } |
| 3002 | } |
| 3003 | dst->SetAccessFlags(access_flags); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3004 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3005 | return dst; |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3008 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3009 | StackHandleScope<1> hs(self); |
| 3010 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3011 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 3012 | << dex_file.GetLocation(); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 3013 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3016 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3017 | Handle<mirror::DexCache> dex_cache) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3018 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3019 | boot_class_path_.push_back(&dex_file); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3020 | RegisterDexFile(dex_file, dex_cache); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3023 | bool ClassLinker::IsDexFileRegisteredLocked(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3024 | dex_lock_.AssertSharedHeld(Thread::Current()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3025 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3026 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3027 | if (dex_cache->GetDexFile() == &dex_file) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3028 | return true; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | return false; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3034 | bool ClassLinker::IsDexFileRegistered(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3035 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | 0691851 | 2011-10-16 23:39:12 -0700 | [diff] [blame] | 3036 | return IsDexFileRegisteredLocked(dex_file); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3037 | } |
| 3038 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3039 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3040 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3041 | dex_lock_.AssertExclusiveHeld(Thread::Current()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3042 | CHECK(dex_cache.Get() != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 3043 | CHECK(dex_cache->GetLocation()->Equals(dex_file.GetLocation())) |
| 3044 | << dex_cache->GetLocation()->ToModifiedUtf8() << " " << dex_file.GetLocation(); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3045 | dex_caches_.push_back(GcRoot<mirror::DexCache>(dex_cache.Get())); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3046 | dex_cache->SetDexFile(&dex_file); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3047 | if (log_new_dex_caches_roots_) { |
| 3048 | // TODO: This is not safe if we can remove dex caches. |
| 3049 | new_dex_cache_roots_.push_back(dex_caches_.size() - 1); |
| 3050 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3053 | void ClassLinker::RegisterDexFile(const DexFile& dex_file) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3054 | Thread* self = Thread::Current(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3055 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3056 | ReaderMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3057 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 3058 | return; |
| 3059 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3060 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3061 | // Don't alloc while holding the lock, since allocation may need to |
| 3062 | // suspend all threads and another thread may need the dex_lock_ to |
| 3063 | // get to a suspend point. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3064 | StackHandleScope<1> hs(self); |
| 3065 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3066 | CHECK(dex_cache.Get() != nullptr) << "Failed to allocate dex cache for " |
| 3067 | << dex_file.GetLocation(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3068 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3069 | WriterMutexLock mu(self, dex_lock_); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3070 | if (IsDexFileRegisteredLocked(dex_file)) { |
| 3071 | return; |
| 3072 | } |
| 3073 | RegisterDexFileLocked(dex_file, dex_cache); |
| 3074 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3075 | } |
| 3076 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3077 | void ClassLinker::RegisterDexFile(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3078 | Handle<mirror::DexCache> dex_cache) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3079 | WriterMutexLock mu(Thread::Current(), dex_lock_); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3080 | RegisterDexFileLocked(dex_file, dex_cache); |
| 3081 | } |
| 3082 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3083 | mirror::DexCache* ClassLinker::FindDexCache(const DexFile& dex_file) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3084 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3085 | // Search assuming unique-ness of dex file. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3086 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3087 | mirror::DexCache* dex_cache = GetDexCache(i); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3088 | if (dex_cache->GetDexFile() == &dex_file) { |
| 3089 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3090 | } |
| 3091 | } |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3092 | // Search matching by location name. |
| 3093 | std::string location(dex_file.GetLocation()); |
| 3094 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3095 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3096 | if (dex_cache->GetDexFile()->GetLocation() == location) { |
| 3097 | return dex_cache; |
| 3098 | } |
| 3099 | } |
| 3100 | // Failure, dump diagnostic and abort. |
| 3101 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3102 | mirror::DexCache* dex_cache = GetDexCache(i); |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 3103 | LOG(ERROR) << "Registered dex file " << i << " = " << dex_cache->GetDexFile()->GetLocation(); |
| 3104 | } |
| 3105 | LOG(FATAL) << "Failed to find DexCache for DexFile " << location; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3106 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3109 | void ClassLinker::FixupDexCaches(mirror::ArtMethod* resolution_method) { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3110 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3111 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3112 | mirror::DexCache* dex_cache = GetDexCache(i); |
| 3113 | dex_cache->Fixup(resolution_method); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3117 | mirror::Class* ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3118 | mirror::Class* klass = AllocClass(self, mirror::Class::PrimitiveClassSize()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3119 | if (UNLIKELY(klass == nullptr)) { |
| 3120 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3121 | } |
| 3122 | return InitializePrimitiveClass(klass, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3123 | } |
| 3124 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3125 | mirror::Class* ClassLinker::InitializePrimitiveClass(mirror::Class* primitive_class, |
| 3126 | Primitive::Type type) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3127 | CHECK(primitive_class != nullptr); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3128 | // Must hold lock on object when initializing. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3129 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3130 | StackHandleScope<1> hs(self); |
| 3131 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3132 | ObjectLock<mirror::Class> lock(self, h_class); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3133 | primitive_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3134 | primitive_class->SetPrimitiveType(type); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3135 | primitive_class->SetStatus(mirror::Class::kStatusInitialized, self); |
| 3136 | const char* descriptor = Primitive::Descriptor(type); |
| 3137 | mirror::Class* existing = InsertClass(descriptor, primitive_class, Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3138 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3139 | return primitive_class; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3140 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3141 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3142 | // Create an array class (i.e. the class object for the array, not the |
| 3143 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3144 | // "[Ljava/lang/String;". |
| 3145 | // |
| 3146 | // If "descriptor" refers to an array of primitives, look up the |
| 3147 | // primitive type's internally-generated class object. |
| 3148 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3149 | // "class_loader" is the class loader of the class that's referring to |
| 3150 | // us. It's used to ensure that we're looking for the element type in |
| 3151 | // the right context. It does NOT become the class loader for the |
| 3152 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3153 | // |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3154 | // Returns nullptr with an exception raised on failure. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 3155 | mirror::Class* ClassLinker::CreateArrayClass(Thread* self, const char* descriptor, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3156 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3157 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3158 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3159 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3160 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3161 | class_loader))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3162 | if (component_type.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3163 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3164 | // We need to accept erroneous classes as component types. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3165 | component_type.Assign(LookupClass(self, descriptor + 1, class_loader.Get())); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3166 | if (component_type.Get() == nullptr) { |
| 3167 | DCHECK(self->IsExceptionPending()); |
| 3168 | return nullptr; |
| 3169 | } else { |
| 3170 | self->ClearException(); |
| 3171 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3172 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3173 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3174 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3175 | return nullptr; |
| 3176 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3177 | // See if the component type is already loaded. Array classes are |
| 3178 | // always associated with the class loader of their underlying |
| 3179 | // element type -- an array of Strings goes with the loader for |
| 3180 | // java/lang/String -- so we need to look for it there. (The |
| 3181 | // caller should have checked for the existence of the class |
| 3182 | // before calling here, but they did so with *their* class loader, |
| 3183 | // not the component type's loader.) |
| 3184 | // |
| 3185 | // If we find it, the caller adds "loader" to the class' initiating |
| 3186 | // loader list, which should prevent us from going through this again. |
| 3187 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3188 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3189 | // are the same, because our caller (FindClass) just did the |
| 3190 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3191 | // because we effectively do this lookup again when we add the new |
| 3192 | // class to the hash table --- necessary because of possible races with |
| 3193 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3194 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3195 | mirror::Class* new_class = LookupClass(self, descriptor, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3196 | if (new_class != nullptr) { |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3197 | return new_class; |
| 3198 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3199 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3200 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3201 | // Fill out the fields in the Class. |
| 3202 | // |
| 3203 | // It is possible to execute some methods against arrays, because |
| 3204 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3205 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3206 | // |
| 3207 | // Array classes are simple enough that we don't need to do a full |
| 3208 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3209 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3210 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3211 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3212 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3213 | new_class.Assign(GetClassRoot(kClassArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3214 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3215 | new_class.Assign(GetClassRoot(kObjectArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3216 | } else if (strcmp(descriptor, class_roots_descriptors_[kJavaLangStringArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3217 | new_class.Assign(GetClassRoot(kJavaLangStringArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3218 | } else if (strcmp(descriptor, |
| 3219 | class_roots_descriptors_[kJavaLangReflectArtMethodArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3220 | new_class.Assign(GetClassRoot(kJavaLangReflectArtMethodArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3221 | } else if (strcmp(descriptor, |
| 3222 | class_roots_descriptors_[kJavaLangReflectArtFieldArrayClass]) == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3223 | new_class.Assign(GetClassRoot(kJavaLangReflectArtFieldArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3224 | } else if (strcmp(descriptor, "[C") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3225 | new_class.Assign(GetClassRoot(kCharArrayClass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3226 | } else if (strcmp(descriptor, "[I") == 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3227 | new_class.Assign(GetClassRoot(kIntArrayClass)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3228 | } |
| 3229 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3230 | if (new_class.Get() == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3231 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3232 | if (new_class.Get() == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3233 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3234 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3235 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3236 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3237 | 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] | 3238 | DCHECK(new_class->GetComponentType() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3239 | mirror::Class* java_lang_Object = GetClassRoot(kJavaLangObject); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3240 | new_class->SetSuperClass(java_lang_Object); |
| 3241 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3242 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3243 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3244 | new_class->SetStatus(mirror::Class::kStatusLoaded, self); |
| 3245 | new_class->PopulateEmbeddedImtAndVTable(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3246 | new_class->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3247 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3248 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3249 | |
| 3250 | |
| 3251 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3252 | // interfaces. We need to set that up here, so that stuff like |
| 3253 | // "instanceof" works right. |
| 3254 | // |
| 3255 | // Note: The GC could run during the call to FindSystemClass, |
| 3256 | // so we need to make sure the class object is GC-valid while we're in |
| 3257 | // there. Do this by clearing the interface list so the GC will just |
| 3258 | // think that the entries are null. |
| 3259 | |
| 3260 | |
| 3261 | // Use the single, global copies of "interfaces" and "iftable" |
| 3262 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3263 | { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3264 | mirror::IfTable* array_iftable = array_iftable_.Read(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3265 | CHECK(array_iftable != nullptr); |
| 3266 | new_class->SetIfTable(array_iftable); |
| 3267 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3268 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3269 | // Inherit access flags from the component type. |
| 3270 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3271 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3272 | access_flags &= kAccJavaFlagsMask; |
| 3273 | // 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] | 3274 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3275 | access_flags |= kAccAbstract | kAccFinal; |
| 3276 | access_flags &= ~kAccInterface; |
| 3277 | |
| 3278 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3279 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3280 | mirror::Class* existing = InsertClass(descriptor, new_class.Get(), Hash(descriptor)); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3281 | if (existing == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3282 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3283 | } |
| 3284 | // Another thread must have loaded the class after we |
| 3285 | // started but before we finished. Abandon what we've |
| 3286 | // done. |
| 3287 | // |
| 3288 | // (Yes, this happens.) |
| 3289 | |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3290 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3291 | } |
| 3292 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3293 | mirror::Class* ClassLinker::FindPrimitiveClass(char type) { |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3294 | switch (type) { |
| 3295 | case 'B': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3296 | return GetClassRoot(kPrimitiveByte); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3297 | case 'C': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3298 | return GetClassRoot(kPrimitiveChar); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3299 | case 'D': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3300 | return GetClassRoot(kPrimitiveDouble); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3301 | case 'F': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3302 | return GetClassRoot(kPrimitiveFloat); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3303 | case 'I': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3304 | return GetClassRoot(kPrimitiveInt); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3305 | case 'J': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3306 | return GetClassRoot(kPrimitiveLong); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3307 | case 'S': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3308 | return GetClassRoot(kPrimitiveShort); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3309 | case 'Z': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3310 | return GetClassRoot(kPrimitiveBoolean); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3311 | case 'V': |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3312 | return GetClassRoot(kPrimitiveVoid); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3313 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3314 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3315 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3316 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3317 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3318 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3321 | mirror::Class* ClassLinker::InsertClass(const char* descriptor, mirror::Class* klass, |
| 3322 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3323 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3324 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3325 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3326 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3327 | source += " from "; |
| 3328 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3329 | } |
| 3330 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3331 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3332 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 3333 | mirror::Class* existing = |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3334 | LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3335 | if (existing != nullptr) { |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3336 | return existing; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3337 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3338 | if (kIsDebugBuild && !klass->IsTemp() && klass->GetClassLoader() == nullptr && |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3339 | dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3340 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3341 | // is in the image. |
| 3342 | existing = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3343 | if (existing != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3344 | CHECK(klass == existing); |
| 3345 | } |
| 3346 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 3347 | VerifyObject(klass); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3348 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3349 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3350 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3351 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3352 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3353 | } |
| 3354 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3355 | mirror::Class* ClassLinker::UpdateClass(const char* descriptor, mirror::Class* klass, |
| 3356 | size_t hash) { |
| 3357 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3358 | mirror::Class* existing = |
| 3359 | LookupClassFromTableLocked(descriptor, klass->GetClassLoader(), hash); |
| 3360 | |
| 3361 | if (existing == nullptr) { |
| 3362 | CHECK(klass->IsProxyClass()); |
| 3363 | return nullptr; |
| 3364 | } |
| 3365 | |
| 3366 | CHECK_NE(existing, klass) << descriptor; |
| 3367 | CHECK(!existing->IsResolved()) << descriptor; |
| 3368 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor; |
| 3369 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3370 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
| 3371 | it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3372 | mirror::Class* klass = it->second.Read(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 3373 | if (klass == existing) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3374 | class_table_.erase(it); |
| 3375 | break; |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | CHECK(!klass->IsTemp()) << descriptor; |
| 3380 | if (kIsDebugBuild && klass->GetClassLoader() == nullptr && |
| 3381 | dex_cache_image_class_lookup_required_) { |
| 3382 | // Check a class loaded with the system class loader matches one in the image if the class |
| 3383 | // is in the image. |
| 3384 | existing = LookupClassFromImage(descriptor); |
| 3385 | if (existing != nullptr) { |
| 3386 | CHECK(klass == existing) << descriptor; |
| 3387 | } |
| 3388 | } |
| 3389 | VerifyObject(klass); |
| 3390 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3391 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3392 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3393 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | return existing; |
| 3397 | } |
| 3398 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3399 | bool ClassLinker::RemoveClass(const char* descriptor, const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 3400 | size_t hash = Hash(descriptor); |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3401 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3402 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
| 3403 | it != end && it->first == hash; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3404 | ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3405 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3406 | if (klass->GetClassLoader() == class_loader && klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3407 | class_table_.erase(it); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3408 | return true; |
| 3409 | } |
| 3410 | } |
| 3411 | return false; |
| 3412 | } |
| 3413 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3414 | mirror::Class* ClassLinker::LookupClass(Thread* self, const char* descriptor, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3415 | const mirror::ClassLoader* class_loader) { |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 3416 | size_t hash = Hash(descriptor); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3417 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3418 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3419 | mirror::Class* result = LookupClassFromTableLocked(descriptor, class_loader, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3420 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3421 | return result; |
| 3422 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3423 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3424 | if (class_loader != nullptr || !dex_cache_image_class_lookup_required_) { |
| 3425 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3426 | } else { |
| 3427 | // Lookup failed but need to search dex_caches_. |
| 3428 | mirror::Class* result = LookupClassFromImage(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3429 | if (result != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3430 | InsertClass(descriptor, result, hash); |
| 3431 | } else { |
| 3432 | // Searching the image dex files/caches failed, we don't want to get into this situation |
| 3433 | // often as map searches are faster, so after kMaxFailedDexCacheLookups move all image |
| 3434 | // classes into the class table. |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3435 | constexpr uint32_t kMaxFailedDexCacheLookups = 1000; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3436 | if (++failed_dex_cache_class_lookups_ > kMaxFailedDexCacheLookups) { |
| 3437 | MoveImageClassesToClassTable(); |
| 3438 | } |
| 3439 | } |
| 3440 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3441 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3442 | } |
| 3443 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3444 | mirror::Class* ClassLinker::LookupClassFromTableLocked(const char* descriptor, |
| 3445 | const mirror::ClassLoader* class_loader, |
| 3446 | size_t hash) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3447 | auto end = class_table_.end(); |
| 3448 | for (auto it = class_table_.lower_bound(hash); it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3449 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3450 | if (klass->GetClassLoader() == class_loader && klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3451 | if (kIsDebugBuild) { |
| 3452 | // Check for duplicates in the table. |
| 3453 | for (++it; it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3454 | mirror::Class* klass2 = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3455 | CHECK(!(klass2->GetClassLoader() == class_loader && |
| 3456 | klass2->DescriptorEquals(descriptor))) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3457 | << PrettyClass(klass) << " " << klass << " " << klass->GetClassLoader() << " " |
| 3458 | << PrettyClass(klass2) << " " << klass2 << " " << klass2->GetClassLoader(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3459 | } |
Brian Carlstrom | 07bb855 | 2012-01-18 22:10:50 -0800 | [diff] [blame] | 3460 | } |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 3461 | return klass; |
| 3462 | } |
| 3463 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3464 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3467 | static mirror::ObjectArray<mirror::DexCache>* GetImageDexCaches() |
| 3468 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 3469 | gc::space::ImageSpace* image = Runtime::Current()->GetHeap()->GetImageSpace(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3470 | CHECK(image != nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3471 | mirror::Object* root = image->GetImageHeader().GetImageRoot(ImageHeader::kDexCaches); |
| 3472 | return root->AsObjectArray<mirror::DexCache>(); |
| 3473 | } |
| 3474 | |
| 3475 | void ClassLinker::MoveImageClassesToClassTable() { |
| 3476 | Thread* self = Thread::Current(); |
| 3477 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3478 | if (!dex_cache_image_class_lookup_required_) { |
| 3479 | return; // All dex cache classes are already in the class table. |
| 3480 | } |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3481 | ScopedAssertNoThreadSuspension ants(self, "Moving image classes to class table"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3482 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3483 | std::string temp; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3484 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 3485 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3486 | mirror::ObjectArray<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 3487 | for (int32_t j = 0; j < types->GetLength(); j++) { |
| 3488 | mirror::Class* klass = types->Get(j); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3489 | if (klass != nullptr) { |
| 3490 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3491 | const char* descriptor = klass->GetDescriptor(&temp); |
| 3492 | size_t hash = Hash(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3493 | mirror::Class* existing = LookupClassFromTableLocked(descriptor, nullptr, hash); |
| 3494 | if (existing != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3495 | CHECK(existing == klass) << PrettyClassAndClassLoader(existing) << " != " |
| 3496 | << PrettyClassAndClassLoader(klass); |
| 3497 | } else { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3498 | class_table_.insert(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3499 | if (log_new_class_table_roots_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3500 | new_class_roots_.push_back(std::make_pair(hash, GcRoot<mirror::Class>(klass))); |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3501 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3502 | } |
| 3503 | } |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3504 | } |
| 3505 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3506 | dex_cache_image_class_lookup_required_ = false; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | mirror::Class* ClassLinker::LookupClassFromImage(const char* descriptor) { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 3510 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "Image class lookup"); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3511 | mirror::ObjectArray<mirror::DexCache>* dex_caches = GetImageDexCaches(); |
| 3512 | for (int32_t i = 0; i < dex_caches->GetLength(); ++i) { |
| 3513 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 3514 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | 801a811 | 2014-01-06 14:28:16 +0000 | [diff] [blame] | 3515 | // Try binary searching the string/type index. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3516 | const DexFile::StringId* string_id = dex_file->FindStringId(descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3517 | if (string_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3518 | const DexFile::TypeId* type_id = |
| 3519 | dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3520 | if (type_id != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3521 | uint16_t type_idx = dex_file->GetIndexForTypeId(*type_id); |
| 3522 | mirror::Class* klass = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3523 | if (klass != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3524 | return klass; |
| 3525 | } |
| 3526 | } |
| 3527 | } |
| 3528 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3529 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | void ClassLinker::LookupClasses(const char* descriptor, std::vector<mirror::Class*>& result) { |
| 3533 | result.clear(); |
| 3534 | if (dex_cache_image_class_lookup_required_) { |
| 3535 | MoveImageClassesToClassTable(); |
| 3536 | } |
| 3537 | size_t hash = Hash(descriptor); |
| 3538 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3539 | for (auto it = class_table_.lower_bound(hash), end = class_table_.end(); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 3540 | it != end && it->first == hash; ++it) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 3541 | mirror::Class* klass = it->second.Read(); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 3542 | if (klass->DescriptorEquals(descriptor)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3543 | result.push_back(klass); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3544 | } |
| 3545 | } |
| 3546 | } |
| 3547 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3548 | void ClassLinker::VerifyClass(Thread* self, Handle<mirror::Class> klass) { |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3549 | // TODO: assert that the monitor on the Class is held |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3550 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 3551 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3552 | // Don't attempt to re-verify if already sufficiently verified. |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3553 | if (klass->IsVerified()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3554 | EnsurePreverifiedMethods(klass); |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3555 | return; |
| 3556 | } |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3557 | if (klass->IsCompileTimeVerified() && Runtime::Current()->IsCompiler()) { |
| 3558 | return; |
| 3559 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3560 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3561 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 3562 | // this class as a parent to another. |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3563 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3564 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 3565 | return; |
| 3566 | } |
| 3567 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3568 | if (klass->GetStatus() == mirror::Class::kStatusResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3569 | klass->SetStatus(mirror::Class::kStatusVerifying, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3570 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3571 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3572 | << PrettyClass(klass.Get()); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3573 | CHECK(!Runtime::Current()->IsCompiler()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3574 | klass->SetStatus(mirror::Class::kStatusVerifyingAtRuntime, self); |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3575 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3576 | |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3577 | // Skip verification if disabled. |
| 3578 | if (!Runtime::Current()->IsVerificationEnabled()) { |
| 3579 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3580 | EnsurePreverifiedMethods(klass); |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 3581 | return; |
| 3582 | } |
| 3583 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3584 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3585 | StackHandleScope<2> hs(self); |
| 3586 | Handle<mirror::Class> super(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3587 | if (super.Get() != nullptr) { |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 3588 | // Acquire lock to prevent races on verifying the super class. |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3589 | ObjectLock<mirror::Class> lock(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3590 | |
| 3591 | if (!super->IsVerified() && !super->IsErroneous()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3592 | VerifyClass(self, super); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3593 | } |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3594 | if (!super->IsCompileTimeVerified()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3595 | std::string error_msg( |
| 3596 | StringPrintf("Rejecting class %s that attempts to sub-class erroneous class %s", |
| 3597 | PrettyDescriptor(klass.Get()).c_str(), |
| 3598 | PrettyDescriptor(super.Get()).c_str())); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3599 | LOG(ERROR) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3600 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
| 3601 | if (cause.Get() != nullptr) { |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3602 | self->ClearException(); |
| 3603 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3604 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
| 3605 | if (cause.Get() != nullptr) { |
| 3606 | self->GetException(nullptr)->SetCause(cause.Get()); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3607 | } |
Jeff Hao | 22cb09b | 2013-12-12 14:29:15 -0800 | [diff] [blame] | 3608 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 3609 | if (Runtime::Current()->IsCompiler()) { |
| 3610 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3611 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3612 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3613 | return; |
| 3614 | } |
| 3615 | } |
| 3616 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3617 | // 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] | 3618 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3619 | mirror::Class::Status oat_file_class_status(mirror::Class::kStatusNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3620 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3621 | if (oat_file_class_status == mirror::Class::kStatusError) { |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3622 | VLOG(class_linker) << "Skipping runtime verification of erroneous class " |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3623 | << PrettyDescriptor(klass.Get()) << " in " |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 3624 | << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3625 | ThrowVerifyError(klass.Get(), "Rejecting class %s because it failed compile-time verification", |
| 3626 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3627 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | ec01423 | 2012-09-05 10:42:25 -0700 | [diff] [blame] | 3628 | return; |
| 3629 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3630 | verifier::MethodVerifier::FailureKind verifier_failure = verifier::MethodVerifier::kNoFailure; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3631 | std::string error_msg; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3632 | if (!preverified) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3633 | verifier_failure = verifier::MethodVerifier::VerifyClass(self, klass.Get(), |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3634 | Runtime::Current()->IsCompiler(), |
| 3635 | &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 3636 | } |
| 3637 | if (preverified || verifier_failure != verifier::MethodVerifier::kHardFailure) { |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3638 | if (!preverified && verifier_failure != verifier::MethodVerifier::kNoFailure) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3639 | VLOG(class_linker) << "Soft verification failure in class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 3640 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3641 | << " because: " << error_msg; |
| 3642 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3643 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3644 | // Make sure all classes referenced by catch blocks are resolved. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3645 | ResolveClassExceptionHandlerTypes(dex_file, klass); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3646 | if (verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3647 | // Even though there were no verifier failures we need to respect whether the super-class |
| 3648 | // was verified or requiring runtime reverification. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3649 | if (super.Get() == nullptr || super->IsVerified()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3650 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3651 | } else { |
| 3652 | CHECK_EQ(super->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3653 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3654 | // Pretend a soft failure occured so that we don't consider the class verified below. |
| 3655 | verifier_failure = verifier::MethodVerifier::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 3656 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3657 | } else { |
| 3658 | CHECK_EQ(verifier_failure, verifier::MethodVerifier::kSoftFailure); |
| 3659 | // Soft failures at compile time should be retried at runtime. Soft |
| 3660 | // failures at runtime will be handled by slow paths in the generated |
| 3661 | // code. Set status accordingly. |
| 3662 | if (Runtime::Current()->IsCompiler()) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3663 | klass->SetStatus(mirror::Class::kStatusRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3664 | } else { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3665 | klass->SetStatus(mirror::Class::kStatusVerified, self); |
Andreas Gampe | bb0c7f6 | 2014-09-11 10:59:33 -0700 | [diff] [blame] | 3666 | // As this is a fake verified status, make sure the methods are _not_ marked preverified |
| 3667 | // later. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3668 | klass->SetPreverified(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 3669 | } |
| 3670 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3671 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3672 | LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(klass.Get()) |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 3673 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 3674 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3675 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3676 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3677 | klass->SetStatus(mirror::Class::kStatusError, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 3678 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3679 | if (preverified || verifier_failure == verifier::MethodVerifier::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 3680 | // 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] | 3681 | // Let the interpreter know it by setting the kAccPreverified flag onto each |
| 3682 | // method. |
| 3683 | // Note: we're going here during compilation and at runtime. When we set the |
| 3684 | // kAccPreverified flag when compiling image classes, the flag is recorded |
| 3685 | // in the image and is set when loading the image. |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3686 | EnsurePreverifiedMethods(klass); |
| 3687 | } |
| 3688 | } |
| 3689 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3690 | void ClassLinker::EnsurePreverifiedMethods(Handle<mirror::Class> klass) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3691 | if (!klass->IsPreverified()) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3692 | klass->SetPreverifiedFlagOnAllMethods(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3693 | klass->SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 3694 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 3695 | } |
| 3696 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3697 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, mirror::Class* klass, |
| 3698 | mirror::Class::Status& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3699 | // If we're compiling, we can only verify the class using the oat file if |
| 3700 | // we are not compiling the image or if the class we're verifying is not part of |
| 3701 | // the app. In other words, we will only check for preverification of bootclasspath |
| 3702 | // classes. |
| 3703 | if (Runtime::Current()->IsCompiler()) { |
| 3704 | // Are we compiling the bootclasspath? |
| 3705 | if (!Runtime::Current()->UseCompileTimeClassPath()) { |
| 3706 | return false; |
| 3707 | } |
| 3708 | // We are compiling an app (not the image). |
| 3709 | |
| 3710 | // Is this an app class? (I.e. not a bootclasspath class) |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3711 | if (klass->GetClassLoader() != nullptr) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3712 | return false; |
| 3713 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3714 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3715 | |
Vladimir Marko | aa4497d | 2014-09-05 14:01:17 +0100 | [diff] [blame] | 3716 | const OatFile::OatDexFile* oat_dex_file = FindOpenedOatDexFileForDexFile(dex_file); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3717 | // In case we run without an image there won't be a backing oat file. |
| 3718 | if (oat_dex_file == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 3719 | return false; |
| 3720 | } |
| 3721 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3722 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 3723 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3724 | if (oat_file_class_status == mirror::Class::kStatusVerified || |
| 3725 | oat_file_class_status == mirror::Class::kStatusInitialized) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 3726 | return true; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3727 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3728 | if (oat_file_class_status == mirror::Class::kStatusRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3729 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 3730 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3731 | // class ... { |
| 3732 | // Foo x; |
| 3733 | // .... () { |
| 3734 | // if (...) { |
| 3735 | // v1 gets assigned a type of resolved class Foo |
| 3736 | // } else { |
| 3737 | // v1 gets assigned a type of unresolved class Bar |
| 3738 | // } |
| 3739 | // iput x = v1 |
| 3740 | // } } |
| 3741 | // when we merge v1 following the if-the-else it results in Conflict |
| 3742 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 3743 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 3744 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 3745 | // at compile time). |
| 3746 | return false; |
| 3747 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3748 | if (oat_file_class_status == mirror::Class::kStatusError) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 3749 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 3750 | // in the class. These errors are unrecoverable. |
| 3751 | return false; |
| 3752 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3753 | if (oat_file_class_status == mirror::Class::kStatusNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 3754 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 3755 | // not loading the class. |
| 3756 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 3757 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3758 | return false; |
| 3759 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3760 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 3761 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 3762 | << " " << dex_file.GetLocation() << " " << PrettyClass(klass) << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3763 | << klass->GetDescriptor(&temp); |
Brian Carlstrom | 5b332c8 | 2012-02-01 15:02:31 -0800 | [diff] [blame] | 3764 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3765 | return false; |
| 3766 | } |
| 3767 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3768 | void ClassLinker::ResolveClassExceptionHandlerTypes(const DexFile& dex_file, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3769 | Handle<mirror::Class> klass) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3770 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 3771 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetDirectMethod(i)); |
| 3772 | } |
| 3773 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 3774 | ResolveMethodExceptionHandlerTypes(dex_file, klass->GetVirtualMethod(i)); |
| 3775 | } |
| 3776 | } |
| 3777 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3778 | void ClassLinker::ResolveMethodExceptionHandlerTypes(const DexFile& dex_file, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3779 | mirror::ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3780 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
| 3781 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3782 | if (code_item == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3783 | return; // native or abstract method |
| 3784 | } |
| 3785 | if (code_item->tries_size_ == 0) { |
| 3786 | return; // nothing to process |
| 3787 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 3788 | const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item, 0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3789 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
| 3790 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
| 3791 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 3792 | CatchHandlerIterator iterator(handlers_ptr); |
| 3793 | for (; iterator.HasNext(); iterator.Next()) { |
| 3794 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 3795 | // unresolved exception types will be ignored by exception delivery |
| 3796 | if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3797 | mirror::Class* exception_type = linker->ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3798 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 3799 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 3800 | Thread::Current()->ClearException(); |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | handlers_ptr = iterator.EndDataPointer(); |
| 3805 | } |
| 3806 | } |
| 3807 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3808 | static void CheckProxyConstructor(mirror::ArtMethod* constructor); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3809 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 3810 | Handle<mirror::ArtMethod> prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3811 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 3812 | mirror::Class* ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, jstring name, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3813 | jobjectArray interfaces, jobject loader, |
| 3814 | jobjectArray methods, jobjectArray throws) { |
| 3815 | Thread* self = soa.Self(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3816 | StackHandleScope<8> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3817 | MutableHandle<mirror::Class> klass(hs.NewHandle( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3818 | AllocClass(self, GetClassRoot(kJavaLangClass), sizeof(mirror::Class)))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3819 | if (klass.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3820 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3821 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3822 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3823 | DCHECK(klass->GetClass() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3824 | klass->SetObjectSize(sizeof(mirror::Proxy)); |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 3825 | // Set the class access flags incl. preverified, so we do not try to set the flag on the methods. |
| 3826 | klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccPreverified); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3827 | klass->SetClassLoader(soa.Decode<mirror::ClassLoader*>(loader)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3828 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3829 | klass->SetName(soa.Decode<mirror::String*>(name)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3830 | mirror::Class* proxy_class = GetClassRoot(kJavaLangReflectProxy); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3831 | klass->SetDexCache(proxy_class->GetDexCache()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3832 | klass->SetStatus(mirror::Class::kStatusIdx, self); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3833 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3834 | // Instance fields are inherited, but we add a couple of static fields... |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3835 | { |
| 3836 | mirror::ObjectArray<mirror::ArtField>* sfields = AllocArtFieldArray(self, 2); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3837 | if (UNLIKELY(sfields == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3838 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3839 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3840 | } |
| 3841 | klass->SetSFields(sfields); |
| 3842 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3843 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 3844 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3845 | Handle<mirror::ArtField> interfaces_sfield(hs.NewHandle(AllocArtField(self))); |
| 3846 | if (UNLIKELY(interfaces_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3847 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3848 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3849 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3850 | klass->SetStaticField(0, interfaces_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3851 | interfaces_sfield->SetDexFieldIndex(0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3852 | interfaces_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3853 | interfaces_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
| 3854 | // 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] | 3855 | Handle<mirror::ArtField> throws_sfield(hs.NewHandle(AllocArtField(self))); |
| 3856 | if (UNLIKELY(throws_sfield.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3857 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3858 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3859 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3860 | klass->SetStaticField(1, throws_sfield.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3861 | throws_sfield->SetDexFieldIndex(1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3862 | throws_sfield->SetDeclaringClass(klass.Get()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3863 | throws_sfield->SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3864 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 3865 | // Proxies have 1 direct method, the constructor |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3866 | { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3867 | mirror::ObjectArray<mirror::ArtMethod>* directs = AllocArtMethodArray(self, 1); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3868 | if (UNLIKELY(directs == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3869 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3870 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3871 | } |
| 3872 | klass->SetDirectMethods(directs); |
| 3873 | mirror::ArtMethod* constructor = CreateProxyConstructor(self, klass, proxy_class); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3874 | if (UNLIKELY(constructor == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3875 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3876 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3877 | } |
| 3878 | klass->SetDirectMethod(0, constructor); |
| 3879 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3880 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3881 | // Create virtual method using specified prototypes. |
| 3882 | size_t num_virtual_methods = |
| 3883 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods)->GetLength(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3884 | { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3885 | mirror::ObjectArray<mirror::ArtMethod>* virtuals = AllocArtMethodArray(self, |
| 3886 | num_virtual_methods); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3887 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3888 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3889 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3890 | } |
| 3891 | klass->SetVirtualMethods(virtuals); |
| 3892 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3893 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3894 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3895 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3896 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3897 | Handle<mirror::ArtMethod> prototype(hs.NewHandle(decoded_methods->Get(i))); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3898 | mirror::ArtMethod* clone = CreateProxyMethod(self, klass, prototype); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3899 | if (UNLIKELY(clone == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3900 | CHECK(self->IsExceptionPending()); // OOME. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3901 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3902 | } |
| 3903 | klass->SetVirtualMethod(i, clone); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3904 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3905 | |
| 3906 | klass->SetSuperClass(proxy_class); // The super class is java.lang.reflect.Proxy |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3907 | klass->SetStatus(mirror::Class::kStatusLoaded, self); // Now effectively in the loaded state. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3908 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3909 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3910 | std::string descriptor(GetDescriptorForProxy(klass.Get())); |
| 3911 | mirror::Class* new_class = nullptr; |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3912 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3913 | // Must hold lock on object when resolved. |
| 3914 | ObjectLock<mirror::Class> resolution_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3915 | // Link the fields and virtual methods, creating vtable and iftables |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3916 | Handle<mirror::ObjectArray<mirror::Class> > h_interfaces( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3917 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces))); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3918 | if (!LinkClass(self, descriptor.c_str(), klass, h_interfaces, &new_class)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3919 | klass->SetStatus(mirror::Class::kStatusError, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3920 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3921 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3922 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3923 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3924 | CHECK(klass->IsRetired()); |
| 3925 | CHECK_NE(klass.Get(), new_class); |
| 3926 | klass.Assign(new_class); |
| 3927 | |
| 3928 | CHECK_EQ(interfaces_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3929 | interfaces_sfield->SetObject<false>(klass.Get(), |
| 3930 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3931 | CHECK_EQ(throws_sfield->GetDeclaringClass(), new_class); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3932 | throws_sfield->SetObject<false>(klass.Get(), |
| 3933 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class> >*>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3934 | |
| 3935 | { |
| 3936 | // Lock on klass is released. Lock new class object. |
| 3937 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3938 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 3939 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3940 | |
| 3941 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 3942 | if (kIsDebugBuild) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3943 | CHECK(klass->GetIFields() == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3944 | CheckProxyConstructor(klass->GetDirectMethod(0)); |
| 3945 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3946 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3947 | mirror::ObjectArray<mirror::ArtMethod>* decoded_methods = |
| 3948 | soa.Decode<mirror::ObjectArray<mirror::ArtMethod>*>(methods); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3949 | Handle<mirror::ArtMethod> prototype(hs.NewHandle(decoded_methods->Get(i))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 3950 | Handle<mirror::ArtMethod> virtual_method(hs.NewHandle(klass->GetVirtualMethod(i))); |
| 3951 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3952 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3953 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3954 | mirror::String* decoded_name = soa.Decode<mirror::String*>(name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3955 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3956 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3957 | CHECK_EQ(PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
| 3958 | |
| 3959 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3960 | decoded_name->ToModifiedUtf8().c_str())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 3961 | CHECK_EQ(PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3962 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3963 | CHECK_EQ(klass.Get()->GetInterfaces(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3964 | soa.Decode<mirror::ObjectArray<mirror::Class>*>(interfaces)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3965 | CHECK_EQ(klass.Get()->GetThrows(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 3966 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>*>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3967 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3968 | mirror::Class* existing = InsertClass(descriptor.c_str(), klass.Get(), Hash(descriptor.c_str())); |
Mathieu Chartier | 31b9d66 | 2013-10-14 11:53:12 -0700 | [diff] [blame] | 3969 | CHECK(existing == nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3970 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 3971 | } |
| 3972 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3973 | std::string ClassLinker::GetDescriptorForProxy(mirror::Class* proxy_class) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3974 | DCHECK(proxy_class->IsProxyClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 3975 | mirror::String* name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3976 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3977 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 3978 | } |
| 3979 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3980 | mirror::ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, |
| 3981 | mirror::ArtMethod* proxy_method) { |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3982 | DCHECK(proxy_class->IsProxyClass()); |
| 3983 | DCHECK(proxy_method->IsProxyMethod()); |
| 3984 | // Locate the dex cache of the original interface/Object |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3985 | mirror::DexCache* dex_cache = nullptr; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3986 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 3987 | ReaderMutexLock mu(Thread::Current(), dex_lock_); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3988 | for (size_t i = 0; i != dex_caches_.size(); ++i) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3989 | mirror::DexCache* a_dex_cache = GetDexCache(i); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3990 | if (proxy_method->HasSameDexCacheResolvedTypes(a_dex_cache->GetResolvedTypes())) { |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3991 | dex_cache = a_dex_cache; |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3992 | break; |
| 3993 | } |
| 3994 | } |
| 3995 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3996 | CHECK(dex_cache != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 3997 | uint32_t method_idx = proxy_method->GetDexMethodIndex(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 3998 | mirror::ArtMethod* resolved_method = dex_cache->GetResolvedMethod(method_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 3999 | CHECK(resolved_method != nullptr); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4000 | return resolved_method; |
| 4001 | } |
| 4002 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4003 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4004 | mirror::ArtMethod* ClassLinker::CreateProxyConstructor(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4005 | Handle<mirror::Class> klass, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4006 | mirror::Class* proxy_class) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4007 | // Create constructor for Proxy that must initialize h |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4008 | mirror::ObjectArray<mirror::ArtMethod>* proxy_direct_methods = |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4009 | proxy_class->GetDirectMethods(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4010 | CHECK_EQ(proxy_direct_methods->GetLength(), 16); |
| 4011 | mirror::ArtMethod* proxy_constructor = proxy_direct_methods->Get(2); |
Sebastien Hertz | ae94e35 | 2014-08-27 15:32:56 +0200 | [diff] [blame] | 4012 | // Ensure constructor is in dex cache so that we can use the dex cache to look up the overridden |
| 4013 | // constructor method. |
| 4014 | proxy_class->GetDexCache()->SetResolvedMethod(proxy_constructor->GetDexMethodIndex(), |
| 4015 | proxy_constructor); |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4016 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4017 | // code_ too) |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 4018 | mirror::ArtMethod* constructor = down_cast<mirror::ArtMethod*>(proxy_constructor->Clone(self)); |
| 4019 | if (constructor == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4020 | CHECK(self->IsExceptionPending()); // OOME. |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 4021 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4022 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4023 | // Make this constructor public and fix the class to be our Proxy version |
| 4024 | constructor->SetAccessFlags((constructor->GetAccessFlags() & ~kAccProtected) | kAccPublic); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4025 | constructor->SetDeclaringClass(klass.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4026 | return constructor; |
| 4027 | } |
| 4028 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4029 | static void CheckProxyConstructor(mirror::ArtMethod* constructor) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4030 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4031 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4032 | CHECK_STREQ(constructor->GetName(), "<init>"); |
| 4033 | CHECK_STREQ(constructor->GetSignature().ToString().c_str(), |
| 4034 | "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4035 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4036 | } |
| 4037 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4038 | mirror::ArtMethod* ClassLinker::CreateProxyMethod(Thread* self, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4039 | Handle<mirror::Class> klass, |
| 4040 | Handle<mirror::ArtMethod> prototype) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4041 | // Ensure prototype is in dex cache so that we can use the dex cache to look up the overridden |
| 4042 | // prototype method |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4043 | prototype->GetDeclaringClass()->GetDexCache()->SetResolvedMethod(prototype->GetDexMethodIndex(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4044 | prototype.Get()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4045 | // 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] | 4046 | // as necessary |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4047 | mirror::ArtMethod* method = down_cast<mirror::ArtMethod*>(prototype->Clone(self)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4048 | if (UNLIKELY(method == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4049 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4050 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4051 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4052 | |
| 4053 | // Set class to be the concrete proxy class and clear the abstract flag, modify exceptions to |
| 4054 | // the intersection of throw exceptions as defined in Proxy |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4055 | method->SetDeclaringClass(klass.Get()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4056 | method->SetAccessFlags((method->GetAccessFlags() & ~kAccAbstract) | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4057 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4058 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4059 | // related parameters from this method. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 4060 | method->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
| 4061 | method->SetEntryPointFromPortableCompiledCode(GetPortableProxyInvokeHandler()); |
Dragos Sbirlea | 08bf196 | 2013-08-12 08:53:04 -0700 | [diff] [blame] | 4062 | method->SetEntryPointFromInterpreter(artInterpreterToCompiledCodeBridge); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 4063 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4064 | return method; |
| 4065 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4066 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4067 | static void CheckProxyMethod(Handle<mirror::ArtMethod> method, |
| 4068 | Handle<mirror::ArtMethod> prototype) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4069 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4070 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4071 | CHECK(!prototype->IsFinal()); |
| 4072 | CHECK(method->IsFinal()); |
| 4073 | CHECK(!method->IsAbstract()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4074 | |
| 4075 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4076 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
| 4077 | CHECK_EQ(prototype->GetDexCacheStrings(), method->GetDexCacheStrings()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 4078 | CHECK(prototype->HasSameDexCacheResolvedMethods(method.Get())); |
| 4079 | CHECK(prototype->HasSameDexCacheResolvedTypes(method.Get())); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4080 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
| 4081 | |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 4082 | StackHandleScope<2> hs(Thread::Current()); |
| 4083 | MethodHelper mh(hs.NewHandle(method.Get())); |
| 4084 | MethodHelper mh2(hs.NewHandle(prototype.Get())); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4085 | CHECK_STREQ(method->GetName(), prototype->GetName()); |
| 4086 | CHECK_STREQ(method->GetShorty(), prototype->GetShorty()); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4087 | // More complex sanity - via dex cache |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4088 | CHECK_EQ(mh.GetReturnType(), mh2.GetReturnType()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4089 | } |
| 4090 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4091 | static bool CanWeInitializeClass(mirror::Class* klass, bool can_init_statics, |
| 4092 | bool can_init_parents) |
| 4093 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4094 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4095 | return true; |
| 4096 | } |
| 4097 | if (!can_init_statics) { |
| 4098 | // Check if there's a class initializer. |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4099 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4100 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4101 | return false; |
| 4102 | } |
| 4103 | // Check if there are encoded static values needing initialization. |
| 4104 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4105 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4106 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4107 | if (dex_class_def->static_values_off_ != 0) { |
| 4108 | return false; |
| 4109 | } |
| 4110 | } |
| 4111 | } |
| 4112 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4113 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4114 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4115 | return false; |
| 4116 | } else { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4117 | if (!CanWeInitializeClass(super_class, can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4118 | return false; |
| 4119 | } |
| 4120 | } |
| 4121 | } |
| 4122 | return true; |
| 4123 | } |
| 4124 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4125 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4126 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4127 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4128 | |
| 4129 | // Are we already initialized and therefore done? |
| 4130 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4131 | // an initialized class will never change its state. |
| 4132 | if (klass->IsInitialized()) { |
| 4133 | return true; |
| 4134 | } |
| 4135 | |
| 4136 | // 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] | 4137 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4138 | return false; |
| 4139 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4140 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4141 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4142 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4143 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4144 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4145 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4146 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4147 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4148 | return true; |
| 4149 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4150 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4151 | // 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] | 4152 | if (klass->IsErroneous()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4153 | ThrowEarlierClassFailure(klass.Get()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4154 | return false; |
| 4155 | } |
| 4156 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4157 | CHECK(klass->IsResolved()) << PrettyClass(klass.Get()) << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4158 | |
| 4159 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4160 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4161 | if (!klass->IsVerified()) { |
| 4162 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4163 | // compile time. |
| 4164 | if (klass->IsErroneous()) { |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4165 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4166 | } else { |
| 4167 | CHECK(Runtime::Current()->IsCompiler()); |
| 4168 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4169 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4170 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4171 | } else { |
| 4172 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4173 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4174 | } |
| 4175 | |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4176 | // If the class is kStatusInitializing, either this thread is |
| 4177 | // initializing higher up the stack or another thread has beat us |
| 4178 | // to initializing and we need to wait. Either way, this |
| 4179 | // invocation of InitializeClass will not be responsible for |
| 4180 | // running <clinit> and will return. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4181 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4182 | // Could have got an exception during verification. |
| 4183 | if (self->IsExceptionPending()) { |
| 4184 | return false; |
| 4185 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4186 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4187 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4188 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4189 | return true; |
| 4190 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4191 | // No. That's fine. Wait for another thread to finish initializing. |
| 4192 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | if (!ValidateSuperClassDescriptors(klass)) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4196 | klass->SetStatus(mirror::Class::kStatusError, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4197 | return false; |
| 4198 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4199 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4200 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4201 | CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusVerified) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4202 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4203 | // From here out other threads may observe that we're initializing and so changes of state |
| 4204 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4205 | klass->SetClinitThreadId(self->GetTid()); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4206 | klass->SetStatus(mirror::Class::kStatusInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4207 | |
| 4208 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4209 | } |
| 4210 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4211 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4212 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
| 4213 | mirror::Class* super_class = klass->GetSuperClass(); |
| 4214 | if (!super_class->IsInitialized()) { |
| 4215 | CHECK(!super_class->IsInterface()); |
| 4216 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4217 | StackHandleScope<1> hs(self); |
| 4218 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4219 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4220 | if (!super_initialized) { |
| 4221 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4222 | // the super class became erroneous due to initialization. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4223 | CHECK(handle_scope_super->IsErroneous() && self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4224 | << "Super class initialization failed for " |
| 4225 | << PrettyDescriptor(handle_scope_super.Get()) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4226 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4227 | << "\nPending exception:\n" |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4228 | << (self->GetException(nullptr) != nullptr ? self->GetException(nullptr)->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4229 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4230 | // Initialization failed because the super-class is erroneous. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4231 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4232 | return false; |
| 4233 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4234 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4235 | } |
| 4236 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4237 | if (klass->NumStaticFields() > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4238 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4239 | CHECK(dex_class_def != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4240 | const DexFile& dex_file = klass->GetDexFile(); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4241 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4242 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4243 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4244 | EncodedStaticFieldValueIterator value_it(dex_file, &dex_cache, &class_loader, |
| 4245 | this, *dex_class_def); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame^] | 4246 | const uint8_t* class_data = dex_file.GetClassData(*dex_class_def); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4247 | ClassDataItemIterator field_it(dex_file, class_data); |
| 4248 | if (value_it.HasNext()) { |
| 4249 | DCHECK(field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4250 | CHECK(can_init_statics); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4251 | for ( ; value_it.HasNext(); value_it.Next(), field_it.Next()) { |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4252 | StackHandleScope<1> hs(self); |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4253 | Handle<mirror::ArtField> field(hs.NewHandle( |
| 4254 | ResolveField(dex_file, field_it.GetMemberIndex(), dex_cache, class_loader, true))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4255 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4256 | value_it.ReadValueToField<true>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4257 | } else { |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4258 | value_it.ReadValueToField<false>(field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4259 | } |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4260 | DCHECK(!value_it.HasNext() || field_it.HasNextStaticField()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4264 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 4265 | mirror::ArtMethod* clinit = klass->FindClassInitializer(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4266 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4267 | CHECK(can_init_statics); |
Ian Rogers | 5d27faf | 2014-05-02 17:17:18 -0700 | [diff] [blame] | 4268 | JValue result; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4269 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4272 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4273 | uint64_t t1 = NanoTime(); |
| 4274 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4275 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4276 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4277 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4278 | |
| 4279 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 4280 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4281 | klass->SetStatus(mirror::Class::kStatusError, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4282 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4283 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4284 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4285 | RuntimeStats* thread_stats = self->GetStats(); |
| 4286 | ++global_stats->class_init_count; |
| 4287 | ++thread_stats->class_init_count; |
| 4288 | global_stats->class_init_time_ns += (t1 - t0); |
| 4289 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4290 | // Set the class as initialized except if failed to initialize static fields. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4291 | klass->SetStatus(mirror::Class::kStatusInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4292 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4293 | std::string temp; |
| 4294 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4295 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4296 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4297 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4298 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4299 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4300 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4301 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4302 | } |
| 4303 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4304 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, Thread* self, |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4305 | ObjectLock<mirror::Class>& lock) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 4306 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4307 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4308 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4309 | CHECK(!klass->IsInitialized()); |
Ian Rogers | 05f3057 | 2013-02-20 12:13:11 -0800 | [diff] [blame] | 4310 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4311 | |
| 4312 | // When we wake up, repeat the test for init-in-progress. If |
| 4313 | // there's an exception pending (only possible if |
| 4314 | // "interruptShouldThrow" was set), bail out. |
| 4315 | if (self->IsExceptionPending()) { |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 4316 | WrapExceptionInInitializer(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4317 | klass->SetStatus(mirror::Class::kStatusError, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4318 | return false; |
| 4319 | } |
| 4320 | // Spurious wakeup? Go back to waiting. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4321 | if (klass->GetStatus() == mirror::Class::kStatusInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4322 | continue; |
| 4323 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4324 | if (klass->GetStatus() == mirror::Class::kStatusVerified && Runtime::Current()->IsCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4325 | // Compile time initialization failed. |
| 4326 | return false; |
| 4327 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4328 | if (klass->IsErroneous()) { |
| 4329 | // The caller wants an exception, but it was thrown in a |
| 4330 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4331 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4332 | PrettyDescriptor(klass.Get()).c_str()); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4333 | return false; |
| 4334 | } |
| 4335 | if (klass->IsInitialized()) { |
| 4336 | return true; |
| 4337 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4338 | LOG(FATAL) << "Unexpected class status. " << PrettyClass(klass.Get()) << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4339 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4340 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 4341 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4344 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4345 | if (klass->IsInterface()) { |
| 4346 | return true; |
| 4347 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4348 | // Begin with the methods local to the superclass. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4349 | StackHandleScope<2> hs(Thread::Current()); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4350 | MutableMethodHelper mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4351 | MutableMethodHelper super_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4352 | if (klass->HasSuperClass() && |
| 4353 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4354 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
| 4355 | mh.ChangeMethod(klass->GetVTableEntry(i)); |
| 4356 | super_mh.ChangeMethod(klass->GetSuperClass()->GetVTableEntry(i)); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4357 | if (mh.GetMethod() != super_mh.GetMethod() && |
| 4358 | !mh.HasSameSignatureWithDifferentClassLoaders(&super_mh)) { |
| 4359 | ThrowLinkageError(klass.Get(), |
| 4360 | "Class %s method %s resolves differently in superclass %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4361 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4362 | PrettyMethod(mh.GetMethod()).c_str(), |
| 4363 | PrettyDescriptor(klass->GetSuperClass()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4364 | return false; |
| 4365 | } |
| 4366 | } |
| 4367 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4368 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4369 | if (klass->GetClassLoader() != klass->GetIfTable()->GetInterface(i)->GetClassLoader()) { |
| 4370 | uint32_t num_methods = klass->GetIfTable()->GetInterface(i)->NumVirtualMethods(); |
| 4371 | for (uint32_t j = 0; j < num_methods; ++j) { |
| 4372 | mh.ChangeMethod(klass->GetIfTable()->GetMethodArray(i)->GetWithoutChecks(j)); |
| 4373 | super_mh.ChangeMethod(klass->GetIfTable()->GetInterface(i)->GetVirtualMethod(j)); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4374 | if (mh.GetMethod() != super_mh.GetMethod() && |
| 4375 | !mh.HasSameSignatureWithDifferentClassLoaders(&super_mh)) { |
| 4376 | ThrowLinkageError(klass.Get(), |
| 4377 | "Class %s method %s resolves differently in interface %s", |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4378 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 4379 | PrettyMethod(mh.GetMethod()).c_str(), |
| 4380 | PrettyDescriptor(klass->GetIfTable()->GetInterface(i)).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4381 | return false; |
| 4382 | } |
| 4383 | } |
| 4384 | } |
| 4385 | } |
| 4386 | return true; |
| 4387 | } |
| 4388 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4389 | bool ClassLinker::EnsureInitialized(Thread* self, Handle<mirror::Class> c, bool can_init_fields, |
| 4390 | bool can_init_parents) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 4391 | DCHECK(c.Get() != nullptr); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4392 | if (c->IsInitialized()) { |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4393 | EnsurePreverifiedMethods(c); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4394 | return true; |
| 4395 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4396 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4397 | if (!success) { |
| 4398 | if (can_init_fields && can_init_parents) { |
| 4399 | CHECK(self->IsExceptionPending()) << PrettyClass(c.Get()); |
| 4400 | } |
| 4401 | } else { |
| 4402 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 4403 | } |
| 4404 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4407 | void ClassLinker::FixupTemporaryDeclaringClass(mirror::Class* temp_class, mirror::Class* new_class) { |
| 4408 | mirror::ObjectArray<mirror::ArtField>* fields = new_class->GetIFields(); |
| 4409 | if (fields != nullptr) { |
| 4410 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4411 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4412 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4413 | } |
| 4414 | } |
| 4415 | } |
| 4416 | |
| 4417 | fields = new_class->GetSFields(); |
| 4418 | if (fields != nullptr) { |
| 4419 | for (int index = 0; index < fields->GetLength(); index ++) { |
| 4420 | if (fields->Get(index)->GetDeclaringClass() == temp_class) { |
| 4421 | fields->Get(index)->SetDeclaringClass(new_class); |
| 4422 | } |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | mirror::ObjectArray<mirror::ArtMethod>* methods = new_class->GetDirectMethods(); |
| 4427 | if (methods != nullptr) { |
| 4428 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4429 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4430 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4431 | } |
| 4432 | } |
| 4433 | } |
| 4434 | |
| 4435 | methods = new_class->GetVirtualMethods(); |
| 4436 | if (methods != nullptr) { |
| 4437 | for (int index = 0; index < methods->GetLength(); index ++) { |
| 4438 | if (methods->Get(index)->GetDeclaringClass() == temp_class) { |
| 4439 | methods->Get(index)->SetDeclaringClass(new_class); |
| 4440 | } |
| 4441 | } |
| 4442 | } |
| 4443 | } |
| 4444 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4445 | bool ClassLinker::LinkClass(Thread* self, const char* descriptor, Handle<mirror::Class> klass, |
| 4446 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4447 | mirror::Class** new_class) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4448 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4449 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4450 | if (!LinkSuperClass(klass)) { |
| 4451 | return false; |
| 4452 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4453 | if (!LinkMethods(self, klass, interfaces)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4454 | return false; |
| 4455 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4456 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4457 | return false; |
| 4458 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4459 | size_t class_size; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4460 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 4461 | return false; |
| 4462 | } |
| 4463 | CreateReferenceInstanceOffsets(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4464 | CHECK_EQ(mirror::Class::kStatusLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4465 | |
| 4466 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 4467 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 4468 | // correct size during class linker initialization. |
| 4469 | CHECK_EQ(klass->GetClassSize(), class_size) << PrettyDescriptor(klass.Get()); |
| 4470 | |
| 4471 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
| 4472 | klass->PopulateEmbeddedImtAndVTable(); |
| 4473 | } |
| 4474 | |
| 4475 | // This will notify waiters on klass that saw the not yet resolved |
| 4476 | // class in the class_table_ during EnsureResolved. |
| 4477 | klass->SetStatus(mirror::Class::kStatusResolved, self); |
| 4478 | *new_class = klass.Get(); |
| 4479 | } else { |
| 4480 | CHECK(!klass->IsResolved()); |
| 4481 | // Retire the temporary class and create the correctly sized resolved class. |
| 4482 | *new_class = klass->CopyOf(self, class_size); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4483 | if (UNLIKELY(*new_class == nullptr)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4484 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
| 4485 | klass->SetStatus(mirror::Class::kStatusError, self); |
| 4486 | return false; |
| 4487 | } |
| 4488 | |
| 4489 | CHECK_EQ((*new_class)->GetClassSize(), class_size); |
| 4490 | StackHandleScope<1> hs(self); |
| 4491 | auto new_class_h = hs.NewHandleWrapper<mirror::Class>(new_class); |
| 4492 | ObjectLock<mirror::Class> lock(self, new_class_h); |
| 4493 | |
| 4494 | FixupTemporaryDeclaringClass(klass.Get(), new_class_h.Get()); |
| 4495 | |
| 4496 | mirror::Class* existing = UpdateClass(descriptor, new_class_h.Get(), Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4497 | CHECK(existing == nullptr || existing == klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4498 | |
| 4499 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 4500 | // class_table_ during EnsureResolved. |
| 4501 | klass->SetStatus(mirror::Class::kStatusRetired, self); |
| 4502 | |
| 4503 | CHECK_EQ(new_class_h->GetStatus(), mirror::Class::kStatusResolving); |
| 4504 | // This will notify waiters on new_class that saw the not yet resolved |
| 4505 | // class in the class_table_ during EnsureResolved. |
| 4506 | new_class_h->SetStatus(mirror::Class::kStatusResolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4507 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4508 | return true; |
| 4509 | } |
| 4510 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4511 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4512 | CHECK_EQ(mirror::Class::kStatusIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4513 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 4514 | uint16_t super_class_idx = class_def.superclass_idx_; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4515 | if (super_class_idx != DexFile::kDexNoIndex16) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4516 | mirror::Class* super_class = ResolveType(dex_file, super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4517 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 4518 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4519 | return false; |
| 4520 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4521 | // Verify |
| 4522 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4523 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4524 | PrettyDescriptor(super_class).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4525 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 4526 | return false; |
| 4527 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4528 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4529 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4530 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4531 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4532 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4533 | for (size_t i = 0; i < interfaces->Size(); i++) { |
| 4534 | uint16_t idx = interfaces->GetTypeItem(i).type_idx_; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4535 | mirror::Class* interface = ResolveType(dex_file, idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4536 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4537 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4538 | return false; |
| 4539 | } |
| 4540 | // Verify |
| 4541 | if (!klass->CanAccess(interface)) { |
| 4542 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4543 | ThrowIllegalAccessError(klass.Get(), "Interface %s implemented by class %s is inaccessible", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4544 | PrettyDescriptor(interface).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4545 | PrettyDescriptor(klass.Get()).c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4546 | return false; |
| 4547 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4548 | } |
| 4549 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4550 | // Mark the class as loaded. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4551 | klass->SetStatus(mirror::Class::kStatusLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4552 | return true; |
| 4553 | } |
| 4554 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4555 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4556 | CHECK(!klass->IsPrimitive()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4557 | mirror::Class* super = klass->GetSuperClass(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4558 | if (klass.Get() == GetClassRoot(kJavaLangObject)) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4559 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4560 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4561 | return false; |
| 4562 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4563 | return true; |
| 4564 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4565 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4566 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
| 4567 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4568 | return false; |
| 4569 | } |
| 4570 | // Verify |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 4571 | if (super->IsFinal() || super->IsInterface()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4572 | ThrowIncompatibleClassChangeError(klass.Get(), "Superclass %s of %s is %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4573 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4574 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4575 | super->IsFinal() ? "declared final" : "an interface"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4576 | return false; |
| 4577 | } |
| 4578 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4579 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4580 | PrettyDescriptor(super).c_str(), |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4581 | PrettyDescriptor(klass.Get()).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4582 | return false; |
| 4583 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4584 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4585 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 4586 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 4587 | if (super->IsFinalizable()) { |
| 4588 | klass->SetFinalizable(); |
| 4589 | } |
| 4590 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4591 | // Inherit reference flags (if any) from the superclass. |
| 4592 | int reference_flags = (super->GetAccessFlags() & kAccReferenceFlagsMask); |
| 4593 | if (reference_flags != 0) { |
| 4594 | klass->SetAccessFlags(klass->GetAccessFlags() | reference_flags); |
| 4595 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4596 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Elliott Hughes | bf61ba3 | 2011-10-11 10:53:09 -0700 | [diff] [blame] | 4597 | if (init_done_ && super == GetClassRoot(kJavaLangRefReference)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4598 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4599 | "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] | 4600 | PrettyDescriptor(klass.Get()).c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 4601 | return false; |
| 4602 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 4603 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4604 | if (kIsDebugBuild) { |
| 4605 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4606 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4607 | CHECK(super->IsResolved()); |
| 4608 | super = super->GetSuperClass(); |
| 4609 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4610 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4611 | return true; |
| 4612 | } |
| 4613 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4614 | // Populate the class vtable and itable. Compute return type indices. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4615 | bool ClassLinker::LinkMethods(Thread* self, Handle<mirror::Class> klass, |
| 4616 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4617 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4618 | if (klass->IsInterface()) { |
| 4619 | // No vtable. |
| 4620 | size_t count = klass->NumVirtualMethods(); |
| 4621 | if (!IsUint(16, count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4622 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zd", count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4623 | return false; |
| 4624 | } |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4625 | for (size_t i = 0; i < count; ++i) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4626 | klass->GetVirtualMethodDuringLinking(i)->SetMethodIndex(i); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4627 | } |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 4628 | // Link interface method tables |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4629 | return LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4630 | } else { |
Elliott Hughes | bc258fa | 2011-10-06 14:45:21 -0700 | [diff] [blame] | 4631 | // Link virtual and interface method tables |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4632 | return LinkVirtualMethods(self, klass) && LinkInterfaceMethods(klass, interfaces); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4633 | } |
| 4634 | return true; |
| 4635 | } |
| 4636 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4637 | bool ClassLinker::LinkVirtualMethods(Thread* self, Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4638 | if (klass->HasSuperClass()) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4639 | uint32_t max_count = klass->NumVirtualMethods() + |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4640 | klass->GetSuperClass()->GetVTableLength(); |
| 4641 | size_t actual_count = klass->GetSuperClass()->GetVTableLength(); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4642 | CHECK_LE(actual_count, max_count); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4643 | StackHandleScope<4> hs(self); |
| 4644 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4645 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable; |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4646 | if (super_class->ShouldHaveEmbeddedImtAndVTable()) { |
| 4647 | vtable = hs.NewHandle(AllocArtMethodArray(self, max_count)); |
| 4648 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4649 | CHECK(self->IsExceptionPending()); // OOME. |
| 4650 | return false; |
| 4651 | } |
| 4652 | int len = super_class->GetVTableLength(); |
Mingyao Yang | 1a12858 | 2014-07-22 17:33:25 -0700 | [diff] [blame] | 4653 | for (int i = 0; i < len; i++) { |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4654 | vtable->Set<false>(i, super_class->GetVTableEntry(i)); |
| 4655 | } |
| 4656 | } else { |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 4657 | CHECK(super_class->GetVTable() != nullptr) << PrettyClass(super_class.Get()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4658 | vtable = hs.NewHandle(super_class->GetVTable()->CopyOf(self, max_count)); |
| 4659 | if (UNLIKELY(vtable.Get() == nullptr)) { |
| 4660 | CHECK(self->IsExceptionPending()); // OOME. |
| 4661 | return false; |
| 4662 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4663 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 4664 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4665 | // See if any of our virtual methods override the superclass. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4666 | MutableMethodHelper local_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4667 | MutableMethodHelper super_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4668 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4669 | mirror::ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4670 | local_mh.ChangeMethod(local_method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4671 | size_t j = 0; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4672 | for (; j < actual_count; ++j) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4673 | mirror::ArtMethod* super_method = vtable->Get(j); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4674 | super_mh.ChangeMethod(super_method); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4675 | if (local_mh.HasSameNameAndSignature(&super_mh)) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4676 | if (klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 4677 | super_method->GetAccessFlags())) { |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4678 | if (super_method->IsFinal()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4679 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4680 | PrettyMethod(local_method).c_str(), |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4681 | super_method->GetDeclaringClassDescriptor()); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4682 | return false; |
| 4683 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4684 | vtable->Set<false>(j, local_method); |
Elliott Hughes | 3971737 | 2012-07-13 16:21:23 -0700 | [diff] [blame] | 4685 | local_method->SetMethodIndex(j); |
| 4686 | break; |
| 4687 | } else { |
| 4688 | LOG(WARNING) << "Before Android 4.1, method " << PrettyMethod(local_method) |
| 4689 | << " would have incorrectly overridden the package-private method in " |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4690 | << PrettyDescriptor(super_method->GetDeclaringClassDescriptor()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4691 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4692 | } |
| 4693 | } |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 4694 | if (j == actual_count) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4695 | // Not overriding, append. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4696 | vtable->Set<false>(actual_count, local_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4697 | local_method->SetMethodIndex(actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4698 | actual_count += 1; |
| 4699 | } |
| 4700 | } |
| 4701 | if (!IsUint(16, actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4702 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4703 | return false; |
| 4704 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4705 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4706 | CHECK_LE(actual_count, max_count); |
| 4707 | if (actual_count < max_count) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4708 | vtable.Assign(vtable->CopyOf(self, actual_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4709 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4710 | CHECK(self->IsExceptionPending()); // OOME. |
| 4711 | return false; |
| 4712 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4713 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4714 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4715 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4716 | CHECK_EQ(klass.Get(), GetClassRoot(kJavaLangObject)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4717 | uint32_t num_virtual_methods = klass->NumVirtualMethods(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4718 | if (!IsUint(16, num_virtual_methods)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4719 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", num_virtual_methods); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4720 | return false; |
| 4721 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4722 | StackHandleScope<1> hs(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4723 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4724 | vtable(hs.NewHandle(AllocArtMethodArray(self, num_virtual_methods))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4725 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4726 | CHECK(self->IsExceptionPending()); // OOME. |
| 4727 | return false; |
| 4728 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 4729 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4730 | mirror::ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4731 | vtable->Set<false>(i, virtual_method); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4732 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4733 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4734 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4735 | } |
| 4736 | return true; |
| 4737 | } |
| 4738 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4739 | bool ClassLinker::LinkInterfaceMethods(Handle<mirror::Class> klass, |
| 4740 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4741 | Thread* const self = Thread::Current(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4742 | Runtime* const runtime = Runtime::Current(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4743 | // Set the imt table to be all conflicts by default. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4744 | klass->SetImTable(runtime->GetDefaultImt()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4745 | size_t super_ifcount; |
| 4746 | if (klass->HasSuperClass()) { |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4747 | super_ifcount = klass->GetSuperClass()->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4748 | } else { |
| 4749 | super_ifcount = 0; |
| 4750 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4751 | uint32_t num_interfaces = |
| 4752 | interfaces.Get() == nullptr ? klass->NumDirectInterfaces() : interfaces->GetLength(); |
| 4753 | size_t ifcount = super_ifcount + num_interfaces; |
| 4754 | for (size_t i = 0; i < num_interfaces; i++) { |
| 4755 | mirror::Class* interface = |
| 4756 | interfaces.Get() == nullptr ? mirror::Class::GetDirectInterface(self, klass, i) : |
| 4757 | interfaces->Get(i); |
| 4758 | ifcount += interface->GetIfTableCount(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4759 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 4760 | if (ifcount == 0) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4761 | // Class implements no interfaces. |
| 4762 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4763 | DCHECK(klass->GetIfTable() == nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4764 | return true; |
| 4765 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4766 | if (ifcount == super_ifcount) { |
| 4767 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 4768 | bool has_non_marker_interface = false; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4769 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4770 | for (size_t i = 0; i < ifcount; ++i) { |
| 4771 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 4772 | has_non_marker_interface = true; |
| 4773 | break; |
| 4774 | } |
| 4775 | } |
| 4776 | if (!has_non_marker_interface) { |
| 4777 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
| 4778 | klass->SetIfTable(super_iftable); |
| 4779 | return true; |
| 4780 | } |
| 4781 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4782 | StackHandleScope<5> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4783 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4784 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4785 | CHECK(self->IsExceptionPending()); // OOME. |
| 4786 | return false; |
| 4787 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4788 | if (super_ifcount != 0) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4789 | mirror::IfTable* super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4790 | for (size_t i = 0; i < super_ifcount; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4791 | mirror::Class* super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4792 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4793 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4794 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4795 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4796 | // Flatten the interface inheritance hierarchy. |
| 4797 | size_t idx = super_ifcount; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4798 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4799 | mirror::Class* interface = |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4800 | interfaces.Get() == nullptr ? mirror::Class::GetDirectInterface(self, klass, i) : |
| 4801 | interfaces->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4802 | DCHECK(interface != nullptr); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4803 | if (!interface->IsInterface()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4804 | std::string temp; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4805 | ThrowIncompatibleClassChangeError(klass.Get(), "Class %s implements non-interface class %s", |
| 4806 | PrettyDescriptor(klass.Get()).c_str(), |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4807 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4808 | return false; |
| 4809 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4810 | // Check if interface is already in iftable |
| 4811 | bool duplicate = false; |
| 4812 | for (size_t j = 0; j < idx; j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4813 | mirror::Class* existing_interface = iftable->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4814 | if (existing_interface == interface) { |
| 4815 | duplicate = true; |
| 4816 | break; |
| 4817 | } |
| 4818 | } |
| 4819 | if (!duplicate) { |
| 4820 | // Add this non-duplicate interface. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4821 | iftable->SetInterface(idx++, interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4822 | // Add this interface's non-duplicate super-interfaces. |
| 4823 | for (int32_t j = 0; j < interface->GetIfTableCount(); j++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4824 | mirror::Class* super_interface = interface->GetIfTable()->GetInterface(j); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4825 | bool super_duplicate = false; |
| 4826 | for (size_t k = 0; k < idx; k++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 4827 | mirror::Class* existing_interface = iftable->GetInterface(k); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4828 | if (existing_interface == super_interface) { |
| 4829 | super_duplicate = true; |
| 4830 | break; |
| 4831 | } |
| 4832 | } |
| 4833 | if (!super_duplicate) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4834 | iftable->SetInterface(idx++, super_interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4835 | } |
| 4836 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4837 | } |
| 4838 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4839 | self->AllowThreadSuspension(); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4840 | // Shrink iftable in case duplicates were found |
| 4841 | if (idx < ifcount) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4842 | 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] | 4843 | if (UNLIKELY(iftable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4844 | CHECK(self->IsExceptionPending()); // OOME. |
| 4845 | return false; |
| 4846 | } |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 4847 | ifcount = idx; |
| 4848 | } else { |
| 4849 | CHECK_EQ(idx, ifcount); |
| 4850 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4851 | klass->SetIfTable(iftable.Get()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4852 | |
| 4853 | // 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] | 4854 | if (klass->IsInterface()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4855 | return true; |
| 4856 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4857 | self->AllowThreadSuspension(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4858 | // Allocate imtable |
| 4859 | bool imtable_changed = false; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4860 | Handle<mirror::ObjectArray<mirror::ArtMethod>> imtable( |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4861 | hs.NewHandle(AllocArtMethodArray(self, mirror::Class::kImtSize))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4862 | if (UNLIKELY(imtable.Get() == nullptr)) { |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4863 | CHECK(self->IsExceptionPending()); // OOME. |
| 4864 | return false; |
| 4865 | } |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4866 | MutableMethodHelper interface_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
| 4867 | MutableMethodHelper vtable_mh(hs.NewHandle<mirror::ArtMethod>(nullptr)); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4868 | size_t max_miranda_methods = 0; // The max size of miranda_list. |
| 4869 | for (size_t i = 0; i < ifcount; ++i) { |
| 4870 | max_miranda_methods += iftable->GetInterface(i)->NumVirtualMethods(); |
| 4871 | } |
| 4872 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
| 4873 | miranda_list(hs.NewHandle(AllocArtMethodArray(self, max_miranda_methods))); |
| 4874 | size_t miranda_list_size = 0; // The current size of miranda_list. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 4875 | for (size_t i = 0; i < ifcount; ++i) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4876 | self->AllowThreadSuspension(); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4877 | size_t num_methods = iftable->GetInterface(i)->NumVirtualMethods(); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4878 | if (num_methods > 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4879 | StackHandleScope<2> hs(self); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4880 | Handle<mirror::ObjectArray<mirror::ArtMethod>> |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4881 | method_array(hs.NewHandle(AllocArtMethodArray(self, num_methods))); |
| 4882 | if (UNLIKELY(method_array.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4883 | CHECK(self->IsExceptionPending()); // OOME. |
| 4884 | return false; |
| 4885 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4886 | iftable->SetMethodArray(i, method_array.Get()); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 4887 | Handle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4888 | hs.NewHandle(klass->GetVTableDuringLinking())); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4889 | for (size_t j = 0; j < num_methods; ++j) { |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4890 | interface_mh.ChangeMethod(iftable->GetInterface(i)->GetVirtualMethod(j)); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4891 | int32_t k; |
| 4892 | // For each method listed in the interface's method list, find the |
| 4893 | // matching method in our class's method list. We want to favor the |
| 4894 | // subclass over the superclass, which just requires walking |
| 4895 | // back from the end of the vtable. (This only matters if the |
| 4896 | // superclass defines a private method and this class redefines |
| 4897 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 4898 | // those don't end up in the virtual method table, so it shouldn't |
| 4899 | // matter which direction we go. We walk it backward anyway.) |
| 4900 | for (k = vtable->GetLength() - 1; k >= 0; --k) { |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4901 | vtable_mh.ChangeMethod(vtable->Get(k)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4902 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4903 | if (!vtable_mh.Get()->IsAbstract() && !vtable_mh.Get()->IsPublic()) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4904 | ThrowIllegalAccessError( |
| 4905 | klass.Get(), |
| 4906 | "Method '%s' implementing interface method '%s' is not public", |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4907 | PrettyMethod(vtable_mh.Get()).c_str(), |
| 4908 | PrettyMethod(interface_mh.Get()).c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4909 | return false; |
| 4910 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4911 | method_array->Set<false>(j, vtable_mh.Get()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4912 | // Place method in imt if entry is empty, place conflict otherwise. |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4913 | uint32_t imt_index = interface_mh.Get()->GetDexMethodIndex() % mirror::Class::kImtSize; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4914 | if (imtable->Get(imt_index) == nullptr) { |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4915 | imtable->Set<false>(imt_index, vtable_mh.Get()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4916 | imtable_changed = true; |
| 4917 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4918 | imtable->Set<false>(imt_index, runtime->GetImtConflictMethod()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4919 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4920 | break; |
| 4921 | } |
| 4922 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4923 | if (k < 0) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4924 | StackHandleScope<1> hs(self); |
| 4925 | auto miranda_method = hs.NewHandle<mirror::ArtMethod>(nullptr); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4926 | for (size_t l = 0; l < miranda_list_size; ++l) { |
| 4927 | mirror::ArtMethod* mir_method = miranda_list->Get(l); |
| 4928 | DCHECK(mir_method != nullptr); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4929 | vtable_mh.ChangeMethod(mir_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4930 | if (interface_mh.HasSameNameAndSignature(&vtable_mh)) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4931 | miranda_method.Assign(mir_method); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4932 | break; |
| 4933 | } |
| 4934 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4935 | if (miranda_method.Get() == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4936 | // Point the interface table at a phantom slot. |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4937 | miranda_method.Assign(down_cast<mirror::ArtMethod*>(interface_mh.Get()->Clone(self))); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4938 | if (UNLIKELY(miranda_method.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4939 | CHECK(self->IsExceptionPending()); // OOME. |
| 4940 | return false; |
| 4941 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4942 | DCHECK_LT(miranda_list_size, max_miranda_methods); |
| 4943 | miranda_list->Set<false>(miranda_list_size++, miranda_method.Get()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 4944 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4945 | method_array->Set<false>(j, miranda_method.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4946 | } |
| 4947 | } |
| 4948 | } |
| 4949 | } |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4950 | if (imtable_changed) { |
| 4951 | // Fill in empty entries in interface method table with conflict. |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4952 | mirror::ArtMethod* imt_conflict_method = runtime->GetImtConflictMethod(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4953 | for (size_t i = 0; i < mirror::Class::kImtSize; i++) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4954 | if (imtable->Get(i) == nullptr) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4955 | imtable->Set<false>(i, imt_conflict_method); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4956 | } |
| 4957 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4958 | klass->SetImTable(imtable.Get()); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 4959 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4960 | if (miranda_list_size > 0) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 4961 | int old_method_count = klass->NumVirtualMethods(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4962 | int new_method_count = old_method_count + miranda_list_size; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4963 | mirror::ObjectArray<mirror::ArtMethod>* virtuals; |
| 4964 | if (old_method_count == 0) { |
| 4965 | virtuals = AllocArtMethodArray(self, new_method_count); |
| 4966 | } else { |
| 4967 | virtuals = klass->GetVirtualMethods()->CopyOf(self, new_method_count); |
| 4968 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4969 | if (UNLIKELY(virtuals == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4970 | CHECK(self->IsExceptionPending()); // OOME. |
| 4971 | return false; |
| 4972 | } |
| 4973 | klass->SetVirtualMethods(virtuals); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4974 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4975 | StackHandleScope<1> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4976 | MutableHandle<mirror::ObjectArray<mirror::ArtMethod>> vtable( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4977 | hs.NewHandle(klass->GetVTableDuringLinking())); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4978 | CHECK(vtable.Get() != nullptr); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4979 | int old_vtable_count = vtable->GetLength(); |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4980 | int new_vtable_count = old_vtable_count + miranda_list_size; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4981 | vtable.Assign(vtable->CopyOf(self, new_vtable_count)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4982 | if (UNLIKELY(vtable.Get() == nullptr)) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4983 | CHECK(self->IsExceptionPending()); // OOME. |
| 4984 | return false; |
| 4985 | } |
Hiroshi Yamauchi | 41369d2 | 2014-08-19 13:10:36 -0700 | [diff] [blame] | 4986 | for (size_t i = 0; i < miranda_list_size; ++i) { |
| 4987 | mirror::ArtMethod* method = miranda_list->Get(i); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 4988 | // Leave the declaring class alone as type indices are relative to it |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 4989 | method->SetAccessFlags(method->GetAccessFlags() | kAccMiranda); |
| 4990 | method->SetMethodIndex(0xFFFF & (old_vtable_count + i)); |
| 4991 | klass->SetVirtualMethod(old_method_count + i, method); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4992 | vtable->Set<false>(old_vtable_count + i, method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4993 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4994 | // 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] | 4995 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4996 | } |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4997 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 4998 | mirror::ObjectArray<mirror::ArtMethod>* vtable = klass->GetVTableDuringLinking(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 4999 | for (int i = 0; i < vtable->GetLength(); ++i) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5000 | CHECK(vtable->Get(i) != nullptr); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5001 | } |
| 5002 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5003 | self->AllowThreadSuspension(); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 5004 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5005 | return true; |
| 5006 | } |
| 5007 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5008 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5009 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5010 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5013 | 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] | 5014 | CHECK(klass.Get() != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5015 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5016 | } |
| 5017 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5018 | struct LinkFieldsComparator { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5019 | explicit LinkFieldsComparator() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 5020 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5021 | // 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] | 5022 | bool operator()(mirror::ArtField* field1, mirror::ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5023 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5024 | // 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] | 5025 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 5026 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5027 | if (type1 != type2) { |
| 5028 | bool is_primitive1 = type1 != Primitive::kPrimNot; |
| 5029 | bool is_primitive2 = type2 != Primitive::kPrimNot; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5030 | if (type1 != type2) { |
| 5031 | if (is_primitive1 && is_primitive2) { |
| 5032 | // Larger primitive types go first. |
| 5033 | return Primitive::ComponentSize(type1) > Primitive::ComponentSize(type2); |
| 5034 | } else { |
| 5035 | // Reference always goes first. |
| 5036 | return !is_primitive1; |
| 5037 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5038 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5039 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5040 | // same basic group? then sort by string. |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5041 | return strcmp(field1->GetName(), field2->GetName()) < 0; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5042 | } |
| 5043 | }; |
| 5044 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5045 | bool ClassLinker::LinkFields(Thread* self, Handle<mirror::Class> klass, bool is_static, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5046 | size_t* class_size) { |
| 5047 | self->AllowThreadSuspension(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5048 | size_t num_fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5049 | is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5050 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5051 | mirror::ObjectArray<mirror::ArtField>* fields = |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5052 | is_static ? klass->GetSFields() : klass->GetIFields(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5053 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5054 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 5055 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5056 | if (is_static) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5057 | uint32_t base = sizeof(mirror::Class); // Static fields come after the class. |
| 5058 | if (klass->ShouldHaveEmbeddedImtAndVTable()) { |
| 5059 | // Static fields come after the embedded tables. |
| 5060 | base = mirror::Class::ComputeClassSize(true, klass->GetVTableDuringLinking()->GetLength(), |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5061 | 0, 0, 0, 0, 0); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5062 | } |
| 5063 | field_offset = MemberOffset(base); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5064 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5065 | mirror::Class* super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5066 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5067 | CHECK(super_class->IsResolved()) |
| 5068 | << PrettyClass(klass.Get()) << " " << PrettyClass(super_class); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5069 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5070 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5071 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5072 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5073 | CHECK_EQ(num_fields == 0, fields == nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5074 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5075 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5076 | // minimizes disruption of C++ version such as Class and Method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5077 | std::deque<mirror::ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 5078 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5079 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5080 | for (size_t i = 0; i < num_fields; i++) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5081 | mirror::ArtField* f = fields->Get(i); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5082 | CHECK(f != nullptr) << PrettyClass(klass.Get()); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5083 | grouped_and_sorted_fields.push_back(f); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5084 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5085 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 5086 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5087 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5088 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5089 | size_t current_field = 0; |
| 5090 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5091 | FieldGaps gaps; |
| 5092 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5093 | for (; current_field < num_fields; current_field++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5094 | mirror::ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5095 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 5096 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5097 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 5098 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5099 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5100 | if (UNLIKELY(!IsAligned<4>(field_offset.Uint32Value()))) { |
| 5101 | MemberOffset old_offset = field_offset; |
| 5102 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 5103 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 5104 | } |
| 5105 | DCHECK(IsAligned<4>(field_offset.Uint32Value())); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5106 | grouped_and_sorted_fields.pop_front(); |
| 5107 | num_reference_fields++; |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5108 | fields->Set<false>(current_field, field); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5109 | field->SetOffset(field_offset); |
| 5110 | field_offset = MemberOffset(field_offset.Uint32Value() + sizeof(uint32_t)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5111 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 5112 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 5113 | // otherwise we could end up with suboptimal gap fills. |
| 5114 | ShuffleForward<8>(num_fields, ¤t_field, &field_offset, |
| 5115 | fields, &grouped_and_sorted_fields, &gaps); |
| 5116 | ShuffleForward<4>(num_fields, ¤t_field, &field_offset, |
| 5117 | fields, &grouped_and_sorted_fields, &gaps); |
| 5118 | ShuffleForward<2>(num_fields, ¤t_field, &field_offset, |
| 5119 | fields, &grouped_and_sorted_fields, &gaps); |
| 5120 | ShuffleForward<1>(num_fields, ¤t_field, &field_offset, |
| 5121 | fields, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 5122 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 5123 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5124 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5125 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5126 | // 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] | 5127 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5128 | // We know there are no non-reference fields in the Reference classes, and we know |
| 5129 | // that 'referent' is alphabetically last, so this is easy... |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5130 | CHECK_EQ(num_reference_fields, num_fields) << PrettyClass(klass.Get()); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5131 | CHECK_STREQ(fields->Get(num_fields - 1)->GetName(), "referent") << PrettyClass(klass.Get()); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 5132 | --num_reference_fields; |
| 5133 | } |
| 5134 | |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5135 | if (kIsDebugBuild) { |
| 5136 | // Make sure that all reference fields appear before |
| 5137 | // non-reference fields, and all double-wide fields are aligned. |
| 5138 | bool seen_non_ref = false; |
| 5139 | for (size_t i = 0; i < num_fields; i++) { |
| 5140 | mirror::ArtField* field = fields->Get(i); |
| 5141 | if (false) { // enable to debug field layout |
| 5142 | LOG(INFO) << "LinkFields: " << (is_static ? "static" : "instance") |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5143 | << " class=" << PrettyClass(klass.Get()) |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5144 | << " field=" << PrettyField(field) |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 5145 | << " offset=" |
| 5146 | << field->GetField32(MemberOffset(mirror::ArtField::OffsetOffset())); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5147 | } |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5148 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5149 | bool is_primitive = type != Primitive::kPrimNot; |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5150 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 5151 | strcmp("referent", field->GetName()) == 0) { |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5152 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 5153 | } |
| 5154 | if (is_primitive) { |
| 5155 | if (!seen_non_ref) { |
| 5156 | seen_non_ref = true; |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5157 | DCHECK_EQ(num_reference_fields, i) << PrettyField(field); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5158 | } |
| 5159 | } else { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5160 | DCHECK(!seen_non_ref) << PrettyField(field); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | if (!seen_non_ref) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5164 | DCHECK_EQ(num_fields, num_reference_fields) << PrettyClass(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5165 | } |
| 5166 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5167 | |
| 5168 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5169 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5170 | if (is_static) { |
| 5171 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5172 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 5173 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5174 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 5175 | if (!klass->IsVariableSize()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5176 | std::string temp; |
| 5177 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5178 | size_t previous_size = klass->GetObjectSize(); |
| 5179 | if (previous_size != 0) { |
| 5180 | // Make sure that we didn't originally have an incorrect size. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5181 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 5182 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5183 | klass->SetObjectSize(size); |
| 5184 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5185 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5186 | return true; |
| 5187 | } |
| 5188 | |
| 5189 | // Set the bitmap of reference offsets, refOffsets, from the ifields |
| 5190 | // list. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5191 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5192 | uint32_t reference_offsets = 0; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5193 | mirror::Class* super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5194 | // 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] | 5195 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5196 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 5197 | // Compute reference offsets unless our superclass overflowed. |
| 5198 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 5199 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
| 5200 | mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields(); |
| 5201 | // All of the fields that contain object references are guaranteed |
| 5202 | // to be at the beginning of the fields list. |
| 5203 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 5204 | // Note that byte_offset is the offset from the beginning of |
| 5205 | // object, not the offset into instance data |
| 5206 | mirror::ArtField* field = fields->Get(i); |
| 5207 | MemberOffset byte_offset = field->GetOffsetDuringLinking(); |
| 5208 | uint32_t displaced_bitmap_position = |
| 5209 | (byte_offset.Uint32Value() - mirror::kObjectHeaderSize) / |
| 5210 | sizeof(mirror::HeapReference<mirror::Object>); |
| 5211 | if (displaced_bitmap_position >= 32) { |
| 5212 | // Can't encode offset so fall back on slow-path. |
| 5213 | reference_offsets = mirror::Class::kClassWalkSuper; |
| 5214 | break; |
| 5215 | } else { |
| 5216 | reference_offsets |= (1 << displaced_bitmap_position); |
| 5217 | } |
| 5218 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5219 | } |
| 5220 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 5221 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5222 | } |
| 5223 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5224 | mirror::String* ClassLinker::ResolveString(const DexFile& dex_file, uint32_t string_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5225 | Handle<mirror::DexCache> dex_cache) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5226 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5227 | mirror::String* resolved = dex_cache->GetResolvedString(string_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5228 | if (resolved != nullptr) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5229 | return resolved; |
| 5230 | } |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5231 | uint32_t utf16_length; |
| 5232 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5233 | mirror::String* string = intern_table_->InternStrong(utf16_length, utf8_data); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5234 | dex_cache->SetResolvedString(string_idx, string); |
| 5235 | return string; |
| 5236 | } |
| 5237 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5238 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 5239 | mirror::Class* referrer) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5240 | StackHandleScope<2> hs(Thread::Current()); |
| 5241 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 5242 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5243 | return ResolveType(dex_file, type_idx, dex_cache, class_loader); |
| 5244 | } |
| 5245 | |
| 5246 | mirror::Class* ClassLinker::ResolveType(const DexFile& dex_file, uint16_t type_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5247 | Handle<mirror::DexCache> dex_cache, |
| 5248 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5249 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5250 | mirror::Class* resolved = dex_cache->GetResolvedType(type_idx); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5251 | if (resolved == nullptr) { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5252 | Thread* self = Thread::Current(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 5253 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5254 | resolved = FindClass(self, descriptor, class_loader); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5255 | if (resolved != nullptr) { |
Jesse Wilson | 254db0f | 2011-11-16 16:44:11 -0500 | [diff] [blame] | 5256 | // TODO: we used to throw here if resolved's class loader was not the |
| 5257 | // boot class loader. This was to permit different classes with the |
| 5258 | // same name to be loaded simultaneously by different loaders |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5259 | dex_cache->SetResolvedType(type_idx, resolved); |
| 5260 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5261 | CHECK(self->IsExceptionPending()) |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 5262 | << "Expected pending exception for failed resolution of: " << descriptor; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5263 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5264 | StackHandleScope<1> hs(self); |
| 5265 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException(nullptr))); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5266 | if (cause->InstanceOf(GetClassRoot(kJavaLangClassNotFoundException))) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5267 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 5268 | self->ClearException(); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5269 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5270 | self->GetException(nullptr)->SetCause(cause.Get()); |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5271 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5272 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5273 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5274 | DCHECK((resolved == nullptr) || resolved->IsResolved() || resolved->IsErroneous()) |
Brian Carlstrom | abcf7ae | 2013-09-23 22:19:52 -0700 | [diff] [blame] | 5275 | << PrettyDescriptor(resolved) << " " << resolved->GetStatus(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5276 | return resolved; |
| 5277 | } |
| 5278 | |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5279 | mirror::ArtMethod* ClassLinker::ResolveMethod(const DexFile& dex_file, uint32_t method_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5280 | Handle<mirror::DexCache> dex_cache, |
| 5281 | Handle<mirror::ClassLoader> class_loader, |
| 5282 | Handle<mirror::ArtMethod> referrer, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5283 | InvokeType type) { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5284 | DCHECK(dex_cache.Get() != nullptr); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5285 | // Check for hit in the dex cache. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5286 | mirror::ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5287 | if (resolved != nullptr && !resolved->IsRuntimeMethod()) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5288 | return resolved; |
| 5289 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5290 | // Fail, get the declaring class. |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5291 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5292 | 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] | 5293 | if (klass == nullptr) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 5294 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5295 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5296 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5297 | // Scan using method_idx, this saves string compares but will only hit for matching dex |
| 5298 | // caches/files. |
| 5299 | switch (type) { |
| 5300 | case kDirect: // Fall-through. |
| 5301 | case kStatic: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5302 | resolved = klass->FindDirectMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5303 | break; |
| 5304 | case kInterface: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5305 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5306 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5307 | break; |
| 5308 | case kSuper: // Fall-through. |
| 5309 | case kVirtual: |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5310 | resolved = klass->FindVirtualMethod(dex_cache.Get(), method_idx); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5311 | break; |
| 5312 | default: |
| 5313 | LOG(FATAL) << "Unreachable - invocation type: " << type; |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5314 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5315 | if (resolved == nullptr) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5316 | // Search by name, which works across dex files. |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5317 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 5318 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5319 | switch (type) { |
| 5320 | case kDirect: // Fall-through. |
| 5321 | case kStatic: |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 5322 | resolved = klass->FindDirectMethod(name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5323 | break; |
| 5324 | case kInterface: |
| 5325 | resolved = klass->FindInterfaceMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5326 | DCHECK(resolved == nullptr || resolved->GetDeclaringClass()->IsInterface()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5327 | break; |
| 5328 | case kSuper: // Fall-through. |
| 5329 | case kVirtual: |
| 5330 | resolved = klass->FindVirtualMethod(name, signature); |
| 5331 | break; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5332 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5333 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5334 | // If we found a method, check for incompatible class changes. |
| 5335 | if (LIKELY(resolved != nullptr && !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5336 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 5337 | dex_cache->SetResolvedMethod(method_idx, resolved); |
| 5338 | return resolved; |
| 5339 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5340 | // If we had a method, it's an incompatible-class-change error. |
| 5341 | if (resolved != nullptr) { |
| 5342 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer.Get()); |
| 5343 | } else { |
| 5344 | // We failed to find the method which means either an access error, an incompatible class |
| 5345 | // change, or no such method. First try to find the method among direct and virtual methods. |
| 5346 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 5347 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 5348 | switch (type) { |
| 5349 | case kDirect: |
| 5350 | case kStatic: |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5351 | resolved = klass->FindVirtualMethod(name, signature); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5352 | // Note: kDirect and kStatic are also mutually exclusive, but in that case we would |
| 5353 | // have had a resolved method before, which triggers the "true" branch above. |
| 5354 | break; |
| 5355 | case kInterface: |
| 5356 | case kVirtual: |
| 5357 | case kSuper: |
| 5358 | resolved = klass->FindDirectMethod(name, signature); |
| 5359 | break; |
| 5360 | } |
| 5361 | |
| 5362 | // If we found something, check that it can be accessed by the referrer. |
| 5363 | if (resolved != nullptr && referrer.Get() != nullptr) { |
| 5364 | mirror::Class* methods_class = resolved->GetDeclaringClass(); |
| 5365 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
| 5366 | if (!referring_class->CanAccess(methods_class)) { |
| 5367 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 5368 | resolved, type); |
| 5369 | return nullptr; |
| 5370 | } else if (!referring_class->CanAccessMember(methods_class, |
| 5371 | resolved->GetAccessFlags())) { |
| 5372 | ThrowIllegalAccessErrorMethod(referring_class, resolved); |
| 5373 | return nullptr; |
| 5374 | } |
| 5375 | } |
| 5376 | |
| 5377 | // Otherwise, throw an IncompatibleClassChangeError if we found something, and check interface |
| 5378 | // methods and throw if we find the method there. If we find nothing, throw a |
| 5379 | // NoSuchMethodError. |
| 5380 | switch (type) { |
| 5381 | case kDirect: |
| 5382 | case kStatic: |
| 5383 | if (resolved != nullptr) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 5384 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5385 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5386 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5387 | if (resolved != nullptr) { |
| 5388 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5389 | } else { |
| 5390 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5391 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5392 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5393 | break; |
| 5394 | case kInterface: |
| 5395 | if (resolved != nullptr) { |
| 5396 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5397 | } else { |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5398 | resolved = klass->FindVirtualMethod(name, signature); |
| 5399 | if (resolved != nullptr) { |
| 5400 | ThrowIncompatibleClassChangeError(type, kVirtual, resolved, referrer.Get()); |
| 5401 | } else { |
| 5402 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5403 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5404 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5405 | break; |
| 5406 | case kSuper: |
Andreas Gampe | df73375 | 2014-08-25 20:55:01 -0700 | [diff] [blame] | 5407 | if (resolved != nullptr) { |
| 5408 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5409 | } else { |
| 5410 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5411 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5412 | break; |
| 5413 | case kVirtual: |
| 5414 | if (resolved != nullptr) { |
| 5415 | ThrowIncompatibleClassChangeError(type, kDirect, resolved, referrer.Get()); |
| 5416 | } else { |
| 5417 | resolved = klass->FindInterfaceMethod(name, signature); |
| 5418 | if (resolved != nullptr) { |
| 5419 | ThrowIncompatibleClassChangeError(type, kInterface, resolved, referrer.Get()); |
| 5420 | } else { |
| 5421 | ThrowNoSuchMethodError(type, klass, name, signature); |
| 5422 | } |
| 5423 | } |
| 5424 | break; |
| 5425 | } |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5426 | } |
| 5427 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 5428 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 5429 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5430 | } |
| 5431 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5432 | mirror::ArtField* ClassLinker::ResolveField(const DexFile& dex_file, uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5433 | Handle<mirror::DexCache> dex_cache, |
| 5434 | Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | e810452 | 2013-10-15 21:56:36 -0700 | [diff] [blame] | 5435 | bool is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5436 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5437 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5438 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5439 | return resolved; |
| 5440 | } |
| 5441 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5442 | Thread* const self = Thread::Current(); |
| 5443 | StackHandleScope<1> hs(self); |
| 5444 | Handle<mirror::Class> klass( |
| 5445 | 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] | 5446 | if (klass.Get() == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 5447 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5448 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5449 | } |
| 5450 | |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5451 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5452 | resolved = mirror::Class::FindStaticField(self, klass, dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5453 | } else { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5454 | resolved = klass->FindInstanceField(dex_cache.Get(), field_idx); |
Brian Carlstrom | 20cfffa | 2011-08-26 02:31:27 -0700 | [diff] [blame] | 5455 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5456 | |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5457 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5458 | const char* name = dex_file.GetFieldName(field_id); |
| 5459 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 5460 | if (is_static) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5461 | resolved = mirror::Class::FindStaticField(self, klass, name, type); |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5462 | } else { |
| 5463 | resolved = klass->FindInstanceField(name, type); |
| 5464 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5465 | if (resolved == nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5466 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass.Get(), type, name); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5467 | return nullptr; |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5468 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5469 | } |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 5470 | dex_cache->SetResolvedField(field_idx, resolved); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5471 | return resolved; |
| 5472 | } |
| 5473 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5474 | mirror::ArtField* ClassLinker::ResolveFieldJLS(const DexFile& dex_file, |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5475 | uint32_t field_idx, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5476 | Handle<mirror::DexCache> dex_cache, |
| 5477 | Handle<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5478 | DCHECK(dex_cache.Get() != nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5479 | mirror::ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 5480 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5481 | return resolved; |
| 5482 | } |
| 5483 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5484 | Thread* self = Thread::Current(); |
| 5485 | StackHandleScope<1> hs(self); |
| 5486 | Handle<mirror::Class> klass( |
| 5487 | 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] | 5488 | if (klass.Get() == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5489 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5490 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5491 | } |
| 5492 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 5493 | StringPiece name(dex_file.StringDataByIdx(field_id.name_idx_)); |
| 5494 | StringPiece type(dex_file.StringDataByIdx( |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 5495 | dex_file.GetTypeId(field_id.type_idx_).descriptor_idx_)); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5496 | resolved = mirror::Class::FindField(self, klass, name, type); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5497 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 5498 | dex_cache->SetResolvedField(field_idx, resolved); |
| 5499 | } else { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5500 | ThrowNoSuchFieldError("", klass.Get(), type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 5501 | } |
| 5502 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 5503 | } |
| 5504 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 5505 | const char* ClassLinker::MethodShorty(uint32_t method_idx, mirror::ArtMethod* referrer, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5506 | uint32_t* length) { |
| 5507 | mirror::Class* declaring_class = referrer->GetDeclaringClass(); |
| 5508 | mirror::DexCache* dex_cache = declaring_class->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 5509 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5510 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5511 | return dex_file.GetMethodShorty(method_id, length); |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 5512 | } |
| 5513 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5514 | void ClassLinker::DumpAllClasses(int flags) { |
| 5515 | if (dex_cache_image_class_lookup_required_) { |
| 5516 | MoveImageClassesToClassTable(); |
| 5517 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5518 | // TODO: at the time this was written, it wasn't safe to call PrettyField with the ClassLinker |
| 5519 | // 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] | 5520 | std::vector<mirror::Class*> all_classes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5521 | { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5522 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5523 | for (std::pair<const size_t, GcRoot<mirror::Class> >& it : class_table_) { |
| 5524 | mirror::Class* klass = it.second.Read(); |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 5525 | all_classes.push_back(klass); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 5526 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | for (size_t i = 0; i < all_classes.size(); ++i) { |
| 5530 | all_classes[i]->DumpClass(std::cerr, flags); |
| 5531 | } |
| 5532 | } |
| 5533 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5534 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5535 | Thread* self = Thread::Current(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5536 | if (dex_cache_image_class_lookup_required_) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5537 | ScopedObjectAccess soa(self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5538 | MoveImageClassesToClassTable(); |
| 5539 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5540 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5541 | os << "Loaded classes: " << class_table_.size() << " allocated classes\n"; |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 5542 | } |
| 5543 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5544 | size_t ClassLinker::NumLoadedClasses() { |
| 5545 | if (dex_cache_image_class_lookup_required_) { |
| 5546 | MoveImageClassesToClassTable(); |
| 5547 | } |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 5548 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5549 | return class_table_.size(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 5550 | } |
| 5551 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5552 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 5553 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | pid_t ClassLinker::GetDexLockOwner() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5557 | return dex_lock_.GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 5558 | } |
| 5559 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 5560 | void ClassLinker::SetClassRoot(ClassRoot class_root, mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5561 | DCHECK(!init_done_); |
| 5562 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5563 | DCHECK(klass != nullptr); |
| 5564 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5565 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 5566 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5567 | DCHECK(class_roots != nullptr); |
| 5568 | DCHECK(class_roots->Get(class_root) == nullptr); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 5569 | class_roots->Set<false>(class_root, klass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5570 | } |
| 5571 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5572 | } // namespace art |