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