Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -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 | */ |
| 16 | |
| 17 | #include "class.h" |
| 18 | |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 19 | #include "android-base/macros.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 20 | #include "android-base/stringprintf.h" |
| 21 | |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 22 | #include "array-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
| 24 | #include "art_method-inl.h" |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 25 | #include "base/enums.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 26 | #include "base/logging.h" // For VLOG. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 27 | #include "base/utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include "class-inl.h" |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 29 | #include "class_ext-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 30 | #include "class_linker-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "class_loader.h" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 32 | #include "class_root.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 33 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file-inl.h" |
| 35 | #include "dex/dex_file_annotations.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 36 | #include "dex/signature-inl.h" |
Vladimir Marko | 58412b1 | 2019-04-01 13:26:34 +0100 | [diff] [blame] | 37 | #include "dex_cache-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 39 | #include "gc/heap-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 40 | #include "handle_scope-inl.h" |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 41 | #include "hidden_api.h" |
Alex Light | f367747 | 2019-06-26 16:31:53 -0700 | [diff] [blame] | 42 | #include "jni_id_type.h" |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 43 | #include "subtype_check.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 44 | #include "method.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 45 | #include "object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 46 | #include "object-refvisitor-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 47 | #include "object_array-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 48 | #include "object_lock.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 49 | #include "string-inl.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 50 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 51 | #include "thread.h" |
| 52 | #include "throwable.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | #include "well_known_classes.h" |
| 54 | |
| 55 | namespace art { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 56 | |
| 57 | // TODO: move to own CC file? |
| 58 | constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity]; |
| 59 | constexpr size_t BitString::kCapacity; |
| 60 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 61 | namespace mirror { |
| 62 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 63 | using android::base::StringPrintf; |
| 64 | |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 65 | ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) { |
| 66 | const char* expected_name = nullptr; |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 67 | ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid. |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 68 | if (name != nullptr && name->GetLength() >= 2) { |
| 69 | // Perfect hash for the expected values: from the second letters of the primitive types, |
| 70 | // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'. |
| 71 | char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1); |
| 72 | switch (hash) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 73 | case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break; |
| 74 | case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break; |
| 75 | case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break; |
| 76 | case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break; |
| 77 | case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break; |
| 78 | case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break; |
| 79 | case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break; |
| 80 | case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break; |
| 81 | case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break; |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 82 | default: break; |
| 83 | } |
| 84 | } |
| 85 | if (expected_name != nullptr && name->Equals(expected_name)) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 86 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 87 | DCHECK(klass != nullptr); |
| 88 | return klass; |
| 89 | } else { |
| 90 | Thread* self = Thread::Current(); |
| 91 | if (name == nullptr) { |
| 92 | // Note: ThrowNullPointerException() requires a message which we deliberately want to omit. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 93 | self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr); |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 94 | } else { |
| 95 | self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str()); |
| 96 | } |
| 97 | return nullptr; |
| 98 | } |
| 99 | } |
| 100 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 101 | ObjPtr<ClassExt> Class::EnsureExtDataPresent(Handle<Class> h_this, Thread* self) { |
| 102 | ObjPtr<ClassExt> existing(h_this->GetExtData()); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 103 | if (!existing.IsNull()) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 104 | return existing; |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 105 | } |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 106 | StackHandleScope<2> hs(self); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 107 | // Clear exception so we can allocate. |
| 108 | Handle<Throwable> throwable(hs.NewHandle(self->GetException())); |
| 109 | self->ClearException(); |
| 110 | // Allocate the ClassExt |
| 111 | Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 112 | if (new_ext == nullptr) { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 113 | // OOM allocating the classExt. |
| 114 | // TODO Should we restore the suppressed exception? |
| 115 | self->AssertPendingOOMException(); |
| 116 | return nullptr; |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 117 | } else { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 118 | MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_)); |
| 119 | bool set; |
| 120 | // Set the ext_data_ field using CAS semantics. |
| 121 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | a9746b9 | 2018-06-22 10:25:40 -0700 | [diff] [blame] | 122 | set = h_this->CasFieldObject<true>(ext_offset, |
| 123 | nullptr, |
| 124 | new_ext.Get(), |
| 125 | CASMode::kStrong, |
| 126 | std::memory_order_seq_cst); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 127 | } else { |
Mathieu Chartier | a9746b9 | 2018-06-22 10:25:40 -0700 | [diff] [blame] | 128 | set = h_this->CasFieldObject<false>(ext_offset, |
| 129 | nullptr, |
| 130 | new_ext.Get(), |
| 131 | CASMode::kStrong, |
| 132 | std::memory_order_seq_cst); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 133 | } |
| 134 | ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData()); |
| 135 | DCHECK(!set || h_this->GetExtData() == new_ext.Get()); |
| 136 | CHECK(!ret.IsNull()); |
| 137 | // Restore the exception if there was one. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 138 | if (throwable != nullptr) { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 139 | self->SetException(throwable.Get()); |
| 140 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 141 | return ret; |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 145 | void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) { |
| 146 | ClassStatus old_status = h_this->GetStatus(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 147 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 148 | bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 149 | if (LIKELY(class_linker_initialized)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 150 | if (UNLIKELY(new_status <= old_status && |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 151 | new_status != ClassStatus::kErrorUnresolved && |
| 152 | new_status != ClassStatus::kErrorResolved && |
| 153 | new_status != ClassStatus::kRetired)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 154 | LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass() |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 155 | << " " << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 156 | } |
Vladimir Marko | 422a9eb | 2019-08-01 12:54:07 +0100 | [diff] [blame] | 157 | if (old_status == ClassStatus::kInitialized) { |
| 158 | // We do not hold the lock for making the class visibly initialized |
| 159 | // as this is unnecessary and could lead to deadlocks. |
| 160 | CHECK_EQ(new_status, ClassStatus::kVisiblyInitialized); |
| 161 | } else if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 162 | // When classes are being resolved the resolution code should hold the lock. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 163 | CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 164 | << "Attempt to change status of class while not holding its lock: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 165 | << h_this->PrettyClass() << " " << old_status << " -> " << new_status; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 166 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 168 | if (UNLIKELY(IsErroneous(new_status))) { |
| 169 | CHECK(!h_this->IsErroneous()) |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 170 | << "Attempt to set as erroneous an already erroneous class " |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 171 | << h_this->PrettyClass() |
| 172 | << " old_status: " << old_status << " new_status: " << new_status; |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 173 | CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved); |
Andreas Gampe | 31decb1 | 2015-08-24 21:09:05 -0700 | [diff] [blame] | 174 | if (VLOG_IS_ON(class_linker)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 175 | LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous."; |
Andreas Gampe | 31decb1 | 2015-08-24 21:09:05 -0700 | [diff] [blame] | 176 | if (self->IsExceptionPending()) { |
| 177 | LOG(ERROR) << "Exception: " << self->GetException()->Dump(); |
| 178 | } |
| 179 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 181 | ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self)); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 182 | if (!ext.IsNull()) { |
| 183 | self->AssertPendingException(); |
| 184 | ext->SetVerifyError(self->GetException()); |
| 185 | } else { |
| 186 | self->AssertPendingOOMException(); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 187 | } |
| 188 | self->AssertPendingException(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 189 | } |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 190 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 191 | if (kBitstringSubtypeCheckEnabled) { |
| 192 | // FIXME: This looks broken with respect to aborted transactions. |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 193 | ObjPtr<mirror::Class> h_this_ptr = h_this.Get(); |
| 194 | SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status); |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 195 | } else { |
| 196 | // The ClassStatus is always in the 4 most-significant bits of status_. |
| 197 | static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32"); |
| 198 | uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize); |
| 199 | if (Runtime::Current()->IsActiveTransaction()) { |
| 200 | h_this->SetField32Volatile<true>(StatusOffset(), new_status_value); |
| 201 | } else { |
| 202 | h_this->SetField32Volatile<false>(StatusOffset(), new_status_value); |
| 203 | } |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Setting the object size alloc fast path needs to be after the status write so that if the |
| 207 | // alloc path sees a valid object size, we would know that it's initialized as long as it has a |
| 208 | // load-acquire/fake dependency. |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 209 | if (new_status == ClassStatus::kVisiblyInitialized && !h_this->IsVariableSize()) { |
Mathieu Chartier | 161db1d | 2016-09-01 14:06:54 -0700 | [diff] [blame] | 210 | DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max()); |
| 211 | // Finalizable objects must always go slow path. |
| 212 | if (!h_this->IsFinalizable()) { |
| 213 | h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment)); |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 214 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 215 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 216 | |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 217 | if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) { |
| 218 | CHECK(h_this->WasVerificationAttempted()) << h_this->PrettyClassAndClassLoader(); |
| 219 | } |
| 220 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 221 | if (!class_linker_initialized) { |
| 222 | // When the class linker is being initialized its single threaded and by definition there can be |
| 223 | // no waiters. During initialization classes may appear temporary but won't be retired as their |
| 224 | // size was statically computed. |
| 225 | } else { |
| 226 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 227 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 228 | if (h_this->IsTemp()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 229 | // Class is a temporary one, ensure that waiters for resolution get notified of retirement |
| 230 | // so that they can grab the new version of the class from the class linker's table. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 231 | CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor(); |
| 232 | if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 233 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 234 | } |
Vladimir Marko | 422a9eb | 2019-08-01 12:54:07 +0100 | [diff] [blame] | 235 | } else if (old_status == ClassStatus::kInitialized) { |
| 236 | // Do not notify for transition from kInitialized to ClassStatus::kVisiblyInitialized. |
| 237 | // This is a hidden transition, not observable by bytecode. |
| 238 | DCHECK_EQ(new_status, ClassStatus::kVisiblyInitialized); // Already CHECK()ed above. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 239 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 240 | CHECK_NE(new_status, ClassStatus::kRetired); |
| 241 | if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 242 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 245 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 248 | void Class::SetStatusForPrimitiveOrArray(ClassStatus new_status) { |
| 249 | DCHECK(IsPrimitive<kVerifyNone>() || IsArrayClass<kVerifyNone>()); |
| 250 | DCHECK(!IsErroneous(new_status)); |
| 251 | DCHECK(!IsErroneous(GetStatus<kVerifyNone>())); |
| 252 | DCHECK_GT(new_status, GetStatus<kVerifyNone>()); |
| 253 | |
| 254 | if (kBitstringSubtypeCheckEnabled) { |
| 255 | LOG(FATAL) << "Unimplemented"; |
| 256 | } |
| 257 | // The ClassStatus is always in the 4 most-significant bits of status_. |
| 258 | static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32"); |
| 259 | uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize); |
| 260 | // Use normal store. For primitives and core arrays classes (Object[], |
| 261 | // Class[], String[] and primitive arrays), the status is set while the |
| 262 | // process is still single threaded. For other arrays classes, it is set |
| 263 | // in a pre-fence visitor which initializes all fields and the subsequent |
| 264 | // fence together with address dependency shall ensure memory visibility. |
| 265 | SetField32</*kTransactionActive=*/ false, |
| 266 | /*kCheckTransaction=*/ false, |
| 267 | kVerifyNone>(StatusOffset(), new_status_value); |
| 268 | |
| 269 | // Do not update `object_alloc_fast_path_`. Arrays are variable size and |
| 270 | // instances of primitive classes cannot be created at all. |
| 271 | |
| 272 | if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) { |
| 273 | CHECK(WasVerificationAttempted()) << PrettyClassAndClassLoader(); |
| 274 | } |
| 275 | |
| 276 | // There can be no waiters to notify as these classes are initialized |
| 277 | // before another thread can see them. |
| 278 | } |
| 279 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 280 | void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) { |
Chang Xing | 6d3e768 | 2017-07-11 10:31:29 -0700 | [diff] [blame] | 281 | SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 284 | void Class::SetClassSize(uint32_t new_class_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 285 | if (kIsDebugBuild && new_class_size < GetClassSize()) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 286 | DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail); |
| 287 | LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 288 | LOG(FATAL) << "class=" << PrettyTypeOf(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 289 | } |
Chang Xing | 6d3e768 | 2017-07-11 10:31:29 -0700 | [diff] [blame] | 290 | SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 294 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 295 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 296 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
Vladimir Marko | 179b7c6 | 2019-03-22 13:38:57 +0000 | [diff] [blame] | 297 | ObjPtr<String> Class::ComputeName(Handle<Class> h_this) { |
| 298 | ObjPtr<String> name = h_this->GetName(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 299 | if (name != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | return name; |
| 301 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 302 | std::string temp; |
| 303 | const char* descriptor = h_this->GetDescriptor(&temp); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 304 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 305 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 306 | // The descriptor indicates that this is the class for |
| 307 | // a primitive type; special-case the return value. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 308 | const char* c_name = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 309 | switch (descriptor[0]) { |
| 310 | case 'Z': c_name = "boolean"; break; |
| 311 | case 'B': c_name = "byte"; break; |
| 312 | case 'C': c_name = "char"; break; |
| 313 | case 'S': c_name = "short"; break; |
| 314 | case 'I': c_name = "int"; break; |
| 315 | case 'J': c_name = "long"; break; |
| 316 | case 'F': c_name = "float"; break; |
| 317 | case 'D': c_name = "double"; break; |
| 318 | case 'V': c_name = "void"; break; |
| 319 | default: |
| 320 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 321 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 322 | name = String::AllocFromModifiedUtf8(self, c_name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 323 | } else { |
| 324 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 325 | // components. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 326 | name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 327 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 328 | h_this->SetName(name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | return name; |
| 330 | } |
| 331 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 332 | void Class::DumpClass(std::ostream& os, int flags) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 333 | if ((flags & kDumpClassFullDetail) == 0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 334 | os << PrettyClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | if ((flags & kDumpClassClassLoader) != 0) { |
| 336 | os << ' ' << GetClassLoader(); |
| 337 | } |
| 338 | if ((flags & kDumpClassInitialized) != 0) { |
| 339 | os << ' ' << GetStatus(); |
| 340 | } |
| 341 | os << "\n"; |
| 342 | return; |
| 343 | } |
| 344 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 345 | Thread* const self = Thread::Current(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 346 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 347 | Handle<Class> h_this(hs.NewHandle(this)); |
| 348 | Handle<Class> h_super(hs.NewHandle(GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 349 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 350 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 351 | std::string temp; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 352 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 353 | << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 354 | os << " objectSize=" << SizeOf() << " " |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 355 | << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 356 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 357 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 358 | if (h_super != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 359 | os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader() |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 360 | << ")\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | } |
| 362 | if (IsArrayClass()) { |
| 363 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 364 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 365 | const size_t num_direct_interfaces = NumDirectInterfaces(); |
| 366 | if (num_direct_interfaces > 0) { |
| 367 | os << " interfaces (" << num_direct_interfaces << "):\n"; |
| 368 | for (size_t i = 0; i < num_direct_interfaces; ++i) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 369 | ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 370 | if (interface == nullptr) { |
| 371 | os << StringPrintf(" %2zd: nullptr!\n", i); |
| 372 | } else { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 373 | ObjPtr<ClassLoader> cl = interface->GetClassLoader(); |
| 374 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr()); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 375 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 378 | if (!IsLoaded()) { |
| 379 | os << " class not yet loaded"; |
| 380 | } else { |
| 381 | // After this point, this may have moved due to GetDirectInterface. |
| 382 | os << " vtable (" << h_this->NumVirtualMethods() << " entries, " |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 383 | << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 384 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 385 | os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 386 | h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 387 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 388 | os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; |
| 389 | for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 390 | os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 391 | h_this->GetDirectMethod(i, image_pointer_size)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 392 | } |
| 393 | if (h_this->NumStaticFields() > 0) { |
| 394 | os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 395 | if (h_this->IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 396 | for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 397 | os << StringPrintf(" %2zd: %s\n", i, |
| 398 | ArtField::PrettyField(h_this->GetStaticField(i)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 399 | } |
| 400 | } else { |
| 401 | os << " <not yet available>"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 402 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 403 | } |
| 404 | if (h_this->NumInstanceFields() > 0) { |
| 405 | os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 406 | if (h_this->IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 407 | for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 408 | os << StringPrintf(" %2zd: %s\n", i, |
| 409 | ArtField::PrettyField(h_this->GetInstanceField(i)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 410 | } |
| 411 | } else { |
| 412 | os << " <not yet available>"; |
| 413 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 419 | if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | // Sanity check that the number of bits set in the reference offset bitmap |
| 421 | // agrees with the number of references |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 422 | uint32_t count = 0; |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 423 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 424 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 425 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 426 | // +1 for the Class in Object. |
| 427 | CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 428 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 429 | // Not called within a transaction. |
| 430 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 431 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 434 | bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 435 | size_t i = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 436 | size_t min_length = std::min(descriptor1.size(), descriptor2.size()); |
| 437 | while (i < min_length && descriptor1[i] == descriptor2[i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 438 | ++i; |
| 439 | } |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 440 | if (descriptor1.find('/', i) != std::string_view::npos || |
| 441 | descriptor2.find('/', i) != std::string_view::npos) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 442 | return false; |
| 443 | } else { |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 448 | bool Class::IsInSamePackage(ObjPtr<Class> that) { |
| 449 | ObjPtr<Class> klass1 = this; |
| 450 | ObjPtr<Class> klass2 = that; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 451 | if (klass1 == klass2) { |
| 452 | return true; |
| 453 | } |
| 454 | // Class loaders must match. |
| 455 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 456 | return false; |
| 457 | } |
| 458 | // Arrays are in the same package when their element classes are. |
| 459 | while (klass1->IsArrayClass()) { |
| 460 | klass1 = klass1->GetComponentType(); |
| 461 | } |
| 462 | while (klass2->IsArrayClass()) { |
| 463 | klass2 = klass2->GetComponentType(); |
| 464 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 465 | // trivial check again for array types |
| 466 | if (klass1 == klass2) { |
| 467 | return true; |
| 468 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 469 | // Compare the package part of the descriptor string. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 470 | std::string temp1, temp2; |
| 471 | return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 474 | bool Class::IsThrowableClass() { |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 475 | return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 478 | template <typename SignatureType> |
| 479 | static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass, |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 480 | std::string_view name, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 481 | const SignatureType& signature, |
| 482 | PointerSize pointer_size) |
| 483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 484 | // If the current class is not an interface, skip the search of its declared methods; |
| 485 | // such lookup is used only to distinguish between IncompatibleClassChangeError and |
| 486 | // NoSuchMethodError and the caller has already tried to search methods in the class. |
| 487 | if (LIKELY(klass->IsInterface())) { |
| 488 | // Search declared methods, both direct and virtual. |
| 489 | // (This lookup is used also for invoke-static on interface classes.) |
| 490 | for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 491 | if (method.GetNameView() == name && method.GetSignature() == signature) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 492 | return &method; |
| 493 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 496 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 497 | // TODO: If there is a unique maximally-specific non-abstract superinterface method, |
| 498 | // we should return it, otherwise an arbitrary one can be returned. |
| 499 | ObjPtr<IfTable> iftable = klass->GetIfTable(); |
| 500 | for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) { |
| 501 | ObjPtr<Class> iface = iftable->GetInterface(i); |
| 502 | for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) { |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 503 | if (method.GetNameView() == name && method.GetSignature() == signature) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 504 | return &method; |
| 505 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 508 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 509 | // Then search for public non-static methods in the java.lang.Object. |
| 510 | if (LIKELY(klass->IsInterface())) { |
| 511 | ObjPtr<Class> object_class = klass->GetSuperClass(); |
| 512 | DCHECK(object_class->IsObjectClass()); |
| 513 | for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) { |
| 514 | if (method.IsPublic() && !method.IsStatic() && |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 515 | method.GetNameView() == name && method.GetSignature() == signature) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 516 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 520 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 523 | ArtMethod* Class::FindInterfaceMethod(std::string_view name, |
| 524 | std::string_view signature, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 525 | PointerSize pointer_size) { |
Vladimir Marko | eb37ba5 | 2019-02-05 14:10:38 +0000 | [diff] [blame] | 526 | return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 529 | ArtMethod* Class::FindInterfaceMethod(std::string_view name, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 530 | const Signature& signature, |
| 531 | PointerSize pointer_size) { |
| 532 | return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 535 | ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache, |
| 536 | uint32_t dex_method_idx, |
| 537 | PointerSize pointer_size) { |
| 538 | // We always search by name and signature, ignoring the type index in the MethodId. |
| 539 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 540 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Eric Holk | abdb459 | 2019-05-16 08:33:12 -0700 | [diff] [blame] | 541 | std::string_view name = dex_file.StringViewByIdx(method_id.name_idx_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 542 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 543 | return FindInterfaceMethod(name, signature, pointer_size); |
| 544 | } |
| 545 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 546 | static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass, |
| 547 | ObjPtr<mirror::Class> declaring_class) |
| 548 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 549 | if (klass->IsArrayClass()) { |
| 550 | return declaring_class->IsObjectClass(); |
| 551 | } else if (klass->IsInterface()) { |
| 552 | return declaring_class->IsObjectClass() || declaring_class == klass; |
| 553 | } else { |
| 554 | return klass->IsSubClass(declaring_class); |
| 555 | } |
| 556 | } |
| 557 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 558 | static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass, |
| 559 | ObjPtr<mirror::Class> declaring_class, |
| 560 | ArtMethod& method) |
| 561 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 562 | DCHECK_EQ(declaring_class, method.GetDeclaringClass()); |
| 563 | DCHECK_NE(klass, declaring_class); |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 564 | DCHECK(IsValidInheritanceCheck(klass, declaring_class)); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 565 | uint32_t access_flags = method.GetAccessFlags(); |
| 566 | if ((access_flags & (kAccPublic | kAccProtected)) != 0) { |
| 567 | return true; |
| 568 | } |
| 569 | if ((access_flags & kAccPrivate) != 0) { |
| 570 | return false; |
| 571 | } |
| 572 | for (; klass != declaring_class; klass = klass->GetSuperClass()) { |
| 573 | if (!klass->IsInSamePackage(declaring_class)) { |
| 574 | return false; |
| 575 | } |
| 576 | } |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | template <typename SignatureType> |
| 581 | static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass, |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 582 | std::string_view name, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 583 | const SignatureType& signature, |
| 584 | PointerSize pointer_size) |
| 585 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 586 | // Search declared methods first. |
| 587 | for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) { |
| 588 | ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 589 | if (np_method->GetName() == name && np_method->GetSignature() == signature) { |
| 590 | return &method; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Then search the superclass chain. If we find an inherited method, return it. |
| 595 | // If we find a method that's not inherited because of access restrictions, |
| 596 | // try to find a method inherited from an interface in copied methods. |
| 597 | ObjPtr<Class> klass = this_klass->GetSuperClass(); |
| 598 | ArtMethod* uninherited_method = nullptr; |
| 599 | for (; klass != nullptr; klass = klass->GetSuperClass()) { |
| 600 | DCHECK(!klass->IsProxyClass()); |
| 601 | for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { |
| 602 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 603 | if (IsInheritedMethod(this_klass, klass, method)) { |
| 604 | return &method; |
| 605 | } |
| 606 | uninherited_method = &method; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | if (uninherited_method != nullptr) { |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Then search copied methods. |
| 616 | // If we found a method that's not inherited, stop the search in its declaring class. |
| 617 | ObjPtr<Class> end_klass = klass; |
| 618 | DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); |
| 619 | klass = this_klass; |
| 620 | if (UNLIKELY(klass->IsProxyClass())) { |
| 621 | DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty()); |
| 622 | klass = klass->GetSuperClass(); |
| 623 | } |
| 624 | for (; klass != end_klass; klass = klass->GetSuperClass()) { |
| 625 | DCHECK(!klass->IsProxyClass()); |
| 626 | for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) { |
| 627 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 628 | return &method; // No further check needed, copied methods are inherited by definition. |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | return uninherited_method; // Return the `uninherited_method` if any. |
| 633 | } |
| 634 | |
| 635 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 636 | ArtMethod* Class::FindClassMethod(std::string_view name, |
| 637 | std::string_view signature, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 638 | PointerSize pointer_size) { |
Vladimir Marko | eb37ba5 | 2019-02-05 14:10:38 +0000 | [diff] [blame] | 639 | return FindClassMethodWithSignature(this, name, signature, pointer_size); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 642 | ArtMethod* Class::FindClassMethod(std::string_view name, |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 643 | const Signature& signature, |
| 644 | PointerSize pointer_size) { |
| 645 | return FindClassMethodWithSignature(this, name, signature, pointer_size); |
| 646 | } |
| 647 | |
| 648 | ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache, |
| 649 | uint32_t dex_method_idx, |
| 650 | PointerSize pointer_size) { |
| 651 | // FIXME: Hijacking a proxy class by a custom class loader can break this assumption. |
| 652 | DCHECK(!IsProxyClass()); |
| 653 | |
| 654 | // First try to find a declared method by dex_method_idx if we have a dex_cache match. |
| 655 | ObjPtr<DexCache> this_dex_cache = GetDexCache(); |
| 656 | if (this_dex_cache == dex_cache) { |
| 657 | // Lookup is always performed in the class referenced by the MethodId. |
| 658 | DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_); |
| 659 | for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { |
| 660 | if (method.GetDexMethodIndex() == dex_method_idx) { |
| 661 | return &method; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | // If not found, we need to search by name and signature. |
| 666 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 667 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 668 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 669 | std::string_view name; // Delay strlen() until actually needed. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 670 | // If we do not have a dex_cache match, try to find the declared method in this class now. |
| 671 | if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) { |
| 672 | DCHECK(name.empty()); |
David Srbecky | 39d8c87 | 2018-11-01 16:44:20 +0000 | [diff] [blame] | 673 | // Avoid string comparisons by comparing the respective unicode lengths first. |
| 674 | uint32_t length, other_length; // UTF16 length. |
| 675 | name = dex_file.GetMethodName(method_id, &length); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 676 | for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { |
David Srbecky | 39d8c87 | 2018-11-01 16:44:20 +0000 | [diff] [blame] | 677 | DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex); |
| 678 | const char* other_name = method.GetDexFile()->GetMethodName( |
| 679 | method.GetDexMethodIndex(), &other_length); |
| 680 | if (length == other_length && name == other_name && signature == method.GetSignature()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 681 | return &method; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // Then search the superclass chain. If we find an inherited method, return it. |
| 687 | // If we find a method that's not inherited because of access restrictions, |
| 688 | // try to find a method inherited from an interface in copied methods. |
| 689 | ArtMethod* uninherited_method = nullptr; |
| 690 | ObjPtr<Class> klass = GetSuperClass(); |
| 691 | for (; klass != nullptr; klass = klass->GetSuperClass()) { |
| 692 | ArtMethod* candidate_method = nullptr; |
| 693 | ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size); |
| 694 | if (klass->GetDexCache() == dex_cache) { |
| 695 | // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because |
| 696 | // the type index differs, so compare the name index and proto index. |
| 697 | for (ArtMethod& method : declared_methods) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 698 | const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 699 | if (cmp_method_id.name_idx_ == method_id.name_idx_ && |
| 700 | cmp_method_id.proto_idx_ == method_id.proto_idx_) { |
| 701 | candidate_method = &method; |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | } else { |
| 706 | if (!declared_methods.empty() && name.empty()) { |
| 707 | name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 708 | } |
| 709 | for (ArtMethod& method : declared_methods) { |
| 710 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 711 | candidate_method = &method; |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | if (candidate_method != nullptr) { |
| 717 | if (IsInheritedMethod(this, klass, *candidate_method)) { |
| 718 | return candidate_method; |
| 719 | } else { |
| 720 | uninherited_method = candidate_method; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Then search copied methods. |
| 727 | // If we found a method that's not inherited, stop the search in its declaring class. |
| 728 | ObjPtr<Class> end_klass = klass; |
| 729 | DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); |
| 730 | // After we have searched the declared methods of the super-class chain, |
| 731 | // search copied methods which can contain methods from interfaces. |
| 732 | for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) { |
| 733 | ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size); |
| 734 | if (!copied_methods.empty() && name.empty()) { |
| 735 | name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 736 | } |
| 737 | for (ArtMethod& method : copied_methods) { |
| 738 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 739 | return &method; // No further check needed, copied methods are inherited by definition. |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | return uninherited_method; // Return the `uninherited_method` if any. |
| 744 | } |
| 745 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 746 | ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 747 | // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy(). |
| 748 | DCHECK(!IsProxyClass()); |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 749 | std::string_view name("<init>"); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 750 | for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) { |
Vladimir Marko | eb37ba5 | 2019-02-05 14:10:38 +0000 | [diff] [blame] | 751 | if (method.GetName() == name && method.GetSignature() == signature) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 752 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 753 | } |
| 754 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 755 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 758 | ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 759 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 760 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 761 | if (name == np_method->GetName()) { |
| 762 | return &method; |
| 763 | } |
| 764 | } |
| 765 | return nullptr; |
| 766 | } |
| 767 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 768 | ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 769 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 770 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 771 | if (name == np_method->GetName()) { |
| 772 | return &method; |
| 773 | } |
| 774 | } |
| 775 | return nullptr; |
| 776 | } |
| 777 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 778 | ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 779 | DCHECK(method->GetDeclaringClass()->IsInterface()); |
| 780 | DCHECK(IsInterface()) << "Should only be called on a interface class"; |
| 781 | // Check if we have one defined on this interface first. This includes searching copied ones to |
| 782 | // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We |
| 783 | // don't do any indirect method checks here. |
| 784 | for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) { |
| 785 | if (method->HasSameNameAndSignature(&iface_method)) { |
| 786 | return &iface_method; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | std::vector<ArtMethod*> abstract_methods; |
| 791 | // Search through the IFTable for a working version. We don't need to check for conflicts |
| 792 | // because if there was one it would appear in this classes virtual_methods_ above. |
| 793 | |
| 794 | Thread* self = Thread::Current(); |
| 795 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 796 | MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable())); |
| 797 | MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 798 | size_t iftable_count = GetIfTableCount(); |
| 799 | // Find the method. We don't need to check for conflicts because they would have been in the |
| 800 | // copied virtuals of this interface. Order matters, traverse in reverse topological order; most |
| 801 | // subtypiest interfaces get visited first. |
| 802 | for (size_t k = iftable_count; k != 0;) { |
| 803 | k--; |
| 804 | DCHECK_LT(k, iftable->Count()); |
| 805 | iface.Assign(iftable->GetInterface(k)); |
| 806 | // Iterate through every declared method on this interface. Each direct method's name/signature |
| 807 | // is unique so the order of the inner loop doesn't matter. |
| 808 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) { |
| 809 | ArtMethod* current_method = &method_iter; |
| 810 | if (current_method->HasSameNameAndSignature(method)) { |
| 811 | if (current_method->IsDefault()) { |
| 812 | // Handle JLS soft errors, a default method from another superinterface tree can |
| 813 | // "override" an abstract method(s) from another superinterface tree(s). To do this, |
| 814 | // ignore any [default] method which are dominated by the abstract methods we've seen so |
| 815 | // far. Check if overridden by any in abstract_methods. We do not need to check for |
| 816 | // default_conflicts because we would hit those before we get to this loop. |
| 817 | bool overridden = false; |
| 818 | for (ArtMethod* possible_override : abstract_methods) { |
| 819 | DCHECK(possible_override->HasSameNameAndSignature(current_method)); |
| 820 | if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) { |
| 821 | overridden = true; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | if (!overridden) { |
| 826 | return current_method; |
| 827 | } |
| 828 | } else { |
| 829 | // Is not default. |
| 830 | // This might override another default method. Just stash it for now. |
| 831 | abstract_methods.push_back(current_method); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | // If we reach here we either never found any declaration of the method (in which case |
| 837 | // 'abstract_methods' is empty or we found no non-overriden default methods in which case |
| 838 | // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one |
| 839 | // of these arbitrarily. |
| 840 | return abstract_methods.empty() ? nullptr : abstract_methods[0]; |
| 841 | } |
| 842 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 843 | ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 844 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
| 845 | if (method.IsClassInitializer()) { |
| 846 | DCHECK_STREQ(method.GetName(), "<clinit>"); |
| 847 | DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); |
| 848 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 851 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 854 | // Custom binary search to avoid double comparisons from std::binary_search. |
| 855 | static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 856 | std::string_view name, |
| 857 | std::string_view type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 858 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 859 | if (fields == nullptr) { |
| 860 | return nullptr; |
| 861 | } |
| 862 | size_t low = 0; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 863 | size_t high = fields->size(); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 864 | ArtField* ret = nullptr; |
| 865 | while (low < high) { |
| 866 | size_t mid = (low + high) / 2; |
| 867 | ArtField& field = fields->At(mid); |
| 868 | // Fields are sorted by class, then name, then type descriptor. This is verified in dex file |
| 869 | // verifier. There can be multiple fields with the same in the same class name due to proguard. |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 870 | // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as |
| 871 | // unsigned; for modified-UTF-8 without embedded nulls this is consistent with the |
| 872 | // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering. |
| 873 | int result = std::string_view(field.GetName()).compare(name); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 874 | if (result == 0) { |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 875 | result = std::string_view(field.GetTypeDescriptor()).compare(type); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 876 | } |
| 877 | if (result < 0) { |
| 878 | low = mid + 1; |
| 879 | } else if (result > 0) { |
| 880 | high = mid; |
| 881 | } else { |
| 882 | ret = &field; |
| 883 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 886 | if (kIsDebugBuild) { |
| 887 | ArtField* found = nullptr; |
| 888 | for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) { |
| 889 | if (name == field.GetName() && type == field.GetTypeDescriptor()) { |
| 890 | found = &field; |
| 891 | break; |
| 892 | } |
| 893 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 894 | CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField(); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 895 | } |
| 896 | return ret; |
| 897 | } |
| 898 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 899 | ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 900 | // Binary search by name. Interfaces are not relevant because they can't contain instance fields. |
| 901 | return FindFieldByNameAndType(GetIFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 902 | } |
| 903 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 904 | ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 905 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 906 | for (ArtField& field : GetIFields()) { |
| 907 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 908 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 912 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 915 | ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 916 | // Is the field in this class, or any of its superclasses? |
| 917 | // Interfaces are not relevant because they can't contain instance fields. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 918 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 919 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 920 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 921 | return f; |
| 922 | } |
| 923 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 924 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 927 | ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 928 | // Is the field in this class, or any of its superclasses? |
| 929 | // Interfaces are not relevant because they can't contain instance fields. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 930 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 931 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 932 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 933 | return f; |
| 934 | } |
| 935 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 936 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 937 | } |
| 938 | |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 939 | ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) { |
| 940 | DCHECK(!type.empty()); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 941 | return FindFieldByNameAndType(GetSFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 944 | ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 945 | if (dex_cache == GetDexCache()) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 946 | for (ArtField& field : GetSFields()) { |
| 947 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 948 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 952 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 955 | ArtField* Class::FindStaticField(Thread* self, |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 956 | ObjPtr<Class> klass, |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 957 | std::string_view name, |
| 958 | std::string_view type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 959 | // Is the field in this class (or its interfaces), or any of its |
| 960 | // superclasses (or their interfaces)? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 961 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 962 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 963 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 964 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 965 | return f; |
| 966 | } |
| 967 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 968 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 969 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 970 | DCHECK(interface != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 971 | f = FindStaticField(self, interface, name, type); |
| 972 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 973 | return f; |
| 974 | } |
| 975 | } |
| 976 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 977 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Vladimir Marko | bb268b1 | 2016-06-30 15:52:56 +0100 | [diff] [blame] | 980 | ArtField* Class::FindStaticField(Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 981 | ObjPtr<Class> klass, |
| 982 | ObjPtr<DexCache> dex_cache, |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 983 | uint32_t dex_field_idx) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 984 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 985 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 986 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 987 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 988 | return f; |
| 989 | } |
Vladimir Marko | bb268b1 | 2016-06-30 15:52:56 +0100 | [diff] [blame] | 990 | // Though GetDirectInterface() should not cause thread suspension when called |
| 991 | // from here, it takes a Handle as an argument, so we need to wrap `k`. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 992 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 993 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 994 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 995 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 996 | DCHECK(interface != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 997 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 998 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 999 | return f; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1003 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1006 | ArtField* Class::FindField(Thread* self, |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1007 | ObjPtr<Class> klass, |
Vladimir Marko | e027d72 | 2019-02-05 10:13:49 +0000 | [diff] [blame] | 1008 | std::string_view name, |
| 1009 | std::string_view type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1010 | // Find a field using the JLS field resolution order |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1011 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1012 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 1013 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1014 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1015 | return f; |
| 1016 | } |
| 1017 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1018 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1019 | return f; |
| 1020 | } |
| 1021 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1022 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 1023 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 1024 | DCHECK(interface != nullptr); |
| 1025 | f = FindStaticField(self, interface, name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1026 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1027 | return f; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1031 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1034 | void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1035 | DCHECK(IsVerified()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1036 | for (auto& m : GetMethods(pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1037 | if (!m.IsNative() && m.IsInvokable()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 1038 | m.SetSkipAccessChecks(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 1041 | } |
| 1042 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1043 | const char* Class::GetDescriptor(std::string* storage) { |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 1044 | size_t dim = 0u; |
| 1045 | ObjPtr<mirror::Class> klass = this; |
| 1046 | while (klass->IsArrayClass()) { |
| 1047 | ++dim; |
Vladimir Marko | d355acf | 2019-03-21 17:09:40 +0000 | [diff] [blame] | 1048 | // No read barrier needed, we're reading a chain of constant references for comparison |
| 1049 | // with null. Then we follow up below with reading constant references to read constant |
| 1050 | // primitive data in both proxy and non-proxy paths. See ReadBarrierOption. |
| 1051 | klass = klass->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1052 | } |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 1053 | if (klass->IsProxyClass()) { |
| 1054 | // No read barrier needed, the `name` field is constant for proxy classes and |
| 1055 | // the contents of the String are also constant. See ReadBarrierOption. |
| 1056 | ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>(); |
| 1057 | DCHECK(name != nullptr); |
| 1058 | *storage = DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 1059 | } else { |
| 1060 | const char* descriptor; |
| 1061 | if (klass->IsPrimitive()) { |
| 1062 | descriptor = Primitive::Descriptor(klass->GetPrimitiveType()); |
| 1063 | } else { |
| 1064 | const DexFile& dex_file = klass->GetDexFile(); |
| 1065 | const dex::TypeId& type_id = dex_file.GetTypeId(klass->GetDexTypeIndex()); |
| 1066 | descriptor = dex_file.GetTypeDescriptor(type_id); |
| 1067 | } |
| 1068 | if (dim == 0) { |
| 1069 | return descriptor; |
| 1070 | } |
| 1071 | *storage = descriptor; |
| 1072 | } |
| 1073 | storage->insert(0u, dim, '['); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1074 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1077 | const dex::ClassDef* Class::GetClassDef() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1078 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 1079 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 1080 | return nullptr; |
| 1081 | } |
| 1082 | return &GetDexFile().GetClassDef(class_def_idx); |
| 1083 | } |
| 1084 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1085 | dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1086 | DCHECK(!IsPrimitive()); |
| 1087 | DCHECK(!IsArrayClass()); |
| 1088 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 1089 | } |
| 1090 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1091 | ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) { |
| 1092 | DCHECK(klass != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1093 | DCHECK(!klass->IsPrimitive()); |
| 1094 | if (klass->IsArrayClass()) { |
| 1095 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1096 | // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass(). |
| 1097 | ObjPtr<Class> interface; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1098 | if (idx == 0) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1099 | interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1100 | } else { |
| 1101 | DCHECK_EQ(1U, idx); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1102 | interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1103 | } |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1104 | DCHECK(interface != nullptr); |
| 1105 | return interface; |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1106 | } else if (klass->IsProxyClass()) { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 1107 | ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1108 | DCHECK(interfaces != nullptr); |
| 1109 | return interfaces->Get(idx); |
| 1110 | } else { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1111 | dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1112 | ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType( |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1113 | type_idx, klass->GetDexCache(), klass->GetClassLoader()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1114 | return interface; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1118 | ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) { |
| 1119 | ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx); |
| 1120 | if (interface == nullptr) { |
| 1121 | DCHECK(!klass->IsArrayClass()); |
| 1122 | DCHECK(!klass->IsProxyClass()); |
| 1123 | dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1124 | interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get()); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1125 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 1126 | } |
| 1127 | return interface; |
| 1128 | } |
| 1129 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1130 | ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1131 | DCHECK(klass != nullptr); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1132 | DCHECK(!klass->IsInterface()); |
| 1133 | DCHECK(!IsInterface()); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1134 | ObjPtr<Class> common_super_class = this; |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1135 | while (!common_super_class->IsAssignableFrom(klass.Get())) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1136 | ObjPtr<Class> old_common = common_super_class; |
Aart Bik | 22deed0 | 2016-04-04 14:19:01 -0700 | [diff] [blame] | 1137 | common_super_class = old_common->GetSuperClass(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1138 | DCHECK(common_super_class != nullptr) << old_common->PrettyClass(); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1139 | } |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1140 | return common_super_class; |
| 1141 | } |
| 1142 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1143 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1144 | const DexFile& dex_file = GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1145 | const dex::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1146 | if (dex_class_def == nullptr) { |
| 1147 | // Generated classes have no class def. |
| 1148 | return nullptr; |
| 1149 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1150 | return dex_file.GetSourceFile(*dex_class_def); |
| 1151 | } |
| 1152 | |
| 1153 | std::string Class::GetLocation() { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1154 | ObjPtr<DexCache> dex_cache = GetDexCache(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1155 | if (dex_cache != nullptr && !IsProxyClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1156 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 1157 | } |
| 1158 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 1159 | return "generated class"; |
| 1160 | } |
| 1161 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1162 | const dex::TypeList* Class::GetInterfaceTypeList() { |
| 1163 | const dex::ClassDef* class_def = GetClassDef(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1164 | if (class_def == nullptr) { |
| 1165 | return nullptr; |
| 1166 | } |
| 1167 | return GetDexFile().GetInterfacesList(*class_def); |
| 1168 | } |
| 1169 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1170 | void Class::PopulateEmbeddedVTable(PointerSize pointer_size) { |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1171 | ObjPtr<PointerArray> table = GetVTableDuringLinking(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1172 | CHECK(table != nullptr) << PrettyClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1173 | const size_t table_length = table->GetLength(); |
| 1174 | SetEmbeddedVTableLength(table_length); |
| 1175 | for (size_t i = 0; i < table_length; i++) { |
| 1176 | SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1177 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 1178 | // Keep java.lang.Object class's vtable around for since it's easier |
| 1179 | // to be reused by array classes during their linking. |
| 1180 | if (!IsObjectClass()) { |
| 1181 | SetVTable(nullptr); |
| 1182 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1185 | class ReadBarrierOnNativeRootsVisitor { |
| 1186 | public: |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1187 | void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED, |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1188 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 1189 | bool is_static ATTRIBUTE_UNUSED) const {} |
| 1190 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1191 | void VisitRootIfNonNull(CompressedReference<Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1192 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1193 | if (!root->IsNull()) { |
| 1194 | VisitRoot(root); |
| 1195 | } |
| 1196 | } |
| 1197 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1198 | void VisitRoot(CompressedReference<Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1199 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1200 | ObjPtr<Object> old_ref = root->AsMirrorPtr(); |
| 1201 | ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1202 | if (old_ref != new_ref) { |
| 1203 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 1204 | auto* atomic_root = |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1205 | reinterpret_cast<Atomic<CompressedReference<Object>>*>(root); |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 1206 | atomic_root->CompareAndSetStrongSequentiallyConsistent( |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1207 | CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()), |
| 1208 | CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr())); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | }; |
| 1212 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1213 | // The pre-fence visitor for Class::CopyOf(). |
| 1214 | class CopyClassVisitor { |
| 1215 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1216 | CopyClassVisitor(Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1217 | Handle<Class>* orig, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1218 | size_t new_length, |
| 1219 | size_t copy_bytes, |
| 1220 | ImTable* imt, |
| 1221 | PointerSize pointer_size) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1222 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1223 | copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1226 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1227 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1228 | StackHandleScope<1> hs(self_); |
| 1229 | Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1230 | Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 1231 | Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1232 | h_new_class_obj->PopulateEmbeddedVTable(pointer_size_); |
| 1233 | h_new_class_obj->SetImt(imt_, pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1234 | h_new_class_obj->SetClassSize(new_length_); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1235 | // Visit all of the references to make sure there is no from space references in the native |
| 1236 | // roots. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1237 | h_new_class_obj->Object::VisitReferences(ReadBarrierOnNativeRootsVisitor(), VoidFunctor()); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | private: |
| 1241 | Thread* const self_; |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1242 | Handle<Class>* const orig_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1243 | const size_t new_length_; |
| 1244 | const size_t copy_bytes_; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1245 | ImTable* imt_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1246 | const PointerSize pointer_size_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1247 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 1248 | }; |
| 1249 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 1250 | ObjPtr<Class> Class::CopyOf(Handle<Class> h_this, |
| 1251 | Thread* self, |
| 1252 | int32_t new_length, |
| 1253 | ImTable* imt, |
| 1254 | PointerSize pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1255 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 1256 | // We may get copied by a compacting GC. |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 1257 | Runtime* runtime = Runtime::Current(); |
| 1258 | gc::Heap* heap = runtime->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1259 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 1260 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1261 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 1262 | ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker()); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1263 | ObjPtr<Object> new_class = kMovingClasses ? |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 1264 | heap->AllocObject(self, java_lang_Class, new_length, visitor) : |
| 1265 | heap->AllocNonMovableObject(self, java_lang_Class, new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1266 | if (UNLIKELY(new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1267 | self->AssertPendingOOMException(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1268 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1269 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1270 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1273 | bool Class::ProxyDescriptorEquals(const char* match) { |
| 1274 | DCHECK(IsProxyClass()); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 1275 | std::string storage; |
| 1276 | const char* descriptor = GetDescriptor(&storage); |
| 1277 | DCHECK(descriptor == storage.c_str()); |
| 1278 | return storage == match; |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1281 | // TODO: Move this to java_lang_Class.cc? |
| 1282 | ArtMethod* Class::GetDeclaredConstructor( |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1283 | Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) { |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1284 | for (auto& m : GetDirectMethods(pointer_size)) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1285 | // Skip <clinit> which is a static constructor, as well as non constructors. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1286 | if (m.IsStatic() || !m.IsConstructor()) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1287 | continue; |
| 1288 | } |
| 1289 | // May cause thread suspension and exceptions. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1290 | if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1291 | return &m; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1292 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1293 | if (UNLIKELY(self->IsExceptionPending())) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1294 | return nullptr; |
| 1295 | } |
| 1296 | } |
| 1297 | return nullptr; |
| 1298 | } |
| 1299 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1300 | uint32_t Class::Depth() { |
| 1301 | uint32_t depth = 0; |
Roland Levillain | d32ead2 | 2018-05-30 17:38:21 +0100 | [diff] [blame] | 1302 | for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1303 | depth++; |
| 1304 | } |
| 1305 | return depth; |
| 1306 | } |
| 1307 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1308 | dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 1309 | std::string temp; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1310 | const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp)); |
Andreas Gampe | 2722f38 | 2017-06-08 18:03:25 -0700 | [diff] [blame] | 1311 | return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1314 | ALWAYS_INLINE |
| 1315 | static bool IsMethodPreferredOver(ArtMethod* orig_method, |
| 1316 | bool orig_method_hidden, |
| 1317 | ArtMethod* new_method, |
| 1318 | bool new_method_hidden) { |
| 1319 | DCHECK(new_method != nullptr); |
| 1320 | |
| 1321 | // Is this the first result? |
| 1322 | if (orig_method == nullptr) { |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | // Original method is hidden, the new one is not? |
| 1327 | if (orig_method_hidden && !new_method_hidden) { |
| 1328 | return true; |
| 1329 | } |
| 1330 | |
| 1331 | // We iterate over virtual methods first and then over direct ones, |
| 1332 | // so we can never be in situation where `orig_method` is direct and |
| 1333 | // `new_method` is virtual. |
| 1334 | DCHECK(!orig_method->IsDirect() || new_method->IsDirect()); |
| 1335 | |
| 1336 | // Original method is synthetic, the new one is not? |
| 1337 | if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) { |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1344 | template <PointerSize kPointerSize, bool kTransactionActive> |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1345 | ObjPtr<Method> Class::GetDeclaredMethodInternal( |
| 1346 | Thread* self, |
| 1347 | ObjPtr<Class> klass, |
| 1348 | ObjPtr<String> name, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1349 | ObjPtr<ObjectArray<Class>> args, |
| 1350 | const std::function<hiddenapi::AccessContext()>& fn_get_access_context) { |
| 1351 | // Covariant return types (or smali) permit the class to define |
| 1352 | // multiple methods with the same name and parameter types. |
| 1353 | // Prefer (in decreasing order of importance): |
| 1354 | // 1) non-hidden method over hidden |
| 1355 | // 2) virtual methods over direct |
| 1356 | // 3) non-synthetic methods over synthetic |
| 1357 | // We never return miranda methods that were synthesized by the runtime. |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1358 | StackHandleScope<3> hs(self); |
| 1359 | auto h_method_name = hs.NewHandle(name); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1360 | if (UNLIKELY(h_method_name == nullptr)) { |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1361 | ThrowNullPointerException("name == null"); |
| 1362 | return nullptr; |
| 1363 | } |
| 1364 | auto h_args = hs.NewHandle(args); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1365 | Handle<Class> h_klass = hs.NewHandle(klass); |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1366 | constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone; |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1367 | ArtMethod* result = nullptr; |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1368 | bool result_hidden = false; |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1369 | for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) { |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1370 | if (m.IsMiranda()) { |
| 1371 | continue; |
| 1372 | } |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1373 | auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1374 | // May cause thread suspension. |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 1375 | ObjPtr<String> np_name = np_method->ResolveNameString(); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1376 | if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { |
| 1377 | if (UNLIKELY(self->IsExceptionPending())) { |
| 1378 | return nullptr; |
| 1379 | } |
| 1380 | continue; |
| 1381 | } |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1382 | bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method); |
| 1383 | if (!m_hidden && !m.IsSynthetic()) { |
| 1384 | // Non-hidden, virtual, non-synthetic. Best possible result, exit early. |
| 1385 | return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); |
| 1386 | } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) { |
| 1387 | // Remember as potential result. |
| 1388 | result = &m; |
| 1389 | result_hidden = m_hidden; |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1390 | } |
| 1391 | } |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1392 | |
| 1393 | if ((result != nullptr) && !result_hidden) { |
| 1394 | // We have not found a non-hidden, virtual, non-synthetic method, but |
| 1395 | // if we have found a non-hidden, virtual, synthetic method, we cannot |
| 1396 | // do better than that later. |
| 1397 | DCHECK(!result->IsDirect()); |
| 1398 | DCHECK(result->IsSynthetic()); |
| 1399 | } else { |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1400 | for (auto& m : h_klass->GetDirectMethods(kPointerSize)) { |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1401 | auto modifiers = m.GetAccessFlags(); |
| 1402 | if ((modifiers & kAccConstructor) != 0) { |
| 1403 | continue; |
| 1404 | } |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1405 | auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1406 | // May cause thread suspension. |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 1407 | ObjPtr<String> np_name = np_method->ResolveNameString(); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1408 | if (np_name == nullptr) { |
| 1409 | self->AssertPendingException(); |
| 1410 | return nullptr; |
| 1411 | } |
| 1412 | if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { |
| 1413 | if (UNLIKELY(self->IsExceptionPending())) { |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | continue; |
| 1417 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1418 | DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods. |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1419 | bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method); |
| 1420 | if (!m_hidden && !m.IsSynthetic()) { |
| 1421 | // Non-hidden, direct, non-synthetic. Any virtual result could only have been |
| 1422 | // hidden, therefore this is the best possible match. Exit now. |
| 1423 | DCHECK((result == nullptr) || result_hidden); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1424 | return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1425 | } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) { |
| 1426 | // Remember as potential result. |
| 1427 | result = &m; |
| 1428 | result_hidden = m_hidden; |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1429 | } |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1432 | |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1433 | return result != nullptr |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1434 | ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1435 | : nullptr; |
| 1436 | } |
| 1437 | |
| 1438 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1439 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1440 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1441 | ObjPtr<Class> klass, |
| 1442 | ObjPtr<String> name, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1443 | ObjPtr<ObjectArray<Class>> args, |
| 1444 | const std::function<hiddenapi::AccessContext()>& fn_get_access_context); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1445 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1446 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1447 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1448 | ObjPtr<Class> klass, |
| 1449 | ObjPtr<String> name, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1450 | ObjPtr<ObjectArray<Class>> args, |
| 1451 | const std::function<hiddenapi::AccessContext()>& fn_get_access_context); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1452 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1453 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1454 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1455 | ObjPtr<Class> klass, |
| 1456 | ObjPtr<String> name, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1457 | ObjPtr<ObjectArray<Class>> args, |
| 1458 | const std::function<hiddenapi::AccessContext()>& fn_get_access_context); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1459 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1460 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1461 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1462 | ObjPtr<Class> klass, |
| 1463 | ObjPtr<String> name, |
David Brazdil | 4bcd657 | 2019-02-02 20:08:44 +0000 | [diff] [blame] | 1464 | ObjPtr<ObjectArray<Class>> args, |
| 1465 | const std::function<hiddenapi::AccessContext()>& fn_get_access_context); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1466 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1467 | template <PointerSize kPointerSize, bool kTransactionActive> |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1468 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1469 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1470 | ObjPtr<Class> klass, |
| 1471 | ObjPtr<ObjectArray<Class>> args) { |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1472 | StackHandleScope<1> hs(self); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1473 | ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize); |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1474 | return result != nullptr |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1475 | ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1476 | : nullptr; |
| 1477 | } |
| 1478 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1479 | // Constructor::CreateFromArtMethod<kTransactionActive>(self, result) |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1480 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1481 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1482 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1483 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1484 | ObjPtr<Class> klass, |
| 1485 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1486 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1487 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1488 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1489 | ObjPtr<Class> klass, |
| 1490 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1491 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1492 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1493 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1494 | ObjPtr<Class> klass, |
| 1495 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1496 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1497 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1498 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1499 | ObjPtr<Class> klass, |
| 1500 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1501 | |
Andreas Gampe | 715fdc2 | 2016-04-18 17:07:30 -0700 | [diff] [blame] | 1502 | int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) { |
| 1503 | if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) { |
| 1504 | return default_value; |
| 1505 | } |
| 1506 | uint32_t flags; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1507 | if (!annotations::GetInnerClassFlags(h_this, &flags)) { |
Andreas Gampe | 715fdc2 | 2016-04-18 17:07:30 -0700 | [diff] [blame] | 1508 | return default_value; |
| 1509 | } |
| 1510 | return flags; |
| 1511 | } |
| 1512 | |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 1513 | void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) { |
| 1514 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1515 | SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size); |
| 1516 | } else { |
| 1517 | SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size); |
| 1518 | } |
| 1519 | } |
| 1520 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1521 | std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) { |
| 1522 | if (klass == nullptr) { |
| 1523 | return "null"; |
| 1524 | } |
| 1525 | return klass->PrettyDescriptor(); |
| 1526 | } |
| 1527 | |
| 1528 | std::string Class::PrettyDescriptor() { |
| 1529 | std::string temp; |
| 1530 | return art::PrettyDescriptor(GetDescriptor(&temp)); |
| 1531 | } |
| 1532 | |
| 1533 | std::string Class::PrettyClass(ObjPtr<mirror::Class> c) { |
| 1534 | if (c == nullptr) { |
| 1535 | return "null"; |
| 1536 | } |
| 1537 | return c->PrettyClass(); |
| 1538 | } |
| 1539 | |
| 1540 | std::string Class::PrettyClass() { |
| 1541 | std::string result; |
| 1542 | result += "java.lang.Class<"; |
| 1543 | result += PrettyDescriptor(); |
| 1544 | result += ">"; |
| 1545 | return result; |
| 1546 | } |
| 1547 | |
| 1548 | std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) { |
| 1549 | if (c == nullptr) { |
| 1550 | return "null"; |
| 1551 | } |
| 1552 | return c->PrettyClassAndClassLoader(); |
| 1553 | } |
| 1554 | |
| 1555 | std::string Class::PrettyClassAndClassLoader() { |
| 1556 | std::string result; |
| 1557 | result += "java.lang.Class<"; |
| 1558 | result += PrettyDescriptor(); |
| 1559 | result += ","; |
| 1560 | result += mirror::Object::PrettyTypeOf(GetClassLoader()); |
| 1561 | // TODO: add an identifying hash value for the loader |
| 1562 | result += ">"; |
| 1563 | return result; |
| 1564 | } |
| 1565 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1566 | template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() { |
| 1567 | // Check class is loaded/retired or this is java.lang.String that has a |
| 1568 | // circularity issue during loading the names of its members |
| 1569 | DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || |
| 1570 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1571 | this == GetClassRoot<String>()) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1572 | << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() |
| 1573 | << " IsRetired=" << IsRetired<kVerifyFlags>() |
| 1574 | << " IsErroneous=" << |
| 1575 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1576 | << " IsString=" << (this == GetClassRoot<String>()) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1577 | << " status= " << GetStatus<kVerifyFlags>() |
| 1578 | << " descriptor=" << PrettyDescriptor(); |
| 1579 | } |
| 1580 | // Instantiate the common cases. |
| 1581 | template void Class::GetAccessFlagsDCheck<kVerifyNone>(); |
| 1582 | template void Class::GetAccessFlagsDCheck<kVerifyThis>(); |
| 1583 | template void Class::GetAccessFlagsDCheck<kVerifyReads>(); |
| 1584 | template void Class::GetAccessFlagsDCheck<kVerifyWrites>(); |
| 1585 | template void Class::GetAccessFlagsDCheck<kVerifyAll>(); |
| 1586 | |
Andreas Gampe | 62f6e90 | 2018-10-11 18:58:50 -0700 | [diff] [blame] | 1587 | void Class::SetAccessFlagsDCheck(uint32_t new_access_flags) { |
| 1588 | uint32_t old_access_flags = GetField32<kVerifyNone>(AccessFlagsOffset()); |
| 1589 | // kAccVerificationAttempted is retained. |
| 1590 | CHECK((old_access_flags & kAccVerificationAttempted) == 0 || |
| 1591 | (new_access_flags & kAccVerificationAttempted) != 0); |
| 1592 | } |
| 1593 | |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1594 | ObjPtr<PointerArray> Class::GetMethodIds() { |
| 1595 | ObjPtr<ClassExt> ext(GetExtData()); |
| 1596 | if (ext.IsNull()) { |
| 1597 | return nullptr; |
| 1598 | } else { |
| 1599 | return ext->GetJMethodIDs(); |
| 1600 | } |
| 1601 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1602 | ObjPtr<PointerArray> Class::GetOrCreateMethodIds(Handle<Class> h_this) { |
Alex Light | f367747 | 2019-06-26 16:31:53 -0700 | [diff] [blame] | 1603 | DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!"; |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1604 | Thread* self = Thread::Current(); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1605 | ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self)); |
| 1606 | if (ext.IsNull()) { |
| 1607 | self->AssertPendingOOMException(); |
| 1608 | return nullptr; |
| 1609 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1610 | return ext->EnsureJMethodIDsArrayPresent(h_this->NumMethods()); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | ObjPtr<PointerArray> Class::GetStaticFieldIds() { |
| 1614 | ObjPtr<ClassExt> ext(GetExtData()); |
| 1615 | if (ext.IsNull()) { |
| 1616 | return nullptr; |
| 1617 | } else { |
| 1618 | return ext->GetStaticJFieldIDs(); |
| 1619 | } |
| 1620 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1621 | ObjPtr<PointerArray> Class::GetOrCreateStaticFieldIds(Handle<Class> h_this) { |
Alex Light | f367747 | 2019-06-26 16:31:53 -0700 | [diff] [blame] | 1622 | DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!"; |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1623 | Thread* self = Thread::Current(); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1624 | ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self)); |
| 1625 | if (ext.IsNull()) { |
| 1626 | self->AssertPendingOOMException(); |
| 1627 | return nullptr; |
| 1628 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1629 | return ext->EnsureStaticJFieldIDsArrayPresent(h_this->NumStaticFields()); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1630 | } |
| 1631 | ObjPtr<PointerArray> Class::GetInstanceFieldIds() { |
| 1632 | ObjPtr<ClassExt> ext(GetExtData()); |
| 1633 | if (ext.IsNull()) { |
| 1634 | return nullptr; |
| 1635 | } else { |
| 1636 | return ext->GetInstanceJFieldIDs(); |
| 1637 | } |
| 1638 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1639 | ObjPtr<PointerArray> Class::GetOrCreateInstanceFieldIds(Handle<Class> h_this) { |
Alex Light | f367747 | 2019-06-26 16:31:53 -0700 | [diff] [blame] | 1640 | DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!"; |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1641 | Thread* self = Thread::Current(); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1642 | ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self)); |
| 1643 | if (ext.IsNull()) { |
| 1644 | self->AssertPendingOOMException(); |
| 1645 | return nullptr; |
| 1646 | } |
Alex Light | bb5b4f3 | 2019-07-09 02:31:48 -0700 | [diff] [blame] | 1647 | return ext->EnsureInstanceJFieldIDsArrayPresent(h_this->NumInstanceFields()); |
Alex Light | 79d6c80 | 2019-06-27 15:50:11 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | size_t Class::GetStaticFieldIdOffset(ArtField* field) { |
| 1651 | DCHECK_LT(reinterpret_cast<uintptr_t>(field), |
| 1652 | reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->end())) |
| 1653 | << "field not part of the current class. " << field->PrettyField() << " class is " |
| 1654 | << PrettyClass(); |
| 1655 | DCHECK_GE(reinterpret_cast<uintptr_t>(field), |
| 1656 | reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->begin())) |
| 1657 | << "field not part of the current class. " << field->PrettyField() << " class is " |
| 1658 | << PrettyClass(); |
| 1659 | uintptr_t start = reinterpret_cast<uintptr_t>(&GetSFieldsPtr()->At(0)); |
| 1660 | uintptr_t fld = reinterpret_cast<uintptr_t>(field); |
| 1661 | size_t res = (fld - start) / sizeof(ArtField); |
| 1662 | DCHECK_EQ(&GetSFieldsPtr()->At(res), field) |
| 1663 | << "Incorrect field computation expected: " << field->PrettyField() |
| 1664 | << " got: " << GetSFieldsPtr()->At(res).PrettyField(); |
| 1665 | return res; |
| 1666 | } |
| 1667 | |
| 1668 | size_t Class::GetInstanceFieldIdOffset(ArtField* field) { |
| 1669 | DCHECK_LT(reinterpret_cast<uintptr_t>(field), |
| 1670 | reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->end())) |
| 1671 | << "field not part of the current class. " << field->PrettyField() << " class is " |
| 1672 | << PrettyClass(); |
| 1673 | DCHECK_GE(reinterpret_cast<uintptr_t>(field), |
| 1674 | reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->begin())) |
| 1675 | << "field not part of the current class. " << field->PrettyField() << " class is " |
| 1676 | << PrettyClass(); |
| 1677 | uintptr_t start = reinterpret_cast<uintptr_t>(&GetIFieldsPtr()->At(0)); |
| 1678 | uintptr_t fld = reinterpret_cast<uintptr_t>(field); |
| 1679 | size_t res = (fld - start) / sizeof(ArtField); |
| 1680 | DCHECK_EQ(&GetIFieldsPtr()->At(res), field) |
| 1681 | << "Incorrect field computation expected: " << field->PrettyField() |
| 1682 | << " got: " << GetIFieldsPtr()->At(res).PrettyField(); |
| 1683 | return res; |
| 1684 | } |
| 1685 | |
| 1686 | size_t Class::GetMethodIdOffset(ArtMethod* method, PointerSize pointer_size) { |
| 1687 | DCHECK(GetMethodsSlice(kRuntimePointerSize).Contains(method)) |
| 1688 | << "method not part of the current class. " << method->PrettyMethod() << "( " << reinterpret_cast<void*>(method) << ")" << " class is " |
| 1689 | << PrettyClass() << [&]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1690 | std::ostringstream os; |
| 1691 | os << " Methods are ["; |
| 1692 | for (ArtMethod& m : GetMethodsSlice(kRuntimePointerSize)) { |
| 1693 | os << m.PrettyMethod() << "( " << reinterpret_cast<void*>(&m) << "), "; |
| 1694 | } |
| 1695 | os << "]"; |
| 1696 | return os.str(); |
| 1697 | }(); |
| 1698 | uintptr_t start = reinterpret_cast<uintptr_t>(&*GetMethodsSlice(pointer_size).begin()); |
| 1699 | uintptr_t fld = reinterpret_cast<uintptr_t>(method); |
| 1700 | size_t art_method_size = ArtMethod::Size(pointer_size); |
| 1701 | size_t art_method_align = ArtMethod::Alignment(pointer_size); |
| 1702 | size_t res = (fld - start) / art_method_size; |
| 1703 | DCHECK_EQ(&GetMethodsPtr()->At(res, art_method_size, art_method_align), method) |
| 1704 | << "Incorrect method computation expected: " << method->PrettyMethod() |
| 1705 | << " got: " << GetMethodsPtr()->At(res, art_method_size, art_method_align).PrettyMethod(); |
| 1706 | return res; |
| 1707 | } |
| 1708 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1709 | } // namespace mirror |
| 1710 | } // namespace art |