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