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