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