blob: f4849b4015396868eb2d3f77644bc475cb313fd4 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Lightf1b18fa2019-11-11 14:22:00 -080019#include <unordered_set>
20#include <string_view>
21
Alex Light79d6c802019-06-27 15:50:11 +000022#include "android-base/macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Alex Light79d6c802019-06-27 15:50:11 +000025#include "array-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "art_field-inl.h"
27#include "art_method-inl.h"
Alex Light79d6c802019-06-27 15:50:11 +000028#include "base/enums.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080029#include "base/logging.h" // For VLOG.
David Sehrc431b9d2018-03-02 12:01:51 -080030#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "class-inl.h"
Alex Light79d6c802019-06-27 15:50:11 +000032#include "class_ext-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010033#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_loader.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010035#include "class_root-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080036#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/dex_file-inl.h"
38#include "dex/dex_file_annotations.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080039#include "dex/signature-inl.h"
Vladimir Marko58412b12019-04-01 13:26:34 +010040#include "dex_cache-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070041#include "gc/accounting/card_table-inl.h"
Andreas Gampee15b9b12018-10-29 12:54:27 -070042#include "gc/heap-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070043#include "handle_scope-inl.h"
David Brazdil4bcd6572019-02-02 20:08:44 +000044#include "hidden_api.h"
Alex Lightf3677472019-06-26 16:31:53 -070045#include "jni_id_type.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070046#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070047#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070048#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080049#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070051#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010052#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070053#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054#include "thread.h"
55#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056#include "well_known_classes.h"
57
58namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070059
60// TODO: move to own CC file?
61constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
62constexpr size_t BitString::kCapacity;
63
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064namespace mirror {
65
Andreas Gampe46ee31b2016-12-14 10:11:49 -080066using android::base::StringPrintf;
67
Alex Lightf1b18fa2019-11-11 14:22:00 -080068bool Class::IsMirrored() {
69 if (LIKELY(!IsBootStrapClassLoaded())) {
70 return false;
71 }
72 if (IsPrimitive() || IsArrayClass() || IsProxyClass()) {
73 return true;
74 }
Alex Lightf1b18fa2019-11-11 14:22:00 -080075 std::string name_storage;
Alex Light8f187c32021-04-20 14:29:00 -070076 const std::string_view name(this->GetDescriptor(&name_storage));
77 return IsMirroredDescriptor(name);
Alex Lightf1b18fa2019-11-11 14:22:00 -080078}
79
Vladimir Marko7287c4d2018-02-15 10:41:07 +000080ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
81 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010082 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000083 if (name != nullptr && name->GetLength() >= 2) {
84 // Perfect hash for the expected values: from the second letters of the primitive types,
85 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
86 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
87 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010088 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
89 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
90 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
91 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
92 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
93 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
94 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
95 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
96 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000097 default: break;
98 }
99 }
100 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +0100101 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000102 DCHECK(klass != nullptr);
103 return klass;
104 } else {
105 Thread* self = Thread::Current();
106 if (name == nullptr) {
107 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700108 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr);
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000109 } else {
110 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
111 }
112 return nullptr;
113 }
114}
115
Vladimir Marko3068d582019-05-28 16:39:29 +0100116ObjPtr<ClassExt> Class::EnsureExtDataPresent(Handle<Class> h_this, Thread* self) {
117 ObjPtr<ClassExt> existing(h_this->GetExtData());
Alex Light0273ad12016-11-02 11:19:31 -0700118 if (!existing.IsNull()) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000119 return existing;
Alex Light0273ad12016-11-02 11:19:31 -0700120 }
Vladimir Marko3068d582019-05-28 16:39:29 +0100121 StackHandleScope<2> hs(self);
Alex Light0273ad12016-11-02 11:19:31 -0700122 // Clear exception so we can allocate.
123 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
124 self->ClearException();
125 // Allocate the ClassExt
126 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800127 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700128 // OOM allocating the classExt.
129 // TODO Should we restore the suppressed exception?
130 self->AssertPendingOOMException();
131 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800132 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700133 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
134 bool set;
135 // Set the ext_data_ field using CAS semantics.
136 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700137 set = h_this->CasFieldObject<true>(ext_offset,
138 nullptr,
139 new_ext.Get(),
140 CASMode::kStrong,
141 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700142 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700143 set = h_this->CasFieldObject<false>(ext_offset,
144 nullptr,
145 new_ext.Get(),
146 CASMode::kStrong,
147 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700148 }
149 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
150 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
151 CHECK(!ret.IsNull());
152 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800153 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700154 self->SetException(throwable.Get());
155 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000156 return ret;
Andreas Gampe99babb62015-11-02 16:20:00 -0800157 }
158}
159
Alex Light270db1c2019-12-03 12:20:01 +0000160template <typename T>
161static void CheckSetStatus(Thread* self, T thiz, ClassStatus new_status, ClassStatus old_status)
162 REQUIRES_SHARED(Locks::mutator_lock_) {
163 if (UNLIKELY(new_status <= old_status && new_status != ClassStatus::kErrorUnresolved &&
164 new_status != ClassStatus::kErrorResolved && new_status != ClassStatus::kRetired)) {
165 LOG(FATAL) << "Unexpected change back of class status for " << thiz->PrettyClass() << " "
166 << old_status << " -> " << new_status;
167 }
168 if (old_status == ClassStatus::kInitialized) {
169 // We do not hold the lock for making the class visibly initialized
170 // as this is unnecessary and could lead to deadlocks.
171 CHECK_EQ(new_status, ClassStatus::kVisiblyInitialized);
172 } else if ((new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) &&
173 !Locks::mutator_lock_->IsExclusiveHeld(self)) {
174 // When classes are being resolved the resolution code should hold the
175 // lock or have everything else suspended
176 CHECK_EQ(thiz->GetLockOwnerThreadId(), self->GetThreadId())
177 << "Attempt to change status of class while not holding its lock: " << thiz->PrettyClass()
178 << " " << old_status << " -> " << new_status;
179 }
180 if (UNLIKELY(Locks::mutator_lock_->IsExclusiveHeld(self))) {
181 CHECK(!Class::IsErroneous(new_status))
182 << "status " << new_status
183 << " cannot be set while suspend-all is active. Would require allocations.";
184 CHECK(thiz->IsResolved())
185 << thiz->PrettyClass()
186 << " not resolved during suspend-all status change. Waiters might be missed!";
187 }
188}
189
Nicolas Geoffray1715efa2020-06-05 18:34:49 +0100190void Class::SetStatusInternal(ClassStatus new_status) {
Alex Light270db1c2019-12-03 12:20:01 +0000191 if (kBitstringSubtypeCheckEnabled) {
192 // FIXME: This looks broken with respect to aborted transactions.
193 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(this, new_status);
194 } else {
195 // The ClassStatus is always in the 4 most-significant bits of status_.
196 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
197 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
Nicolas Geoffray1715efa2020-06-05 18:34:49 +0100198 if (Runtime::Current()->IsActiveTransaction()) {
199 SetField32Volatile<true>(StatusOffset(), new_status_value);
200 } else {
201 SetField32Volatile<false>(StatusOffset(), new_status_value);
202 }
Alex Light270db1c2019-12-03 12:20:01 +0000203 }
204}
205
Nicolas Geoffray1715efa2020-06-05 18:34:49 +0100206void Class::SetStatusLocked(ClassStatus new_status) {
207 ClassStatus old_status = GetStatus();
208 CheckSetStatus(Thread::Current(), this, new_status, old_status);
209 SetStatusInternal(new_status);
210}
211
Vladimir Marko2c64a832018-01-04 11:31:56 +0000212void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
213 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700214 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
215 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700216 if (LIKELY(class_linker_initialized)) {
Alex Light270db1c2019-12-03 12:20:01 +0000217 CheckSetStatus(self, h_this, new_status, old_status);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000219 if (UNLIKELY(IsErroneous(new_status))) {
220 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700221 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000222 << h_this->PrettyClass()
223 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000224 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700225 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700226 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700227 if (self->IsExceptionPending()) {
228 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
229 }
230 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231
Vladimir Marko3068d582019-05-28 16:39:29 +0100232 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
Alex Light0273ad12016-11-02 11:19:31 -0700233 if (!ext.IsNull()) {
234 self->AssertPendingException();
Nicolas Geoffray4dc65892021-07-05 17:43:35 +0100235 ext->SetErroneousStateError(self->GetException());
Alex Light0273ad12016-11-02 11:19:31 -0700236 } else {
237 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700238 }
239 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
Alex Light0273ad12016-11-02 11:19:31 -0700241
Nicolas Geoffray1715efa2020-06-05 18:34:49 +0100242 h_this->SetStatusInternal(new_status);
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700243
244 // Setting the object size alloc fast path needs to be after the status write so that if the
245 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
246 // load-acquire/fake dependency.
Vladimir Marko8e110652019-07-30 10:14:41 +0100247 if (new_status == ClassStatus::kVisiblyInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700248 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
249 // Finalizable objects must always go slow path.
250 if (!h_this->IsFinalizable()) {
251 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700252 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100253 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254
Andreas Gampe5b20b352018-10-11 19:03:20 -0700255 if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) {
256 CHECK(h_this->WasVerificationAttempted()) << h_this->PrettyClassAndClassLoader();
257 }
258
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700259 if (!class_linker_initialized) {
260 // When the class linker is being initialized its single threaded and by definition there can be
261 // no waiters. During initialization classes may appear temporary but won't be retired as their
262 // size was statically computed.
263 } else {
264 // Classes that are being resolved or initialized need to notify waiters that the class status
265 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700266 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
268 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000269 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
270 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700271 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700272 }
Vladimir Marko422a9eb2019-08-01 12:54:07 +0100273 } else if (old_status == ClassStatus::kInitialized) {
274 // Do not notify for transition from kInitialized to ClassStatus::kVisiblyInitialized.
275 // This is a hidden transition, not observable by bytecode.
276 DCHECK_EQ(new_status, ClassStatus::kVisiblyInitialized); // Already CHECK()ed above.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000278 CHECK_NE(new_status, ClassStatus::kRetired);
279 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700280 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700281 }
282 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700283 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284}
285
Vladimir Marko70e2a762019-07-12 16:49:00 +0100286void Class::SetStatusForPrimitiveOrArray(ClassStatus new_status) {
287 DCHECK(IsPrimitive<kVerifyNone>() || IsArrayClass<kVerifyNone>());
288 DCHECK(!IsErroneous(new_status));
289 DCHECK(!IsErroneous(GetStatus<kVerifyNone>()));
290 DCHECK_GT(new_status, GetStatus<kVerifyNone>());
291
292 if (kBitstringSubtypeCheckEnabled) {
293 LOG(FATAL) << "Unimplemented";
294 }
295 // The ClassStatus is always in the 4 most-significant bits of status_.
296 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
297 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
298 // Use normal store. For primitives and core arrays classes (Object[],
299 // Class[], String[] and primitive arrays), the status is set while the
300 // process is still single threaded. For other arrays classes, it is set
301 // in a pre-fence visitor which initializes all fields and the subsequent
302 // fence together with address dependency shall ensure memory visibility.
303 SetField32</*kTransactionActive=*/ false,
304 /*kCheckTransaction=*/ false,
305 kVerifyNone>(StatusOffset(), new_status_value);
306
307 // Do not update `object_alloc_fast_path_`. Arrays are variable size and
308 // instances of primitive classes cannot be created at all.
309
310 if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) {
311 CHECK(WasVerificationAttempted()) << PrettyClassAndClassLoader();
312 }
313
314 // There can be no waiters to notify as these classes are initialized
315 // before another thread can see them.
316}
317
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700318void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700319 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320}
321
Ian Rogersef7d42f2014-01-06 12:55:46 -0800322void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700324 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
325 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700326 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700327 }
Vladimir Markob68bb7a2020-03-17 10:55:25 +0000328 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
329 OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330}
331
Alex Light270db1c2019-12-03 12:20:01 +0000332ObjPtr<Class> Class::GetObsoleteClass() {
333 ObjPtr<ClassExt> ext(GetExtData());
334 if (ext.IsNull()) {
335 return nullptr;
336 } else {
337 return ext->GetObsoleteClass();
338 }
339}
340
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341// Return the class' name. The exact format is bizarre, but it's the specified behavior for
342// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
343// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
344// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Vladimir Marko179b7c62019-03-22 13:38:57 +0000345ObjPtr<String> Class::ComputeName(Handle<Class> h_this) {
346 ObjPtr<String> name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800347 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 return name;
349 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700350 std::string temp;
351 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800352 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
354 // The descriptor indicates that this is the class for
355 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700356 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 switch (descriptor[0]) {
358 case 'Z': c_name = "boolean"; break;
359 case 'B': c_name = "byte"; break;
360 case 'C': c_name = "char"; break;
361 case 'S': c_name = "short"; break;
362 case 'I': c_name = "int"; break;
363 case 'J': c_name = "long"; break;
364 case 'F': c_name = "float"; break;
365 case 'D': c_name = "double"; break;
366 case 'V': c_name = "void"; break;
367 default:
368 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
369 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800370 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 } else {
372 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
373 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700374 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700376 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 return name;
378}
379
Ian Rogersef7d42f2014-01-06 12:55:46 -0800380void Class::DumpClass(std::ostream& os, int flags) {
Vladimir Markob10668c2021-06-10 09:52:53 +0100381 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700383 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 if ((flags & kDumpClassClassLoader) != 0) {
385 os << ' ' << GetClassLoader();
386 }
387 if ((flags & kDumpClassInitialized) != 0) {
388 os << ' ' << GetStatus();
389 }
390 os << "\n";
391 return;
392 }
393
Vladimir Markob10668c2021-06-10 09:52:53 +0100394 ObjPtr<Class> super = GetSuperClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700396
Ian Rogers1ff3c982014-08-12 02:30:58 -0700397 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Vladimir Markob10668c2021-06-10 09:52:53 +0100399 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n"
400 << " objectSize=" << SizeOf() << " "
401 << "(" << (super != nullptr ? super->SizeOf() : -1) << " from super)\n"
402 << StringPrintf(" access=0x%04x.%04x\n",
403 GetAccessFlags() >> 16,
404 GetAccessFlags() & kAccJavaFlagsMask);
405 if (super != nullptr) {
406 os << " super='" << super->PrettyClass() << "' (cl=" << super->GetClassLoader() << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 }
408 if (IsArrayClass()) {
409 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
410 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700411 const size_t num_direct_interfaces = NumDirectInterfaces();
412 if (num_direct_interfaces > 0) {
413 os << " interfaces (" << num_direct_interfaces << "):\n";
414 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +0100415 ObjPtr<Class> interface = GetDirectInterface(i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700416 if (interface == nullptr) {
417 os << StringPrintf(" %2zd: nullptr!\n", i);
418 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700419 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
420 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700421 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 }
423 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700424 if (!IsLoaded()) {
425 os << " class not yet loaded";
426 } else {
Vladimir Markob10668c2021-06-10 09:52:53 +0100427 os << " vtable (" << NumVirtualMethods() << " entries, "
428 << (super != nullptr ? super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700429 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700430 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Vladimir Markob10668c2021-06-10 09:52:53 +0100431 GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100433 os << " direct methods (" << NumDirectMethods() << " entries):\n";
434 for (size_t i = 0; i < NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700435 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Vladimir Markob10668c2021-06-10 09:52:53 +0100436 GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700437 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100438 if (NumStaticFields() > 0) {
439 os << " static fields (" << NumStaticFields() << " entries):\n";
440 if (IsResolved()) {
441 for (size_t i = 0; i < NumStaticFields(); ++i) {
442 os << StringPrintf(" %2zd: %s\n", i, ArtField::PrettyField(GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700443 }
444 } else {
445 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700447 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100448 if (NumInstanceFields() > 0) {
449 os << " instance fields (" << NumInstanceFields() << " entries):\n";
450 if (IsResolved()) {
451 for (size_t i = 0; i < NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700452 os << StringPrintf(" %2zd: %s\n", i,
Vladimir Markob10668c2021-06-10 09:52:53 +0100453 ArtField::PrettyField(GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700454 }
455 } else {
456 os << " <not yet available>";
457 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458 }
459 }
460}
461
462void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
David Srbecky346fd962020-07-27 16:51:00 +0100464 // Check that the number of bits set in the reference offset bitmap
465 // agrees with the number of references.
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700466 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700467 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 count += c->NumReferenceInstanceFieldsDuringLinking();
469 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700470 // +1 for the Class in Object.
471 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100473 // Not called within a transaction.
474 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700475 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476}
477
Vladimir Markoe027d722019-02-05 10:13:49 +0000478bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700480 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
481 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 ++i;
483 }
Vladimir Markoe027d722019-02-05 10:13:49 +0000484 if (descriptor1.find('/', i) != std::string_view::npos ||
485 descriptor2.find('/', i) != std::string_view::npos) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 return false;
487 } else {
488 return true;
489 }
490}
491
Mathieu Chartier3398c782016-09-30 10:27:43 -0700492bool Class::IsInSamePackage(ObjPtr<Class> that) {
493 ObjPtr<Class> klass1 = this;
494 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 if (klass1 == klass2) {
496 return true;
497 }
498 // Class loaders must match.
499 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
500 return false;
501 }
502 // Arrays are in the same package when their element classes are.
503 while (klass1->IsArrayClass()) {
504 klass1 = klass1->GetComponentType();
505 }
506 while (klass2->IsArrayClass()) {
507 klass2 = klass2->GetComponentType();
508 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700509 // trivial check again for array types
510 if (klass1 == klass2) {
511 return true;
512 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700514 std::string temp1, temp2;
515 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516}
517
Ian Rogersef7d42f2014-01-06 12:55:46 -0800518bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100519 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520}
521
Vladimir Markoba118822017-06-12 15:41:56 +0100522template <typename SignatureType>
523static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000524 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100525 const SignatureType& signature,
526 PointerSize pointer_size)
527 REQUIRES_SHARED(Locks::mutator_lock_) {
528 // If the current class is not an interface, skip the search of its declared methods;
529 // such lookup is used only to distinguish between IncompatibleClassChangeError and
530 // NoSuchMethodError and the caller has already tried to search methods in the class.
531 if (LIKELY(klass->IsInterface())) {
532 // Search declared methods, both direct and virtual.
533 // (This lookup is used also for invoke-static on interface classes.)
534 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700535 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100536 return &method;
537 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538 }
539 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700540
Vladimir Markoba118822017-06-12 15:41:56 +0100541 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
542 // we should return it, otherwise an arbitrary one can be returned.
543 ObjPtr<IfTable> iftable = klass->GetIfTable();
544 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
545 ObjPtr<Class> iface = iftable->GetInterface(i);
546 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700547 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100548 return &method;
549 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700550 }
551 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552
Vladimir Markoba118822017-06-12 15:41:56 +0100553 // Then search for public non-static methods in the java.lang.Object.
554 if (LIKELY(klass->IsInterface())) {
555 ObjPtr<Class> object_class = klass->GetSuperClass();
556 DCHECK(object_class->IsObjectClass());
557 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
558 if (method.IsPublic() && !method.IsStatic() &&
Eric Holkabdb4592019-05-16 08:33:12 -0700559 method.GetNameView() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 }
562 }
563 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700564 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565}
566
Vladimir Markoe027d722019-02-05 10:13:49 +0000567ArtMethod* Class::FindInterfaceMethod(std::string_view name,
568 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100569 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000570 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571}
572
Vladimir Markoe027d722019-02-05 10:13:49 +0000573ArtMethod* Class::FindInterfaceMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100574 const Signature& signature,
575 PointerSize pointer_size) {
576 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700577}
578
Vladimir Markoba118822017-06-12 15:41:56 +0100579ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
580 uint32_t dex_method_idx,
581 PointerSize pointer_size) {
582 // We always search by name and signature, ignoring the type index in the MethodId.
583 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800584 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Eric Holkabdb4592019-05-16 08:33:12 -0700585 std::string_view name = dex_file.StringViewByIdx(method_id.name_idx_);
Vladimir Markoba118822017-06-12 15:41:56 +0100586 const Signature signature = dex_file.GetMethodSignature(method_id);
587 return FindInterfaceMethod(name, signature, pointer_size);
588}
589
Alex Lightafb66472017-08-01 09:54:49 -0700590static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
591 ObjPtr<mirror::Class> declaring_class)
592 REQUIRES_SHARED(Locks::mutator_lock_) {
593 if (klass->IsArrayClass()) {
594 return declaring_class->IsObjectClass();
595 } else if (klass->IsInterface()) {
596 return declaring_class->IsObjectClass() || declaring_class == klass;
597 } else {
598 return klass->IsSubClass(declaring_class);
599 }
600}
601
Vladimir Markoba118822017-06-12 15:41:56 +0100602static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
603 ObjPtr<mirror::Class> declaring_class,
604 ArtMethod& method)
605 REQUIRES_SHARED(Locks::mutator_lock_) {
606 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
607 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700608 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100609 uint32_t access_flags = method.GetAccessFlags();
610 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
611 return true;
612 }
613 if ((access_flags & kAccPrivate) != 0) {
614 return false;
615 }
616 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
617 if (!klass->IsInSamePackage(declaring_class)) {
618 return false;
619 }
620 }
621 return true;
622}
623
624template <typename SignatureType>
625static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000626 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100627 const SignatureType& signature,
628 PointerSize pointer_size)
629 REQUIRES_SHARED(Locks::mutator_lock_) {
630 // Search declared methods first.
631 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
632 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
Vladimir Marko4573be32021-06-07 11:07:05 +0100633 if (np_method->GetNameView() == name && np_method->GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100634 return &method;
635 }
636 }
637
638 // Then search the superclass chain. If we find an inherited method, return it.
639 // If we find a method that's not inherited because of access restrictions,
640 // try to find a method inherited from an interface in copied methods.
641 ObjPtr<Class> klass = this_klass->GetSuperClass();
642 ArtMethod* uninherited_method = nullptr;
643 for (; klass != nullptr; klass = klass->GetSuperClass()) {
644 DCHECK(!klass->IsProxyClass());
645 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100646 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100647 if (IsInheritedMethod(this_klass, klass, method)) {
648 return &method;
649 }
650 uninherited_method = &method;
651 break;
652 }
653 }
654 if (uninherited_method != nullptr) {
655 break;
656 }
657 }
658
659 // Then search copied methods.
660 // If we found a method that's not inherited, stop the search in its declaring class.
661 ObjPtr<Class> end_klass = klass;
662 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
663 klass = this_klass;
664 if (UNLIKELY(klass->IsProxyClass())) {
665 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
666 klass = klass->GetSuperClass();
667 }
668 for (; klass != end_klass; klass = klass->GetSuperClass()) {
669 DCHECK(!klass->IsProxyClass());
670 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100671 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100672 return &method; // No further check needed, copied methods are inherited by definition.
673 }
674 }
675 }
676 return uninherited_method; // Return the `uninherited_method` if any.
677}
678
679
Vladimir Markoe027d722019-02-05 10:13:49 +0000680ArtMethod* Class::FindClassMethod(std::string_view name,
681 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100682 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000683 return FindClassMethodWithSignature(this, name, signature, pointer_size);
Vladimir Markoba118822017-06-12 15:41:56 +0100684}
685
Vladimir Markoe027d722019-02-05 10:13:49 +0000686ArtMethod* Class::FindClassMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100687 const Signature& signature,
688 PointerSize pointer_size) {
689 return FindClassMethodWithSignature(this, name, signature, pointer_size);
690}
691
Vladimir Marko17769472021-06-23 10:40:29 +0100692// Binary search a range with a three-way compare function.
693//
694// Return a tuple consisting of a `success` value, the index of the match (`mid`) and
695// the remaining range when we found the match (`begin` and `end`). This is useful for
696// subsequent binary search with a secondary comparator, see `ClassMemberBinarySearch()`.
697template <typename Compare>
698ALWAYS_INLINE
699std::tuple<bool, uint32_t, uint32_t, uint32_t> BinarySearch(uint32_t begin,
700 uint32_t end,
701 Compare&& cmp)
702 REQUIRES_SHARED(Locks::mutator_lock_) {
703 while (begin != end) {
704 uint32_t mid = (begin + end) >> 1;
705 auto cmp_result = cmp(mid);
706 if (cmp_result == 0) {
707 return {true, mid, begin, end};
708 }
709 if (cmp_result > 0) {
710 begin = mid + 1u;
711 } else {
712 end = mid;
713 }
714 }
715 return {false, 0u, 0u, 0u};
716}
717
718// Binary search for class members. The range passed to this search must be sorted, so
719// declared methods or fields cannot be searched directly but declared direct methods,
720// declared virtual methods, declared static fields or declared instance fields can.
721template <typename NameCompare, typename SecondCompare, typename NameIndexGetter>
722ALWAYS_INLINE
723std::tuple<bool, uint32_t> ClassMemberBinarySearch(uint32_t begin,
724 uint32_t end,
725 NameCompare&& name_cmp,
726 SecondCompare&& second_cmp,
727 NameIndexGetter&& get_name_idx)
728 REQUIRES_SHARED(Locks::mutator_lock_) {
729 // First search for the item with the given name.
730 bool success;
731 uint32_t mid;
732 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_cmp);
733 if (!success) {
734 return {false, 0u};
735 }
736 // If found, do the secondary comparison.
737 auto second_cmp_result = second_cmp(mid);
738 if (second_cmp_result == 0) {
739 return {true, mid};
740 }
741 // We have matched the name but not the secondary comparison. We no longer need to
742 // search for the name as string as we know the matching name string index.
743 // Repeat the above binary searches and secondary comparisons with a simpler name
744 // index compare until the search range contains only matching name.
745 auto name_idx = get_name_idx(mid);
746 if (second_cmp_result > 0) {
747 do {
748 begin = mid + 1u;
749 auto name_index_cmp = [&](uint32_t mid2) REQUIRES_SHARED(Locks::mutator_lock_) {
750 DCHECK_LE(name_idx, get_name_idx(mid2));
751 return (name_idx != get_name_idx(mid2)) ? -1 : 0;
752 };
753 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_index_cmp);
754 if (!success) {
755 return {false, 0u};
756 }
757 second_cmp_result = second_cmp(mid);
758 } while (second_cmp_result > 0);
759 end = mid;
760 } else {
761 do {
762 end = mid;
763 auto name_index_cmp = [&](uint32_t mid2) REQUIRES_SHARED(Locks::mutator_lock_) {
764 DCHECK_GE(name_idx, get_name_idx(mid2));
765 return (name_idx != get_name_idx(mid2)) ? 1 : 0;
766 };
767 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_index_cmp);
768 if (!success) {
769 return {false, 0u};
770 }
771 second_cmp_result = second_cmp(mid);
772 } while (second_cmp_result < 0);
773 begin = mid + 1u;
774 }
775 if (second_cmp_result == 0) {
776 return {true, mid};
777 }
778 // All items in the remaining range have a matching name, so search with secondary comparison.
779 std::tie(success, mid, std::ignore, std::ignore) = BinarySearch(begin, end, second_cmp);
780 return {success, mid};
781}
782
783static std::tuple<bool, ArtMethod*> FindDeclaredClassMethod(ObjPtr<mirror::Class> klass,
784 const DexFile& dex_file,
785 std::string_view name,
786 Signature signature,
787 PointerSize pointer_size)
788 REQUIRES_SHARED(Locks::mutator_lock_) {
789 DCHECK(&klass->GetDexFile() == &dex_file);
790 DCHECK(!name.empty());
791
792 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
793 DCHECK(!declared_methods.empty());
794 auto get_method_id = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE
795 -> const dex::MethodId& {
796 ArtMethod& method = declared_methods[mid];
797 DCHECK(method.GetDexFile() == &dex_file);
798 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
799 return dex_file.GetMethodId(method.GetDexMethodIndex());
800 };
801 auto name_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
802 // Do not use ArtMethod::GetNameView() to avoid reloading dex file through the same
803 // declaring class from different methods and also avoid the runtime method check.
804 const dex::MethodId& method_id = get_method_id(mid);
805 return name.compare(dex_file.GetMethodNameView(method_id));
806 };
807 auto signature_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
808 // Do not use ArtMethod::GetSignature() to avoid reloading dex file through the same
809 // declaring class from different methods and also avoid the runtime method check.
810 const dex::MethodId& method_id = get_method_id(mid);
811 return signature.Compare(dex_file.GetMethodSignature(method_id));
812 };
813 auto get_name_idx = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
814 const dex::MethodId& method_id = get_method_id(mid);
815 return method_id.name_idx_;
816 };
817
818 // Use binary search in the sorted direct methods, then in the sorted virtual methods.
819 uint32_t num_direct_methods = klass->NumDirectMethods();
820 uint32_t num_declared_methods = dchecked_integral_cast<uint32_t>(declared_methods.size());
821 DCHECK_LE(num_direct_methods, num_declared_methods);
822 const uint32_t ranges[2][2] = {
823 {0u, num_direct_methods}, // Declared direct methods.
824 {num_direct_methods, num_declared_methods} // Declared virtual methods.
825 };
826 for (const uint32_t (&range)[2] : ranges) {
827 auto [success, mid] =
828 ClassMemberBinarySearch(range[0], range[1], name_cmp, signature_cmp, get_name_idx);
829 if (success) {
830 return {true, &declared_methods[mid]};
831 }
832 }
833
834 // Did not find a declared method in either slice.
835 return {false, nullptr};
836}
837
Vladimir Marko4573be32021-06-07 11:07:05 +0100838FLATTEN
Vladimir Markoba118822017-06-12 15:41:56 +0100839ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
840 uint32_t dex_method_idx,
841 PointerSize pointer_size) {
842 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
843 DCHECK(!IsProxyClass());
844
845 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
846 ObjPtr<DexCache> this_dex_cache = GetDexCache();
847 if (this_dex_cache == dex_cache) {
848 // Lookup is always performed in the class referenced by the MethodId.
849 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
850 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
851 if (method.GetDexMethodIndex() == dex_method_idx) {
852 return &method;
853 }
854 }
855 }
Vladimir Marko17769472021-06-23 10:40:29 +0100856
Vladimir Markoba118822017-06-12 15:41:56 +0100857 // If not found, we need to search by name and signature.
858 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800859 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100860 const Signature signature = dex_file.GetMethodSignature(method_id);
Vladimir Marko4573be32021-06-07 11:07:05 +0100861 std::string_view name; // Do not touch the dex file string data until actually needed.
Vladimir Marko17769472021-06-23 10:40:29 +0100862
Vladimir Markoba118822017-06-12 15:41:56 +0100863 // If we do not have a dex_cache match, try to find the declared method in this class now.
864 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
865 DCHECK(name.empty());
Vladimir Marko4573be32021-06-07 11:07:05 +0100866 name = dex_file.GetMethodNameView(method_id);
Vladimir Marko17769472021-06-23 10:40:29 +0100867 auto [success, method] = FindDeclaredClassMethod(
868 this, *this_dex_cache->GetDexFile(), name, signature, pointer_size);
869 DCHECK_EQ(success, method != nullptr);
870 if (success) {
871 return method;
Vladimir Markoba118822017-06-12 15:41:56 +0100872 }
873 }
874
875 // Then search the superclass chain. If we find an inherited method, return it.
876 // If we find a method that's not inherited because of access restrictions,
877 // try to find a method inherited from an interface in copied methods.
878 ArtMethod* uninherited_method = nullptr;
879 ObjPtr<Class> klass = GetSuperClass();
880 for (; klass != nullptr; klass = klass->GetSuperClass()) {
881 ArtMethod* candidate_method = nullptr;
882 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
Vladimir Marko17769472021-06-23 10:40:29 +0100883 ObjPtr<DexCache> klass_dex_cache = klass->GetDexCache();
884 if (klass_dex_cache == dex_cache) {
Vladimir Markoba118822017-06-12 15:41:56 +0100885 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
886 // the type index differs, so compare the name index and proto index.
887 for (ArtMethod& method : declared_methods) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800888 const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
Vladimir Markoba118822017-06-12 15:41:56 +0100889 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
890 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
891 candidate_method = &method;
892 break;
893 }
894 }
Vladimir Marko4573be32021-06-07 11:07:05 +0100895 } else if (!declared_methods.empty()) {
896 if (name.empty()) {
897 name = dex_file.GetMethodNameView(method_id);
Vladimir Markoba118822017-06-12 15:41:56 +0100898 }
Vladimir Marko17769472021-06-23 10:40:29 +0100899 auto [success, method] = FindDeclaredClassMethod(
900 klass, *klass_dex_cache->GetDexFile(), name, signature, pointer_size);
901 DCHECK_EQ(success, method != nullptr);
902 if (success) {
903 candidate_method = method;
Vladimir Markoba118822017-06-12 15:41:56 +0100904 }
905 }
906 if (candidate_method != nullptr) {
907 if (IsInheritedMethod(this, klass, *candidate_method)) {
908 return candidate_method;
909 } else {
910 uninherited_method = candidate_method;
911 break;
912 }
913 }
914 }
915
916 // Then search copied methods.
917 // If we found a method that's not inherited, stop the search in its declaring class.
918 ObjPtr<Class> end_klass = klass;
919 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
920 // After we have searched the declared methods of the super-class chain,
921 // search copied methods which can contain methods from interfaces.
922 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
923 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
924 if (!copied_methods.empty() && name.empty()) {
925 name = dex_file.StringDataByIdx(method_id.name_idx_);
926 }
927 for (ArtMethod& method : copied_methods) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100928 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100929 return &method; // No further check needed, copied methods are inherited by definition.
930 }
931 }
932 }
933 return uninherited_method; // Return the `uninherited_method` if any.
934}
935
Vladimir Markoe027d722019-02-05 10:13:49 +0000936ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) {
Vladimir Markoba118822017-06-12 15:41:56 +0100937 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
938 DCHECK(!IsProxyClass());
Vladimir Markoe027d722019-02-05 10:13:49 +0000939 std::string_view name("<init>");
Vladimir Markoba118822017-06-12 15:41:56 +0100940 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000941 if (method.GetName() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100942 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800943 }
944 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700945 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800946}
947
Vladimir Markoe027d722019-02-05 10:13:49 +0000948ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000949 for (auto& method : GetDirectMethods(pointer_size)) {
950 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
951 if (name == np_method->GetName()) {
952 return &method;
953 }
954 }
955 return nullptr;
956}
957
Vladimir Markoe027d722019-02-05 10:13:49 +0000958ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000959 for (auto& method : GetVirtualMethods(pointer_size)) {
960 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
961 if (name == np_method->GetName()) {
962 return &method;
963 }
964 }
965 return nullptr;
966}
967
Andreas Gampe542451c2016-07-26 09:02:02 -0700968ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700969 DCHECK(method->GetDeclaringClass()->IsInterface());
970 DCHECK(IsInterface()) << "Should only be called on a interface class";
971 // Check if we have one defined on this interface first. This includes searching copied ones to
972 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
973 // don't do any indirect method checks here.
974 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
975 if (method->HasSameNameAndSignature(&iface_method)) {
976 return &iface_method;
977 }
978 }
979
980 std::vector<ArtMethod*> abstract_methods;
981 // Search through the IFTable for a working version. We don't need to check for conflicts
982 // because if there was one it would appear in this classes virtual_methods_ above.
983
984 Thread* self = Thread::Current();
985 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700986 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
987 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700988 size_t iftable_count = GetIfTableCount();
989 // Find the method. We don't need to check for conflicts because they would have been in the
990 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
991 // subtypiest interfaces get visited first.
992 for (size_t k = iftable_count; k != 0;) {
993 k--;
994 DCHECK_LT(k, iftable->Count());
995 iface.Assign(iftable->GetInterface(k));
996 // Iterate through every declared method on this interface. Each direct method's name/signature
997 // is unique so the order of the inner loop doesn't matter.
998 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
999 ArtMethod* current_method = &method_iter;
1000 if (current_method->HasSameNameAndSignature(method)) {
1001 if (current_method->IsDefault()) {
1002 // Handle JLS soft errors, a default method from another superinterface tree can
1003 // "override" an abstract method(s) from another superinterface tree(s). To do this,
1004 // ignore any [default] method which are dominated by the abstract methods we've seen so
1005 // far. Check if overridden by any in abstract_methods. We do not need to check for
1006 // default_conflicts because we would hit those before we get to this loop.
1007 bool overridden = false;
1008 for (ArtMethod* possible_override : abstract_methods) {
1009 DCHECK(possible_override->HasSameNameAndSignature(current_method));
1010 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
1011 overridden = true;
1012 break;
1013 }
1014 }
1015 if (!overridden) {
1016 return current_method;
1017 }
1018 } else {
1019 // Is not default.
1020 // This might override another default method. Just stash it for now.
1021 abstract_methods.push_back(current_method);
1022 }
1023 }
1024 }
1025 }
1026 // If we reach here we either never found any declaration of the method (in which case
1027 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
1028 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
1029 // of these arbitrarily.
1030 return abstract_methods.empty() ? nullptr : abstract_methods[0];
1031}
1032
Andreas Gampe542451c2016-07-26 09:02:02 -07001033ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001034 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
1035 if (method.IsClassInitializer()) {
1036 DCHECK_STREQ(method.GetName(), "<clinit>");
1037 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
1038 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001039 }
1040 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001041 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001042}
1043
Vladimir Marko70e126a2021-06-24 14:51:34 +01001044static std::tuple<bool, ArtField*> FindFieldByNameAndType(const DexFile& dex_file,
1045 LengthPrefixedArray<ArtField>* fields,
1046 std::string_view name,
1047 std::string_view type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001048 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001049 DCHECK(fields != nullptr);
Vladimir Marko70e126a2021-06-24 14:51:34 +01001050 DCHECK(!name.empty());
1051 DCHECK(!type.empty());
1052
1053 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
1054 // verifier. There can be multiple fields with the same name in the same class due to proguard.
1055 // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as
1056 // unsigned; for Modified-UTF-8 without embedded nulls this is consistent with the
1057 // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering.
1058 auto get_field_id = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE
1059 -> const dex::FieldId& {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001060 ArtField& field = fields->At(mid);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001061 DCHECK(field.GetDexFile() == &dex_file);
Vladimir Marko70e126a2021-06-24 14:51:34 +01001062 return dex_file.GetFieldId(field.GetDexFieldIndex());
1063 };
1064 auto name_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1065 const dex::FieldId& field_id = get_field_id(mid);
1066 return name.compare(dex_file.GetFieldNameView(field_id));
1067 };
1068 auto type_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1069 const dex::FieldId& field_id = get_field_id(mid);
1070 return type.compare(dex_file.GetTypeDescriptorView(dex_file.GetTypeId(field_id.type_idx_)));
1071 };
1072 auto get_name_idx = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1073 const dex::FieldId& field_id = get_field_id(mid);
1074 return field_id.name_idx_;
1075 };
1076
1077 // Use binary search in the sorted fields.
1078 auto [success, mid] =
1079 ClassMemberBinarySearch(/*begin=*/ 0u, fields->size(), name_cmp, type_cmp, get_name_idx);
1080
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001081 if (kIsDebugBuild) {
1082 ArtField* found = nullptr;
1083 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
1084 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
1085 found = &field;
1086 break;
1087 }
1088 }
George Burgess IV869746e2021-06-14 14:13:14 -07001089
Vladimir Marko70e126a2021-06-24 14:51:34 +01001090 ArtField* ret = success ? &fields->At(mid) : nullptr;
1091 CHECK_EQ(found, ret)
1092 << "Found " << ArtField::PrettyField(found) << " vs " << ArtField::PrettyField(ret);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001093 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001094
1095 if (success) {
1096 return {true, &fields->At(mid)};
1097 }
1098
1099 return {false, nullptr};
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001100}
1101
Vladimir Markoe027d722019-02-05 10:13:49 +00001102ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001103 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001104 LengthPrefixedArray<ArtField>* ifields = GetIFieldsPtr();
1105 if (ifields == nullptr) {
1106 return nullptr;
1107 }
1108 DCHECK(!IsProxyClass());
Vladimir Marko70e126a2021-06-24 14:51:34 +01001109 auto [success, field] = FindFieldByNameAndType(GetDexFile(), ifields, name, type);
1110 DCHECK_EQ(success, field != nullptr);
1111 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001112}
1113
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001114ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001115 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001116 for (ArtField& field : GetIFields()) {
1117 if (field.GetDexFieldIndex() == dex_field_idx) {
1118 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001119 }
1120 }
1121 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001122 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001123}
1124
Vladimir Markoe027d722019-02-05 10:13:49 +00001125ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001126 // Is the field in this class, or any of its superclasses?
1127 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001128 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001129 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -07001130 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001131 return f;
1132 }
1133 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001134 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001135}
1136
Vladimir Markoe027d722019-02-05 10:13:49 +00001137ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) {
1138 DCHECK(!type.empty());
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001139 LengthPrefixedArray<ArtField>* sfields = GetSFieldsPtr();
1140 if (sfields == nullptr) {
1141 return nullptr;
1142 }
1143 if (UNLIKELY(IsProxyClass())) {
1144 // Proxy fields do not have appropriate dex field indexes required by
1145 // `FindFieldByNameAndType()`. However, each proxy class has exactly
1146 // the same artificial fields created by the `ClassLinker`.
1147 DCHECK_EQ(sfields->size(), 2u);
1148 DCHECK_EQ(strcmp(sfields->At(0).GetName(), "interfaces"), 0);
1149 DCHECK_EQ(strcmp(sfields->At(0).GetTypeDescriptor(), "[Ljava/lang/Class;"), 0);
1150 DCHECK_EQ(strcmp(sfields->At(1).GetName(), "throws"), 0);
1151 DCHECK_EQ(strcmp(sfields->At(1).GetTypeDescriptor(), "[[Ljava/lang/Class;"), 0);
1152 if (name == "interfaces") {
1153 return (type == "[Ljava/lang/Class;") ? &sfields->At(0) : nullptr;
1154 } else if (name == "throws") {
1155 return (type == "[[Ljava/lang/Class;") ? &sfields->At(1) : nullptr;
1156 } else {
1157 return nullptr;
1158 }
1159 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001160 auto [success, field] = FindFieldByNameAndType(GetDexFile(), sfields, name, type);
1161 DCHECK_EQ(success, field != nullptr);
1162 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001163}
1164
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001165ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001166 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001167 for (ArtField& field : GetSFields()) {
1168 if (field.GetDexFieldIndex() == dex_field_idx) {
1169 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001170 }
1171 }
1172 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001173 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001174}
1175
Vladimir Markob10668c2021-06-10 09:52:53 +01001176ArtField* Class::FindStaticField(std::string_view name, std::string_view type) {
1177 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001178 // Is the field in this class (or its interfaces), or any of its
1179 // superclasses (or their interfaces)?
Vladimir Markob10668c2021-06-10 09:52:53 +01001180 for (ObjPtr<Class> k = this; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001181 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -07001182 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001183 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001184 return f;
1185 }
1186 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +00001187 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001188 ObjPtr<Class> interface = k->GetDirectInterface(i);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001189 DCHECK(interface != nullptr);
Vladimir Markob10668c2021-06-10 09:52:53 +01001190 f = interface->FindStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001191 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001192 return f;
1193 }
1194 }
1195 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001196 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001197}
1198
Vladimir Marko40261172021-06-14 10:59:51 +01001199// Find a field using the JLS field resolution order.
1200// Template arguments can be used to limit the search to either static or instance fields.
1201// The search should be limited only if we know that a full search would yield a field of
1202// the right type or no field at all. This can be known for field references in a method
1203// if we have previously verified that method and did not find a field type mismatch.
1204template <bool kSearchInstanceFields, bool kSearchStaticFields>
1205ALWAYS_INLINE
1206ArtField* FindFieldImpl(ObjPtr<mirror::Class> klass,
1207 ObjPtr<mirror::DexCache> dex_cache,
1208 uint32_t field_idx) REQUIRES_SHARED(Locks::mutator_lock_) {
1209 static_assert(kSearchInstanceFields || kSearchStaticFields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001210
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001211 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
Vladimir Marko40261172021-06-14 10:59:51 +01001212 DCHECK(!klass->IsProxyClass());
Vladimir Markob10668c2021-06-10 09:52:53 +01001213
1214 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001215
1216 // First try to find a declared field by `field_idx` if we have a `dex_cache` match.
Vladimir Marko40261172021-06-14 10:59:51 +01001217 ObjPtr<DexCache> klass_dex_cache = klass->GetDexCache();
1218 if (klass_dex_cache == dex_cache) {
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001219 // Lookup is always performed in the class referenced by the FieldId.
Vladimir Marko40261172021-06-14 10:59:51 +01001220 DCHECK_EQ(klass->GetDexTypeIndex(),
1221 klass_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_);
1222 ArtField* f = kSearchInstanceFields
1223 ? klass->FindDeclaredInstanceField(klass_dex_cache, field_idx)
1224 : nullptr;
1225 if (kSearchStaticFields && f == nullptr) {
1226 f = klass->FindDeclaredStaticField(klass_dex_cache, field_idx);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001227 }
1228 if (f != nullptr) {
1229 return f;
1230 }
1231 }
1232
1233 const DexFile& dex_file = *dex_cache->GetDexFile();
1234 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
1235
1236 std::string_view name; // Do not touch the dex file string data until actually needed.
1237 std::string_view type;
1238 auto ensure_name_and_type_initialized = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
1239 if (name.empty()) {
1240 name = dex_file.GetFieldNameView(field_id);
1241 type = dex_file.GetFieldTypeDescriptorView(field_id);
1242 }
1243 };
1244
1245 auto search_direct_interfaces = [&](ObjPtr<mirror::Class> k)
1246 REQUIRES_SHARED(Locks::mutator_lock_) {
1247 // TODO: The `FindStaticField()` performs a recursive search and it's possible to
1248 // construct interface hierarchies that make the time complexity exponential in depth.
1249 // Rewrite this with a `HashSet<mirror::Class*>` to mark classes we have already
1250 // searched for the field, so that we call `FindDeclaredStaticField()` only once
1251 // on each interface. And use a work queue to avoid unlimited recursion depth.
1252 // TODO: Once we call `FindDeclaredStaticField()` directly, use search by indexes
1253 // instead of strings if the interface's dex cache matches `dex_cache`. This shall
1254 // allow delaying the `ensure_name_and_type_initialized()` call further.
1255 uint32_t num_interfaces = k->NumDirectInterfaces();
1256 if (num_interfaces != 0u) {
1257 ensure_name_and_type_initialized();
1258 for (uint32_t i = 0; i != num_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001259 ObjPtr<Class> interface = k->GetDirectInterface(i);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001260 DCHECK(interface != nullptr);
Vladimir Markob10668c2021-06-10 09:52:53 +01001261 ArtField* f = interface->FindStaticField(name, type);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001262 if (f != nullptr) {
1263 return f;
1264 }
1265 }
1266 }
1267 return static_cast<ArtField*>(nullptr);
1268 };
1269
Vladimir Marko70e126a2021-06-24 14:51:34 +01001270 auto find_field_by_name_and_type = [&](ObjPtr<mirror::Class> k, ObjPtr<DexCache> k_dex_cache)
1271 REQUIRES_SHARED(Locks::mutator_lock_) -> std::tuple<bool, ArtField*> {
1272 if ((!kSearchInstanceFields || k->GetIFieldsPtr() == nullptr) &&
1273 (!kSearchStaticFields || k->GetSFieldsPtr() == nullptr)) {
1274 return {false, nullptr};
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001275 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001276 ensure_name_and_type_initialized();
1277 const DexFile& k_dex_file = *k_dex_cache->GetDexFile();
1278 if (kSearchInstanceFields && k->GetIFieldsPtr() != nullptr) {
1279 auto [success, field] = FindFieldByNameAndType(k_dex_file, k->GetIFieldsPtr(), name, type);
1280 DCHECK_EQ(success, field != nullptr);
1281 if (success) {
1282 return {true, field};
1283 }
1284 }
1285 if (kSearchStaticFields && k->GetSFieldsPtr() != nullptr) {
1286 auto [success, field] = FindFieldByNameAndType(k_dex_file, k->GetSFieldsPtr(), name, type);
1287 DCHECK_EQ(success, field != nullptr);
1288 if (success) {
1289 return {true, field};
1290 }
1291 }
1292 return {false, nullptr};
1293 };
1294
1295 // If we had a dex cache mismatch, search declared fields by name and type.
1296 if (klass_dex_cache != dex_cache) {
1297 auto [success, field] = find_field_by_name_and_type(klass, klass_dex_cache);
1298 DCHECK_EQ(success, field != nullptr);
1299 if (success) {
1300 return field;
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001301 }
1302 }
1303
Vladimir Marko40261172021-06-14 10:59:51 +01001304 // Search direct interfaces for static fields.
1305 if (kSearchStaticFields) {
1306 ArtField* f = search_direct_interfaces(klass);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001307 if (f != nullptr) {
1308 return f;
1309 }
1310 }
1311
1312 // Continue searching in superclasses.
Vladimir Marko40261172021-06-14 10:59:51 +01001313 for (ObjPtr<Class> k = klass->GetSuperClass(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001314 // Is the field in this class?
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001315 ObjPtr<DexCache> k_dex_cache = k->GetDexCache();
1316 if (k_dex_cache == dex_cache) {
1317 // Matching dex_cache. We cannot compare the `field_idx` anymore because
1318 // the type index differs, so compare the name index and type index.
Vladimir Marko40261172021-06-14 10:59:51 +01001319 if (kSearchInstanceFields) {
1320 for (ArtField& field : k->GetIFields()) {
1321 const dex::FieldId& other_field_id = dex_file.GetFieldId(field.GetDexFieldIndex());
1322 if (other_field_id.name_idx_ == field_id.name_idx_ &&
1323 other_field_id.type_idx_ == field_id.type_idx_) {
1324 return &field;
1325 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001326 }
1327 }
Vladimir Marko40261172021-06-14 10:59:51 +01001328 if (kSearchStaticFields) {
1329 for (ArtField& field : k->GetSFields()) {
1330 const dex::FieldId& other_field_id = dex_file.GetFieldId(field.GetDexFieldIndex());
1331 if (other_field_id.name_idx_ == field_id.name_idx_ &&
1332 other_field_id.type_idx_ == field_id.type_idx_) {
1333 return &field;
1334 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001335 }
1336 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001337 } else {
1338 auto [success, field] = find_field_by_name_and_type(k, k_dex_cache);
1339 DCHECK_EQ(success, field != nullptr);
1340 if (success) {
1341 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001342 }
1343 }
Vladimir Marko40261172021-06-14 10:59:51 +01001344 if (kSearchStaticFields) {
1345 // Is this field in any of this class' interfaces?
1346 ArtField* f = search_direct_interfaces(k);
1347 if (f != nullptr) {
1348 return f;
1349 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001350 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001351 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001352 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001353}
1354
Vladimir Marko40261172021-06-14 10:59:51 +01001355FLATTEN
1356ArtField* Class::FindField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1357 return FindFieldImpl</*kSearchInstanceFields=*/ true,
1358 /*kSearchStaticFields*/ true>(this, dex_cache, field_idx);
1359}
1360
1361FLATTEN
1362ArtField* Class::FindInstanceField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1363 return FindFieldImpl</*kSearchInstanceFields=*/ true,
1364 /*kSearchStaticFields*/ false>(this, dex_cache, field_idx);
1365}
1366
1367FLATTEN
1368ArtField* Class::FindStaticField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1369 return FindFieldImpl</*kSearchInstanceFields=*/ false,
1370 /*kSearchStaticFields*/ true>(this, dex_cache, field_idx);
1371}
1372
Alex Lightb1eebde2019-10-22 16:30:47 +00001373void Class::ClearSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
1374 DCHECK(IsVerified());
1375 for (auto& m : GetMethods(pointer_size)) {
1376 if (!m.IsNative() && m.IsInvokable()) {
1377 m.ClearSkipAccessChecks();
1378 }
1379 }
1380}
1381
Alex Lightc2d0c962019-10-23 14:14:25 -07001382void Class::ClearMustCountLocksFlagOnAllMethods(PointerSize pointer_size) {
1383 DCHECK(IsVerified());
1384 for (auto& m : GetMethods(pointer_size)) {
1385 if (!m.IsNative() && m.IsInvokable()) {
1386 m.ClearMustCountLocks();
1387 }
1388 }
1389}
1390
1391void Class::ClearDontCompileFlagOnAllMethods(PointerSize pointer_size) {
1392 DCHECK(IsVerified());
1393 for (auto& m : GetMethods(pointer_size)) {
1394 if (!m.IsNative() && m.IsInvokable()) {
1395 m.ClearDontCompile();
1396 }
1397 }
1398}
1399
Andreas Gampe542451c2016-07-26 09:02:02 -07001400void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001401 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001402 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001403 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001404 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001405 }
1406 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001407}
1408
Ian Rogers1ff3c982014-08-12 02:30:58 -07001409const char* Class::GetDescriptor(std::string* storage) {
Vladimir Marko3892e622019-03-15 15:22:18 +00001410 size_t dim = 0u;
1411 ObjPtr<mirror::Class> klass = this;
1412 while (klass->IsArrayClass()) {
1413 ++dim;
Vladimir Markod355acf2019-03-21 17:09:40 +00001414 // No read barrier needed, we're reading a chain of constant references for comparison
1415 // with null. Then we follow up below with reading constant references to read constant
1416 // primitive data in both proxy and non-proxy paths. See ReadBarrierOption.
1417 klass = klass->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001418 }
Vladimir Marko3892e622019-03-15 15:22:18 +00001419 if (klass->IsProxyClass()) {
1420 // No read barrier needed, the `name` field is constant for proxy classes and
1421 // the contents of the String are also constant. See ReadBarrierOption.
1422 ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>();
1423 DCHECK(name != nullptr);
1424 *storage = DotToDescriptor(name->ToModifiedUtf8().c_str());
1425 } else {
1426 const char* descriptor;
1427 if (klass->IsPrimitive()) {
1428 descriptor = Primitive::Descriptor(klass->GetPrimitiveType());
1429 } else {
1430 const DexFile& dex_file = klass->GetDexFile();
1431 const dex::TypeId& type_id = dex_file.GetTypeId(klass->GetDexTypeIndex());
1432 descriptor = dex_file.GetTypeDescriptor(type_id);
1433 }
1434 if (dim == 0) {
1435 return descriptor;
1436 }
1437 *storage = descriptor;
1438 }
1439 storage->insert(0u, dim, '[');
Ian Rogers1ff3c982014-08-12 02:30:58 -07001440 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001441}
1442
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001443const dex::ClassDef* Class::GetClassDef() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001444 uint16_t class_def_idx = GetDexClassDefIndex();
1445 if (class_def_idx == DexFile::kDexNoIndex16) {
1446 return nullptr;
1447 }
1448 return &GetDexFile().GetClassDef(class_def_idx);
1449}
1450
Andreas Gampea5b09a62016-11-17 15:21:22 -08001451dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001452 DCHECK(!IsPrimitive());
1453 DCHECK(!IsArrayClass());
1454 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1455}
1456
Vladimir Markob10668c2021-06-10 09:52:53 +01001457ObjPtr<Class> Class::GetDirectInterface(uint32_t idx) {
1458 DCHECK(!IsPrimitive());
1459 if (IsArrayClass()) {
1460 ObjPtr<IfTable> iftable = GetIfTable();
1461 DCHECK(iftable != nullptr);
1462 DCHECK_EQ(iftable->Count(), 2u);
1463 DCHECK_LT(idx, 2u);
1464 ObjPtr<Class> interface = iftable->GetInterface(idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001465 DCHECK(interface != nullptr);
1466 return interface;
Vladimir Markob10668c2021-06-10 09:52:53 +01001467 } else if (IsProxyClass()) {
1468 ObjPtr<ObjectArray<Class>> interfaces = GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001469 DCHECK(interfaces != nullptr);
1470 return interfaces->Get(idx);
1471 } else {
Vladimir Markob10668c2021-06-10 09:52:53 +01001472 dex::TypeIndex type_idx = GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001473 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Markob10668c2021-06-10 09:52:53 +01001474 type_idx, GetDexCache(), GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001475 return interface;
1476 }
1477}
1478
Vladimir Marko19a4d372016-12-08 14:41:46 +00001479ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001480 ObjPtr<Class> interface = klass->GetDirectInterface(idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001481 if (interface == nullptr) {
1482 DCHECK(!klass->IsArrayClass());
1483 DCHECK(!klass->IsProxyClass());
1484 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001485 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001486 CHECK(interface != nullptr || self->IsExceptionPending());
1487 }
1488 return interface;
1489}
1490
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001491ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001492 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001493 DCHECK(!klass->IsInterface());
1494 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001495 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001496 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001497 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001498 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001499 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001500 }
Calin Juravle52503d82015-11-11 16:58:31 +00001501 return common_super_class;
1502}
1503
Mathieu Chartierf8322842014-05-16 10:59:25 -07001504const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001505 const DexFile& dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001506 const dex::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001507 if (dex_class_def == nullptr) {
1508 // Generated classes have no class def.
1509 return nullptr;
1510 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001511 return dex_file.GetSourceFile(*dex_class_def);
1512}
1513
1514std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001515 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001516 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001517 return dex_cache->GetLocation()->ToModifiedUtf8();
1518 }
1519 // Arrays and proxies are generated and have no corresponding dex file location.
1520 return "generated class";
1521}
1522
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001523const dex::TypeList* Class::GetInterfaceTypeList() {
1524 const dex::ClassDef* class_def = GetClassDef();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001525 if (class_def == nullptr) {
1526 return nullptr;
1527 }
1528 return GetDexFile().GetInterfacesList(*class_def);
1529}
1530
Andreas Gampe542451c2016-07-26 09:02:02 -07001531void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00001532 ObjPtr<PointerArray> table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001533 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001534 const size_t table_length = table->GetLength();
1535 SetEmbeddedVTableLength(table_length);
1536 for (size_t i = 0; i < table_length; i++) {
1537 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001538 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001539 // Keep java.lang.Object class's vtable around for since it's easier
1540 // to be reused by array classes during their linking.
1541 if (!IsObjectClass()) {
1542 SetVTable(nullptr);
1543 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001544}
1545
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001546class ReadBarrierOnNativeRootsVisitor {
1547 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001548 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001549 MemberOffset offset ATTRIBUTE_UNUSED,
1550 bool is_static ATTRIBUTE_UNUSED) const {}
1551
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001552 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001553 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001554 if (!root->IsNull()) {
1555 VisitRoot(root);
1556 }
1557 }
1558
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001559 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001560 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001561 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1562 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001563 if (old_ref != new_ref) {
1564 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1565 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001566 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001567 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001568 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1569 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001570 }
1571 }
1572};
1573
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001574// The pre-fence visitor for Class::CopyOf().
1575class CopyClassVisitor {
1576 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001577 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001578 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001579 size_t new_length,
1580 size_t copy_bytes,
1581 ImTable* imt,
1582 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001583 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001584 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001585 }
1586
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001587 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001588 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001589 StackHandleScope<1> hs(self_);
1590 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001591 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001592 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001593 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1594 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001595 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001596 // Visit all of the references to make sure there is no from space references in the native
1597 // roots.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001598 h_new_class_obj->Object::VisitReferences(ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001599 }
1600
1601 private:
1602 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001603 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001604 const size_t new_length_;
1605 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001606 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001607 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001608 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1609};
1610
Vladimir Marko3068d582019-05-28 16:39:29 +01001611ObjPtr<Class> Class::CopyOf(Handle<Class> h_this,
1612 Thread* self,
1613 int32_t new_length,
1614 ImTable* imt,
1615 PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001616 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1617 // We may get copied by a compacting GC.
Vladimir Marko317892b2018-05-31 11:11:32 +01001618 Runtime* runtime = Runtime::Current();
1619 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001620 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1621 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001622 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001623 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001624 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko991cd5c2019-05-30 14:23:39 +01001625 heap->AllocObject(self, java_lang_Class, new_length, visitor) :
1626 heap->AllocNonMovableObject(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001627 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001628 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001629 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001630 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001631 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001632}
1633
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001634bool Class::ProxyDescriptorEquals(const char* match) {
1635 DCHECK(IsProxyClass());
Vladimir Marko3892e622019-03-15 15:22:18 +00001636 std::string storage;
1637 const char* descriptor = GetDescriptor(&storage);
1638 DCHECK(descriptor == storage.c_str());
1639 return storage == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001640}
1641
Mathieu Chartiere401d142015-04-22 13:56:20 -07001642// TODO: Move this to java_lang_Class.cc?
1643ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001644 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001645 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001646 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001647 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001648 continue;
1649 }
1650 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001651 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001652 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001653 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001655 return nullptr;
1656 }
1657 }
1658 return nullptr;
1659}
1660
Mathieu Chartiere401d142015-04-22 13:56:20 -07001661uint32_t Class::Depth() {
1662 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001663 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664 depth++;
1665 }
1666 return depth;
1667}
1668
Andreas Gampea5b09a62016-11-17 15:21:22 -08001669dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001670 std::string temp;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001671 const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001672 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001673}
1674
David Brazdil4bcd6572019-02-02 20:08:44 +00001675ALWAYS_INLINE
1676static bool IsMethodPreferredOver(ArtMethod* orig_method,
1677 bool orig_method_hidden,
1678 ArtMethod* new_method,
1679 bool new_method_hidden) {
1680 DCHECK(new_method != nullptr);
1681
1682 // Is this the first result?
1683 if (orig_method == nullptr) {
1684 return true;
1685 }
1686
1687 // Original method is hidden, the new one is not?
1688 if (orig_method_hidden && !new_method_hidden) {
1689 return true;
1690 }
1691
1692 // We iterate over virtual methods first and then over direct ones,
1693 // so we can never be in situation where `orig_method` is direct and
1694 // `new_method` is virtual.
1695 DCHECK(!orig_method->IsDirect() || new_method->IsDirect());
1696
1697 // Original method is synthetic, the new one is not?
1698 if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) {
1699 return true;
1700 }
1701
1702 return false;
1703}
1704
Vladimir Markob6f4c792020-05-04 15:37:29 +01001705template <PointerSize kPointerSize>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001706ObjPtr<Method> Class::GetDeclaredMethodInternal(
1707 Thread* self,
1708 ObjPtr<Class> klass,
1709 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001710 ObjPtr<ObjectArray<Class>> args,
1711 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) {
1712 // Covariant return types (or smali) permit the class to define
1713 // multiple methods with the same name and parameter types.
1714 // Prefer (in decreasing order of importance):
1715 // 1) non-hidden method over hidden
1716 // 2) virtual methods over direct
1717 // 3) non-synthetic methods over synthetic
1718 // We never return miranda methods that were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001719 StackHandleScope<3> hs(self);
1720 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001721 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001722 ThrowNullPointerException("name == null");
1723 return nullptr;
1724 }
1725 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001726 Handle<Class> h_klass = hs.NewHandle(klass);
David Brazdil4bcd6572019-02-02 20:08:44 +00001727 constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001728 ArtMethod* result = nullptr;
David Brazdil4bcd6572019-02-02 20:08:44 +00001729 bool result_hidden = false;
Andreas Gampee01e3642016-07-25 13:06:04 -07001730 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
David Brazdil4bcd6572019-02-02 20:08:44 +00001731 if (m.IsMiranda()) {
1732 continue;
1733 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001734 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001735 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001736 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001737 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1738 if (UNLIKELY(self->IsExceptionPending())) {
1739 return nullptr;
1740 }
1741 continue;
1742 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001743 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1744 if (!m_hidden && !m.IsSynthetic()) {
1745 // Non-hidden, virtual, non-synthetic. Best possible result, exit early.
Vladimir Markob6f4c792020-05-04 15:37:29 +01001746 return Method::CreateFromArtMethod<kPointerSize>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001747 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1748 // Remember as potential result.
1749 result = &m;
1750 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001751 }
1752 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001753
1754 if ((result != nullptr) && !result_hidden) {
1755 // We have not found a non-hidden, virtual, non-synthetic method, but
1756 // if we have found a non-hidden, virtual, synthetic method, we cannot
1757 // do better than that later.
1758 DCHECK(!result->IsDirect());
1759 DCHECK(result->IsSynthetic());
1760 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -07001761 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001762 auto modifiers = m.GetAccessFlags();
1763 if ((modifiers & kAccConstructor) != 0) {
1764 continue;
1765 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001766 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001767 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001768 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001769 if (np_name == nullptr) {
1770 self->AssertPendingException();
1771 return nullptr;
1772 }
1773 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1774 if (UNLIKELY(self->IsExceptionPending())) {
1775 return nullptr;
1776 }
1777 continue;
1778 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001779 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
David Brazdil4bcd6572019-02-02 20:08:44 +00001780 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1781 if (!m_hidden && !m.IsSynthetic()) {
1782 // Non-hidden, direct, non-synthetic. Any virtual result could only have been
1783 // hidden, therefore this is the best possible match. Exit now.
1784 DCHECK((result == nullptr) || result_hidden);
Vladimir Markob6f4c792020-05-04 15:37:29 +01001785 return Method::CreateFromArtMethod<kPointerSize>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001786 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1787 // Remember as potential result.
1788 result = &m;
1789 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001790 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001791 }
1792 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001793
Andreas Gampebc4d2182016-02-22 10:03:12 -08001794 return result != nullptr
Vladimir Markob6f4c792020-05-04 15:37:29 +01001795 ? Method::CreateFromArtMethod<kPointerSize>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001796 : nullptr;
1797}
1798
1799template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001800ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001801 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001802 ObjPtr<Class> klass,
1803 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001804 ObjPtr<ObjectArray<Class>> args,
1805 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001806template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001807ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001808 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001809 ObjPtr<Class> klass,
1810 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001811 ObjPtr<ObjectArray<Class>> args,
1812 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001813
Vladimir Markob6f4c792020-05-04 15:37:29 +01001814template <PointerSize kPointerSize>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001815ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001816 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001817 ObjPtr<Class> klass,
1818 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001819 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001820 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001821 return result != nullptr
Vladimir Markob6f4c792020-05-04 15:37:29 +01001822 ? Constructor::CreateFromArtMethod<kPointerSize>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001823 : nullptr;
1824}
1825
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001826// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001827
Andreas Gampe542451c2016-07-26 09:02:02 -07001828template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001829ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001830 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001831 ObjPtr<Class> klass,
1832 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001833template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001834ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001835 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001836 ObjPtr<Class> klass,
1837 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001838
Andreas Gampe715fdc22016-04-18 17:07:30 -07001839int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1840 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1841 return default_value;
1842 }
1843 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001844 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001845 return default_value;
1846 }
1847 return flags;
1848}
1849
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001850void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1851 if (Runtime::Current()->IsActiveTransaction()) {
1852 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1853 } else {
1854 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1855 }
1856}
1857
David Sehr709b0702016-10-13 09:12:37 -07001858std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1859 if (klass == nullptr) {
1860 return "null";
1861 }
1862 return klass->PrettyDescriptor();
1863}
1864
1865std::string Class::PrettyDescriptor() {
1866 std::string temp;
1867 return art::PrettyDescriptor(GetDescriptor(&temp));
1868}
1869
1870std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1871 if (c == nullptr) {
1872 return "null";
1873 }
1874 return c->PrettyClass();
1875}
1876
1877std::string Class::PrettyClass() {
1878 std::string result;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +00001879 if (IsObsoleteObject()) {
1880 result += "(Obsolete)";
1881 }
1882 if (IsRetired()) {
1883 result += "(Retired)";
1884 }
David Sehr709b0702016-10-13 09:12:37 -07001885 result += "java.lang.Class<";
1886 result += PrettyDescriptor();
1887 result += ">";
1888 return result;
1889}
1890
1891std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1892 if (c == nullptr) {
1893 return "null";
1894 }
1895 return c->PrettyClassAndClassLoader();
1896}
1897
1898std::string Class::PrettyClassAndClassLoader() {
1899 std::string result;
1900 result += "java.lang.Class<";
1901 result += PrettyDescriptor();
1902 result += ",";
1903 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1904 // TODO: add an identifying hash value for the loader
1905 result += ">";
1906 return result;
1907}
1908
Andreas Gampe90b936d2017-01-31 08:58:55 -08001909template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1910 // Check class is loaded/retired or this is java.lang.String that has a
1911 // circularity issue during loading the names of its members
1912 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1913 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001914 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001915 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1916 << " IsRetired=" << IsRetired<kVerifyFlags>()
1917 << " IsErroneous=" <<
1918 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001919 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001920 << " status= " << GetStatus<kVerifyFlags>()
1921 << " descriptor=" << PrettyDescriptor();
1922}
1923// Instantiate the common cases.
1924template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1925template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1926template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1927template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1928template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1929
Andreas Gampe62f6e902018-10-11 18:58:50 -07001930void Class::SetAccessFlagsDCheck(uint32_t new_access_flags) {
1931 uint32_t old_access_flags = GetField32<kVerifyNone>(AccessFlagsOffset());
1932 // kAccVerificationAttempted is retained.
1933 CHECK((old_access_flags & kAccVerificationAttempted) == 0 ||
1934 (new_access_flags & kAccVerificationAttempted) != 0);
1935}
1936
Alex Lightbc19b752019-12-02 18:54:13 +00001937ObjPtr<Object> Class::GetMethodIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001938 ObjPtr<ClassExt> ext(GetExtData());
1939 if (ext.IsNull()) {
1940 return nullptr;
1941 } else {
1942 return ext->GetJMethodIDs();
1943 }
1944}
Alex Lightbc19b752019-12-02 18:54:13 +00001945bool Class::EnsureMethodIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001946 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001947 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001948 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1949 if (ext.IsNull()) {
1950 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001951 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001952 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001953 return ext->EnsureJMethodIDsArrayPresent(h_this->NumMethods());
Alex Light79d6c802019-06-27 15:50:11 +00001954}
1955
Alex Lightbc19b752019-12-02 18:54:13 +00001956ObjPtr<Object> Class::GetStaticFieldIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001957 ObjPtr<ClassExt> ext(GetExtData());
1958 if (ext.IsNull()) {
1959 return nullptr;
1960 } else {
1961 return ext->GetStaticJFieldIDs();
1962 }
1963}
Alex Lightbc19b752019-12-02 18:54:13 +00001964bool Class::EnsureStaticFieldIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001965 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001966 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001967 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1968 if (ext.IsNull()) {
1969 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001970 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001971 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001972 return ext->EnsureStaticJFieldIDsArrayPresent(h_this->NumStaticFields());
Alex Light79d6c802019-06-27 15:50:11 +00001973}
Alex Lightbc19b752019-12-02 18:54:13 +00001974ObjPtr<Object> Class::GetInstanceFieldIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001975 ObjPtr<ClassExt> ext(GetExtData());
1976 if (ext.IsNull()) {
1977 return nullptr;
1978 } else {
1979 return ext->GetInstanceJFieldIDs();
1980 }
1981}
Alex Lightbc19b752019-12-02 18:54:13 +00001982bool Class::EnsureInstanceFieldIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001983 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001984 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001985 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1986 if (ext.IsNull()) {
1987 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001988 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001989 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001990 return ext->EnsureInstanceJFieldIDsArrayPresent(h_this->NumInstanceFields());
Alex Light79d6c802019-06-27 15:50:11 +00001991}
1992
1993size_t Class::GetStaticFieldIdOffset(ArtField* field) {
1994 DCHECK_LT(reinterpret_cast<uintptr_t>(field),
1995 reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->end()))
1996 << "field not part of the current class. " << field->PrettyField() << " class is "
1997 << PrettyClass();
1998 DCHECK_GE(reinterpret_cast<uintptr_t>(field),
1999 reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->begin()))
2000 << "field not part of the current class. " << field->PrettyField() << " class is "
2001 << PrettyClass();
2002 uintptr_t start = reinterpret_cast<uintptr_t>(&GetSFieldsPtr()->At(0));
2003 uintptr_t fld = reinterpret_cast<uintptr_t>(field);
2004 size_t res = (fld - start) / sizeof(ArtField);
2005 DCHECK_EQ(&GetSFieldsPtr()->At(res), field)
2006 << "Incorrect field computation expected: " << field->PrettyField()
2007 << " got: " << GetSFieldsPtr()->At(res).PrettyField();
2008 return res;
2009}
2010
2011size_t Class::GetInstanceFieldIdOffset(ArtField* field) {
2012 DCHECK_LT(reinterpret_cast<uintptr_t>(field),
2013 reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->end()))
2014 << "field not part of the current class. " << field->PrettyField() << " class is "
2015 << PrettyClass();
2016 DCHECK_GE(reinterpret_cast<uintptr_t>(field),
2017 reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->begin()))
2018 << "field not part of the current class. " << field->PrettyField() << " class is "
2019 << PrettyClass();
2020 uintptr_t start = reinterpret_cast<uintptr_t>(&GetIFieldsPtr()->At(0));
2021 uintptr_t fld = reinterpret_cast<uintptr_t>(field);
2022 size_t res = (fld - start) / sizeof(ArtField);
2023 DCHECK_EQ(&GetIFieldsPtr()->At(res), field)
2024 << "Incorrect field computation expected: " << field->PrettyField()
2025 << " got: " << GetIFieldsPtr()->At(res).PrettyField();
2026 return res;
2027}
2028
2029size_t Class::GetMethodIdOffset(ArtMethod* method, PointerSize pointer_size) {
2030 DCHECK(GetMethodsSlice(kRuntimePointerSize).Contains(method))
2031 << "method not part of the current class. " << method->PrettyMethod() << "( " << reinterpret_cast<void*>(method) << ")" << " class is "
2032 << PrettyClass() << [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
2033 std::ostringstream os;
2034 os << " Methods are [";
2035 for (ArtMethod& m : GetMethodsSlice(kRuntimePointerSize)) {
2036 os << m.PrettyMethod() << "( " << reinterpret_cast<void*>(&m) << "), ";
2037 }
2038 os << "]";
2039 return os.str();
2040 }();
2041 uintptr_t start = reinterpret_cast<uintptr_t>(&*GetMethodsSlice(pointer_size).begin());
2042 uintptr_t fld = reinterpret_cast<uintptr_t>(method);
2043 size_t art_method_size = ArtMethod::Size(pointer_size);
2044 size_t art_method_align = ArtMethod::Alignment(pointer_size);
2045 size_t res = (fld - start) / art_method_size;
2046 DCHECK_EQ(&GetMethodsPtr()->At(res, art_method_size, art_method_align), method)
2047 << "Incorrect method computation expected: " << method->PrettyMethod()
2048 << " got: " << GetMethodsPtr()->At(res, art_method_size, art_method_align).PrettyMethod();
2049 return res;
2050}
2051
Nicolas Geoffrayaf61f502021-03-31 16:03:50 +00002052ArtMethod* Class::FindAccessibleInterfaceMethod(ArtMethod* implementation_method,
2053 PointerSize pointer_size)
2054 REQUIRES_SHARED(Locks::mutator_lock_) {
2055 ObjPtr<mirror::IfTable> iftable = GetIfTable();
2056 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
2057 ObjPtr<mirror::PointerArray> methods = iftable->GetMethodArrayOrNull(i);
2058 if (methods == nullptr) {
2059 continue;
2060 }
2061 for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) {
2062 if (implementation_method == methods->GetElementPtrSize<ArtMethod*>(j, pointer_size)) {
2063 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
2064 ArtMethod* interface_method = &iface->GetVirtualMethodsSlice(pointer_size)[j];
2065 // If the interface method is part of the public SDK, return it.
2066 if ((hiddenapi::GetRuntimeFlags(interface_method) & kAccPublicApi) != 0) {
2067 return interface_method;
2068 }
2069 }
2070 }
2071 }
2072 return nullptr;
2073}
2074
2075
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002076} // namespace mirror
2077} // namespace art