blob: 3199db3b89482b059184d4cc328c1c8940ad87dc [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
255 if (!class_linker_initialized) {
256 // When the class linker is being initialized its single threaded and by definition there can be
257 // no waiters. During initialization classes may appear temporary but won't be retired as their
258 // size was statically computed.
259 } else {
260 // Classes that are being resolved or initialized need to notify waiters that the class status
261 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700262 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
264 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000265 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
266 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700267 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700268 }
Vladimir Marko422a9eb2019-08-01 12:54:07 +0100269 } else if (old_status == ClassStatus::kInitialized) {
270 // Do not notify for transition from kInitialized to ClassStatus::kVisiblyInitialized.
271 // This is a hidden transition, not observable by bytecode.
272 DCHECK_EQ(new_status, ClassStatus::kVisiblyInitialized); // Already CHECK()ed above.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700273 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000274 CHECK_NE(new_status, ClassStatus::kRetired);
275 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700276 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700277 }
278 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700279 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280}
281
Vladimir Marko70e2a762019-07-12 16:49:00 +0100282void Class::SetStatusForPrimitiveOrArray(ClassStatus new_status) {
283 DCHECK(IsPrimitive<kVerifyNone>() || IsArrayClass<kVerifyNone>());
284 DCHECK(!IsErroneous(new_status));
285 DCHECK(!IsErroneous(GetStatus<kVerifyNone>()));
286 DCHECK_GT(new_status, GetStatus<kVerifyNone>());
287
288 if (kBitstringSubtypeCheckEnabled) {
289 LOG(FATAL) << "Unimplemented";
290 }
291 // The ClassStatus is always in the 4 most-significant bits of status_.
292 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
293 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
294 // Use normal store. For primitives and core arrays classes (Object[],
295 // Class[], String[] and primitive arrays), the status is set while the
296 // process is still single threaded. For other arrays classes, it is set
297 // in a pre-fence visitor which initializes all fields and the subsequent
298 // fence together with address dependency shall ensure memory visibility.
299 SetField32</*kTransactionActive=*/ false,
300 /*kCheckTransaction=*/ false,
301 kVerifyNone>(StatusOffset(), new_status_value);
302
303 // Do not update `object_alloc_fast_path_`. Arrays are variable size and
304 // instances of primitive classes cannot be created at all.
305
Vladimir Marko70e2a762019-07-12 16:49:00 +0100306 // There can be no waiters to notify as these classes are initialized
307 // before another thread can see them.
308}
309
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700310void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700311 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312}
313
Ian Rogersef7d42f2014-01-06 12:55:46 -0800314void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700316 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
317 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700318 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700319 }
Vladimir Markob68bb7a2020-03-17 10:55:25 +0000320 SetField32</*kTransactionActive=*/ false, /*kCheckTransaction=*/ false>(
321 OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322}
323
Alex Light270db1c2019-12-03 12:20:01 +0000324ObjPtr<Class> Class::GetObsoleteClass() {
325 ObjPtr<ClassExt> ext(GetExtData());
326 if (ext.IsNull()) {
327 return nullptr;
328 } else {
329 return ext->GetObsoleteClass();
330 }
331}
332
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333// Return the class' name. The exact format is bizarre, but it's the specified behavior for
334// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
335// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
336// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Vladimir Marko179b7c62019-03-22 13:38:57 +0000337ObjPtr<String> Class::ComputeName(Handle<Class> h_this) {
338 ObjPtr<String> name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800339 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 return name;
341 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700342 std::string temp;
343 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800344 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
346 // The descriptor indicates that this is the class for
347 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700348 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 switch (descriptor[0]) {
350 case 'Z': c_name = "boolean"; break;
351 case 'B': c_name = "byte"; break;
352 case 'C': c_name = "char"; break;
353 case 'S': c_name = "short"; break;
354 case 'I': c_name = "int"; break;
355 case 'J': c_name = "long"; break;
356 case 'F': c_name = "float"; break;
357 case 'D': c_name = "double"; break;
358 case 'V': c_name = "void"; break;
359 default:
360 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
361 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800362 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 } else {
364 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
365 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700366 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700368 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 return name;
370}
371
Ian Rogersef7d42f2014-01-06 12:55:46 -0800372void Class::DumpClass(std::ostream& os, int flags) {
Vladimir Markob10668c2021-06-10 09:52:53 +0100373 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700375 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 if ((flags & kDumpClassClassLoader) != 0) {
377 os << ' ' << GetClassLoader();
378 }
379 if ((flags & kDumpClassInitialized) != 0) {
380 os << ' ' << GetStatus();
381 }
382 os << "\n";
383 return;
384 }
385
Vladimir Markob10668c2021-06-10 09:52:53 +0100386 ObjPtr<Class> super = GetSuperClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700388
Ian Rogers1ff3c982014-08-12 02:30:58 -0700389 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Vladimir Markob10668c2021-06-10 09:52:53 +0100391 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n"
392 << " objectSize=" << SizeOf() << " "
393 << "(" << (super != nullptr ? super->SizeOf() : -1) << " from super)\n"
394 << StringPrintf(" access=0x%04x.%04x\n",
395 GetAccessFlags() >> 16,
396 GetAccessFlags() & kAccJavaFlagsMask);
397 if (super != nullptr) {
398 os << " super='" << super->PrettyClass() << "' (cl=" << super->GetClassLoader() << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 }
400 if (IsArrayClass()) {
401 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
402 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700403 const size_t num_direct_interfaces = NumDirectInterfaces();
404 if (num_direct_interfaces > 0) {
405 os << " interfaces (" << num_direct_interfaces << "):\n";
406 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +0100407 ObjPtr<Class> interface = GetDirectInterface(i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700408 if (interface == nullptr) {
409 os << StringPrintf(" %2zd: nullptr!\n", i);
410 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700411 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
412 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700413 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 }
415 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700416 if (!IsLoaded()) {
417 os << " class not yet loaded";
418 } else {
Vladimir Markob10668c2021-06-10 09:52:53 +0100419 os << " vtable (" << NumVirtualMethods() << " entries, "
420 << (super != nullptr ? super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700421 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700422 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Vladimir Markob10668c2021-06-10 09:52:53 +0100423 GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100425 os << " direct methods (" << NumDirectMethods() << " entries):\n";
426 for (size_t i = 0; i < NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700427 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Vladimir Markob10668c2021-06-10 09:52:53 +0100428 GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700429 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100430 if (NumStaticFields() > 0) {
431 os << " static fields (" << NumStaticFields() << " entries):\n";
432 if (IsResolved()) {
433 for (size_t i = 0; i < NumStaticFields(); ++i) {
434 os << StringPrintf(" %2zd: %s\n", i, ArtField::PrettyField(GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700435 }
436 } else {
437 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700439 }
Vladimir Markob10668c2021-06-10 09:52:53 +0100440 if (NumInstanceFields() > 0) {
441 os << " instance fields (" << NumInstanceFields() << " entries):\n";
442 if (IsResolved()) {
443 for (size_t i = 0; i < NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700444 os << StringPrintf(" %2zd: %s\n", i,
Vladimir Markob10668c2021-06-10 09:52:53 +0100445 ArtField::PrettyField(GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700446 }
447 } else {
448 os << " <not yet available>";
449 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 }
451 }
452}
453
454void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
David Srbecky346fd962020-07-27 16:51:00 +0100456 // Check that the number of bits set in the reference offset bitmap
457 // agrees with the number of references.
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700458 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700459 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460 count += c->NumReferenceInstanceFieldsDuringLinking();
461 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700462 // +1 for the Class in Object.
463 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800464 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100465 // Not called within a transaction.
466 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700467 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468}
469
Vladimir Markoe027d722019-02-05 10:13:49 +0000470bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700472 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
473 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474 ++i;
475 }
Vladimir Markoe027d722019-02-05 10:13:49 +0000476 if (descriptor1.find('/', i) != std::string_view::npos ||
477 descriptor2.find('/', i) != std::string_view::npos) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 return false;
479 } else {
480 return true;
481 }
482}
483
Mathieu Chartier3398c782016-09-30 10:27:43 -0700484bool Class::IsInSamePackage(ObjPtr<Class> that) {
485 ObjPtr<Class> klass1 = this;
486 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 if (klass1 == klass2) {
488 return true;
489 }
490 // Class loaders must match.
491 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
492 return false;
493 }
494 // Arrays are in the same package when their element classes are.
495 while (klass1->IsArrayClass()) {
496 klass1 = klass1->GetComponentType();
497 }
498 while (klass2->IsArrayClass()) {
499 klass2 = klass2->GetComponentType();
500 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700501 // trivial check again for array types
502 if (klass1 == klass2) {
503 return true;
504 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700506 std::string temp1, temp2;
507 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508}
509
Ian Rogersef7d42f2014-01-06 12:55:46 -0800510bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100511 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512}
513
Vladimir Markoba118822017-06-12 15:41:56 +0100514template <typename SignatureType>
515static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000516 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100517 const SignatureType& signature,
518 PointerSize pointer_size)
519 REQUIRES_SHARED(Locks::mutator_lock_) {
520 // If the current class is not an interface, skip the search of its declared methods;
521 // such lookup is used only to distinguish between IncompatibleClassChangeError and
522 // NoSuchMethodError and the caller has already tried to search methods in the class.
523 if (LIKELY(klass->IsInterface())) {
524 // Search declared methods, both direct and virtual.
525 // (This lookup is used also for invoke-static on interface classes.)
526 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700527 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100528 return &method;
529 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 }
531 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700532
Vladimir Markoba118822017-06-12 15:41:56 +0100533 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
534 // we should return it, otherwise an arbitrary one can be returned.
535 ObjPtr<IfTable> iftable = klass->GetIfTable();
536 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
537 ObjPtr<Class> iface = iftable->GetInterface(i);
538 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700539 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100540 return &method;
541 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700542 }
543 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544
Vladimir Markoba118822017-06-12 15:41:56 +0100545 // Then search for public non-static methods in the java.lang.Object.
546 if (LIKELY(klass->IsInterface())) {
547 ObjPtr<Class> object_class = klass->GetSuperClass();
548 DCHECK(object_class->IsObjectClass());
549 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
550 if (method.IsPublic() && !method.IsStatic() &&
Eric Holkabdb4592019-05-16 08:33:12 -0700551 method.GetNameView() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700552 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 }
554 }
555 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700556 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557}
558
Vladimir Markoe027d722019-02-05 10:13:49 +0000559ArtMethod* Class::FindInterfaceMethod(std::string_view name,
560 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100561 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000562 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563}
564
Vladimir Markoe027d722019-02-05 10:13:49 +0000565ArtMethod* Class::FindInterfaceMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100566 const Signature& signature,
567 PointerSize pointer_size) {
568 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700569}
570
Vladimir Markoba118822017-06-12 15:41:56 +0100571ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
572 uint32_t dex_method_idx,
573 PointerSize pointer_size) {
574 // We always search by name and signature, ignoring the type index in the MethodId.
575 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800576 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Eric Holkabdb4592019-05-16 08:33:12 -0700577 std::string_view name = dex_file.StringViewByIdx(method_id.name_idx_);
Vladimir Markoba118822017-06-12 15:41:56 +0100578 const Signature signature = dex_file.GetMethodSignature(method_id);
579 return FindInterfaceMethod(name, signature, pointer_size);
580}
581
Alex Lightafb66472017-08-01 09:54:49 -0700582static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
583 ObjPtr<mirror::Class> declaring_class)
584 REQUIRES_SHARED(Locks::mutator_lock_) {
585 if (klass->IsArrayClass()) {
586 return declaring_class->IsObjectClass();
587 } else if (klass->IsInterface()) {
588 return declaring_class->IsObjectClass() || declaring_class == klass;
589 } else {
590 return klass->IsSubClass(declaring_class);
591 }
592}
593
Vladimir Markoba118822017-06-12 15:41:56 +0100594static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
595 ObjPtr<mirror::Class> declaring_class,
596 ArtMethod& method)
597 REQUIRES_SHARED(Locks::mutator_lock_) {
598 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
599 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700600 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100601 uint32_t access_flags = method.GetAccessFlags();
602 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
603 return true;
604 }
605 if ((access_flags & kAccPrivate) != 0) {
606 return false;
607 }
608 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
609 if (!klass->IsInSamePackage(declaring_class)) {
610 return false;
611 }
612 }
613 return true;
614}
615
616template <typename SignatureType>
617static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000618 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100619 const SignatureType& signature,
620 PointerSize pointer_size)
621 REQUIRES_SHARED(Locks::mutator_lock_) {
622 // Search declared methods first.
623 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
624 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
Vladimir Marko4573be32021-06-07 11:07:05 +0100625 if (np_method->GetNameView() == name && np_method->GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100626 return &method;
627 }
628 }
629
630 // Then search the superclass chain. If we find an inherited method, return it.
631 // If we find a method that's not inherited because of access restrictions,
632 // try to find a method inherited from an interface in copied methods.
633 ObjPtr<Class> klass = this_klass->GetSuperClass();
634 ArtMethod* uninherited_method = nullptr;
635 for (; klass != nullptr; klass = klass->GetSuperClass()) {
636 DCHECK(!klass->IsProxyClass());
637 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100638 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100639 if (IsInheritedMethod(this_klass, klass, method)) {
640 return &method;
641 }
642 uninherited_method = &method;
643 break;
644 }
645 }
646 if (uninherited_method != nullptr) {
647 break;
648 }
649 }
650
651 // Then search copied methods.
652 // If we found a method that's not inherited, stop the search in its declaring class.
653 ObjPtr<Class> end_klass = klass;
654 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
655 klass = this_klass;
656 if (UNLIKELY(klass->IsProxyClass())) {
657 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
658 klass = klass->GetSuperClass();
659 }
660 for (; klass != end_klass; klass = klass->GetSuperClass()) {
661 DCHECK(!klass->IsProxyClass());
662 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100663 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100664 return &method; // No further check needed, copied methods are inherited by definition.
665 }
666 }
667 }
668 return uninherited_method; // Return the `uninherited_method` if any.
669}
670
671
Vladimir Markoe027d722019-02-05 10:13:49 +0000672ArtMethod* Class::FindClassMethod(std::string_view name,
673 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100674 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000675 return FindClassMethodWithSignature(this, name, signature, pointer_size);
Vladimir Markoba118822017-06-12 15:41:56 +0100676}
677
Vladimir Markoe027d722019-02-05 10:13:49 +0000678ArtMethod* Class::FindClassMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100679 const Signature& signature,
680 PointerSize pointer_size) {
681 return FindClassMethodWithSignature(this, name, signature, pointer_size);
682}
683
Vladimir Marko17769472021-06-23 10:40:29 +0100684// Binary search a range with a three-way compare function.
685//
686// Return a tuple consisting of a `success` value, the index of the match (`mid`) and
687// the remaining range when we found the match (`begin` and `end`). This is useful for
688// subsequent binary search with a secondary comparator, see `ClassMemberBinarySearch()`.
689template <typename Compare>
690ALWAYS_INLINE
691std::tuple<bool, uint32_t, uint32_t, uint32_t> BinarySearch(uint32_t begin,
692 uint32_t end,
693 Compare&& cmp)
694 REQUIRES_SHARED(Locks::mutator_lock_) {
695 while (begin != end) {
696 uint32_t mid = (begin + end) >> 1;
697 auto cmp_result = cmp(mid);
698 if (cmp_result == 0) {
699 return {true, mid, begin, end};
700 }
701 if (cmp_result > 0) {
702 begin = mid + 1u;
703 } else {
704 end = mid;
705 }
706 }
707 return {false, 0u, 0u, 0u};
708}
709
710// Binary search for class members. The range passed to this search must be sorted, so
711// declared methods or fields cannot be searched directly but declared direct methods,
712// declared virtual methods, declared static fields or declared instance fields can.
713template <typename NameCompare, typename SecondCompare, typename NameIndexGetter>
714ALWAYS_INLINE
715std::tuple<bool, uint32_t> ClassMemberBinarySearch(uint32_t begin,
716 uint32_t end,
717 NameCompare&& name_cmp,
718 SecondCompare&& second_cmp,
719 NameIndexGetter&& get_name_idx)
720 REQUIRES_SHARED(Locks::mutator_lock_) {
721 // First search for the item with the given name.
722 bool success;
723 uint32_t mid;
724 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_cmp);
725 if (!success) {
726 return {false, 0u};
727 }
728 // If found, do the secondary comparison.
729 auto second_cmp_result = second_cmp(mid);
730 if (second_cmp_result == 0) {
731 return {true, mid};
732 }
733 // We have matched the name but not the secondary comparison. We no longer need to
734 // search for the name as string as we know the matching name string index.
735 // Repeat the above binary searches and secondary comparisons with a simpler name
736 // index compare until the search range contains only matching name.
737 auto name_idx = get_name_idx(mid);
738 if (second_cmp_result > 0) {
739 do {
740 begin = mid + 1u;
741 auto name_index_cmp = [&](uint32_t mid2) REQUIRES_SHARED(Locks::mutator_lock_) {
742 DCHECK_LE(name_idx, get_name_idx(mid2));
743 return (name_idx != get_name_idx(mid2)) ? -1 : 0;
744 };
745 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_index_cmp);
746 if (!success) {
747 return {false, 0u};
748 }
749 second_cmp_result = second_cmp(mid);
750 } while (second_cmp_result > 0);
751 end = mid;
752 } else {
753 do {
754 end = mid;
755 auto name_index_cmp = [&](uint32_t mid2) REQUIRES_SHARED(Locks::mutator_lock_) {
756 DCHECK_GE(name_idx, get_name_idx(mid2));
757 return (name_idx != get_name_idx(mid2)) ? 1 : 0;
758 };
759 std::tie(success, mid, begin, end) = BinarySearch(begin, end, name_index_cmp);
760 if (!success) {
761 return {false, 0u};
762 }
763 second_cmp_result = second_cmp(mid);
764 } while (second_cmp_result < 0);
765 begin = mid + 1u;
766 }
767 if (second_cmp_result == 0) {
768 return {true, mid};
769 }
770 // All items in the remaining range have a matching name, so search with secondary comparison.
771 std::tie(success, mid, std::ignore, std::ignore) = BinarySearch(begin, end, second_cmp);
772 return {success, mid};
773}
774
775static std::tuple<bool, ArtMethod*> FindDeclaredClassMethod(ObjPtr<mirror::Class> klass,
776 const DexFile& dex_file,
777 std::string_view name,
778 Signature signature,
779 PointerSize pointer_size)
780 REQUIRES_SHARED(Locks::mutator_lock_) {
781 DCHECK(&klass->GetDexFile() == &dex_file);
782 DCHECK(!name.empty());
783
784 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
785 DCHECK(!declared_methods.empty());
786 auto get_method_id = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE
787 -> const dex::MethodId& {
788 ArtMethod& method = declared_methods[mid];
789 DCHECK(method.GetDexFile() == &dex_file);
790 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
791 return dex_file.GetMethodId(method.GetDexMethodIndex());
792 };
793 auto name_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
794 // Do not use ArtMethod::GetNameView() to avoid reloading dex file through the same
795 // declaring class from different methods and also avoid the runtime method check.
796 const dex::MethodId& method_id = get_method_id(mid);
797 return name.compare(dex_file.GetMethodNameView(method_id));
798 };
799 auto signature_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
800 // Do not use ArtMethod::GetSignature() to avoid reloading dex file through the same
801 // declaring class from different methods and also avoid the runtime method check.
802 const dex::MethodId& method_id = get_method_id(mid);
803 return signature.Compare(dex_file.GetMethodSignature(method_id));
804 };
805 auto get_name_idx = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
806 const dex::MethodId& method_id = get_method_id(mid);
807 return method_id.name_idx_;
808 };
809
810 // Use binary search in the sorted direct methods, then in the sorted virtual methods.
811 uint32_t num_direct_methods = klass->NumDirectMethods();
812 uint32_t num_declared_methods = dchecked_integral_cast<uint32_t>(declared_methods.size());
813 DCHECK_LE(num_direct_methods, num_declared_methods);
814 const uint32_t ranges[2][2] = {
815 {0u, num_direct_methods}, // Declared direct methods.
816 {num_direct_methods, num_declared_methods} // Declared virtual methods.
817 };
818 for (const uint32_t (&range)[2] : ranges) {
819 auto [success, mid] =
820 ClassMemberBinarySearch(range[0], range[1], name_cmp, signature_cmp, get_name_idx);
821 if (success) {
822 return {true, &declared_methods[mid]};
823 }
824 }
825
826 // Did not find a declared method in either slice.
827 return {false, nullptr};
828}
829
Vladimir Marko4573be32021-06-07 11:07:05 +0100830FLATTEN
Vladimir Markoba118822017-06-12 15:41:56 +0100831ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
832 uint32_t dex_method_idx,
833 PointerSize pointer_size) {
834 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
835 DCHECK(!IsProxyClass());
836
837 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
838 ObjPtr<DexCache> this_dex_cache = GetDexCache();
839 if (this_dex_cache == dex_cache) {
840 // Lookup is always performed in the class referenced by the MethodId.
841 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
842 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
843 if (method.GetDexMethodIndex() == dex_method_idx) {
844 return &method;
845 }
846 }
847 }
Vladimir Marko17769472021-06-23 10:40:29 +0100848
Vladimir Markoba118822017-06-12 15:41:56 +0100849 // If not found, we need to search by name and signature.
850 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800851 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100852 const Signature signature = dex_file.GetMethodSignature(method_id);
Vladimir Marko4573be32021-06-07 11:07:05 +0100853 std::string_view name; // Do not touch the dex file string data until actually needed.
Vladimir Marko17769472021-06-23 10:40:29 +0100854
Vladimir Markoba118822017-06-12 15:41:56 +0100855 // If we do not have a dex_cache match, try to find the declared method in this class now.
856 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
857 DCHECK(name.empty());
Vladimir Marko4573be32021-06-07 11:07:05 +0100858 name = dex_file.GetMethodNameView(method_id);
Vladimir Marko17769472021-06-23 10:40:29 +0100859 auto [success, method] = FindDeclaredClassMethod(
860 this, *this_dex_cache->GetDexFile(), name, signature, pointer_size);
861 DCHECK_EQ(success, method != nullptr);
862 if (success) {
863 return method;
Vladimir Markoba118822017-06-12 15:41:56 +0100864 }
865 }
866
867 // Then search the superclass chain. If we find an inherited method, return it.
868 // If we find a method that's not inherited because of access restrictions,
869 // try to find a method inherited from an interface in copied methods.
870 ArtMethod* uninherited_method = nullptr;
871 ObjPtr<Class> klass = GetSuperClass();
872 for (; klass != nullptr; klass = klass->GetSuperClass()) {
873 ArtMethod* candidate_method = nullptr;
874 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
Vladimir Marko17769472021-06-23 10:40:29 +0100875 ObjPtr<DexCache> klass_dex_cache = klass->GetDexCache();
876 if (klass_dex_cache == dex_cache) {
Vladimir Markoba118822017-06-12 15:41:56 +0100877 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
878 // the type index differs, so compare the name index and proto index.
879 for (ArtMethod& method : declared_methods) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800880 const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
Vladimir Markoba118822017-06-12 15:41:56 +0100881 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
882 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
883 candidate_method = &method;
884 break;
885 }
886 }
Vladimir Marko4573be32021-06-07 11:07:05 +0100887 } else if (!declared_methods.empty()) {
888 if (name.empty()) {
889 name = dex_file.GetMethodNameView(method_id);
Vladimir Markoba118822017-06-12 15:41:56 +0100890 }
Vladimir Marko17769472021-06-23 10:40:29 +0100891 auto [success, method] = FindDeclaredClassMethod(
892 klass, *klass_dex_cache->GetDexFile(), name, signature, pointer_size);
893 DCHECK_EQ(success, method != nullptr);
894 if (success) {
895 candidate_method = method;
Vladimir Markoba118822017-06-12 15:41:56 +0100896 }
897 }
898 if (candidate_method != nullptr) {
899 if (IsInheritedMethod(this, klass, *candidate_method)) {
900 return candidate_method;
901 } else {
902 uninherited_method = candidate_method;
903 break;
904 }
905 }
906 }
907
908 // Then search copied methods.
909 // If we found a method that's not inherited, stop the search in its declaring class.
910 ObjPtr<Class> end_klass = klass;
911 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
912 // After we have searched the declared methods of the super-class chain,
913 // search copied methods which can contain methods from interfaces.
914 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
915 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
916 if (!copied_methods.empty() && name.empty()) {
917 name = dex_file.StringDataByIdx(method_id.name_idx_);
918 }
919 for (ArtMethod& method : copied_methods) {
Vladimir Marko4573be32021-06-07 11:07:05 +0100920 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100921 return &method; // No further check needed, copied methods are inherited by definition.
922 }
923 }
924 }
925 return uninherited_method; // Return the `uninherited_method` if any.
926}
927
Vladimir Markoe027d722019-02-05 10:13:49 +0000928ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) {
Vladimir Markoba118822017-06-12 15:41:56 +0100929 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
930 DCHECK(!IsProxyClass());
Vladimir Markoe027d722019-02-05 10:13:49 +0000931 std::string_view name("<init>");
Vladimir Markoba118822017-06-12 15:41:56 +0100932 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000933 if (method.GetName() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100934 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800935 }
936 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700937 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800938}
939
Vladimir Markoe027d722019-02-05 10:13:49 +0000940ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000941 for (auto& method : GetDirectMethods(pointer_size)) {
942 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
943 if (name == np_method->GetName()) {
944 return &method;
945 }
946 }
947 return nullptr;
948}
949
Vladimir Markoe027d722019-02-05 10:13:49 +0000950ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000951 for (auto& method : GetVirtualMethods(pointer_size)) {
952 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
953 if (name == np_method->GetName()) {
954 return &method;
955 }
956 }
957 return nullptr;
958}
959
Andreas Gampe542451c2016-07-26 09:02:02 -0700960ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700961 DCHECK(method->GetDeclaringClass()->IsInterface());
962 DCHECK(IsInterface()) << "Should only be called on a interface class";
963 // Check if we have one defined on this interface first. This includes searching copied ones to
964 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
965 // don't do any indirect method checks here.
966 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
967 if (method->HasSameNameAndSignature(&iface_method)) {
968 return &iface_method;
969 }
970 }
971
972 std::vector<ArtMethod*> abstract_methods;
973 // Search through the IFTable for a working version. We don't need to check for conflicts
974 // because if there was one it would appear in this classes virtual_methods_ above.
975
976 Thread* self = Thread::Current();
977 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700978 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
979 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700980 size_t iftable_count = GetIfTableCount();
981 // Find the method. We don't need to check for conflicts because they would have been in the
982 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
983 // subtypiest interfaces get visited first.
984 for (size_t k = iftable_count; k != 0;) {
985 k--;
986 DCHECK_LT(k, iftable->Count());
987 iface.Assign(iftable->GetInterface(k));
988 // Iterate through every declared method on this interface. Each direct method's name/signature
989 // is unique so the order of the inner loop doesn't matter.
990 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
991 ArtMethod* current_method = &method_iter;
992 if (current_method->HasSameNameAndSignature(method)) {
993 if (current_method->IsDefault()) {
994 // Handle JLS soft errors, a default method from another superinterface tree can
995 // "override" an abstract method(s) from another superinterface tree(s). To do this,
996 // ignore any [default] method which are dominated by the abstract methods we've seen so
997 // far. Check if overridden by any in abstract_methods. We do not need to check for
998 // default_conflicts because we would hit those before we get to this loop.
999 bool overridden = false;
1000 for (ArtMethod* possible_override : abstract_methods) {
1001 DCHECK(possible_override->HasSameNameAndSignature(current_method));
1002 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
1003 overridden = true;
1004 break;
1005 }
1006 }
1007 if (!overridden) {
1008 return current_method;
1009 }
1010 } else {
1011 // Is not default.
1012 // This might override another default method. Just stash it for now.
1013 abstract_methods.push_back(current_method);
1014 }
1015 }
1016 }
1017 }
1018 // If we reach here we either never found any declaration of the method (in which case
1019 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
1020 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
1021 // of these arbitrarily.
1022 return abstract_methods.empty() ? nullptr : abstract_methods[0];
1023}
1024
Andreas Gampe542451c2016-07-26 09:02:02 -07001025ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001026 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
1027 if (method.IsClassInitializer()) {
1028 DCHECK_STREQ(method.GetName(), "<clinit>");
1029 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
1030 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001031 }
1032 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001033 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -07001034}
1035
Vladimir Marko70e126a2021-06-24 14:51:34 +01001036static std::tuple<bool, ArtField*> FindFieldByNameAndType(const DexFile& dex_file,
1037 LengthPrefixedArray<ArtField>* fields,
1038 std::string_view name,
1039 std::string_view type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001040 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001041 DCHECK(fields != nullptr);
Vladimir Marko70e126a2021-06-24 14:51:34 +01001042 DCHECK(!name.empty());
1043 DCHECK(!type.empty());
1044
1045 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
1046 // verifier. There can be multiple fields with the same name in the same class due to proguard.
1047 // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as
1048 // unsigned; for Modified-UTF-8 without embedded nulls this is consistent with the
1049 // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering.
1050 auto get_field_id = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE
1051 -> const dex::FieldId& {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001052 ArtField& field = fields->At(mid);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001053 DCHECK(field.GetDexFile() == &dex_file);
Vladimir Marko70e126a2021-06-24 14:51:34 +01001054 return dex_file.GetFieldId(field.GetDexFieldIndex());
1055 };
1056 auto name_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1057 const dex::FieldId& field_id = get_field_id(mid);
1058 return name.compare(dex_file.GetFieldNameView(field_id));
1059 };
1060 auto type_cmp = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1061 const dex::FieldId& field_id = get_field_id(mid);
1062 return type.compare(dex_file.GetTypeDescriptorView(dex_file.GetTypeId(field_id.type_idx_)));
1063 };
1064 auto get_name_idx = [&](uint32_t mid) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE {
1065 const dex::FieldId& field_id = get_field_id(mid);
1066 return field_id.name_idx_;
1067 };
1068
1069 // Use binary search in the sorted fields.
1070 auto [success, mid] =
1071 ClassMemberBinarySearch(/*begin=*/ 0u, fields->size(), name_cmp, type_cmp, get_name_idx);
1072
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001073 if (kIsDebugBuild) {
1074 ArtField* found = nullptr;
1075 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
1076 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
1077 found = &field;
1078 break;
1079 }
1080 }
George Burgess IV869746e2021-06-14 14:13:14 -07001081
Vladimir Marko70e126a2021-06-24 14:51:34 +01001082 ArtField* ret = success ? &fields->At(mid) : nullptr;
1083 CHECK_EQ(found, ret)
1084 << "Found " << ArtField::PrettyField(found) << " vs " << ArtField::PrettyField(ret);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001085 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001086
1087 if (success) {
1088 return {true, &fields->At(mid)};
1089 }
1090
1091 return {false, nullptr};
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001092}
1093
Vladimir Markoe027d722019-02-05 10:13:49 +00001094ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001095 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001096 LengthPrefixedArray<ArtField>* ifields = GetIFieldsPtr();
1097 if (ifields == nullptr) {
1098 return nullptr;
1099 }
1100 DCHECK(!IsProxyClass());
Vladimir Marko70e126a2021-06-24 14:51:34 +01001101 auto [success, field] = FindFieldByNameAndType(GetDexFile(), ifields, name, type);
1102 DCHECK_EQ(success, field != nullptr);
1103 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001104}
1105
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001106ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001107 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001108 for (ArtField& field : GetIFields()) {
1109 if (field.GetDexFieldIndex() == dex_field_idx) {
1110 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001111 }
1112 }
1113 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001114 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001115}
1116
Vladimir Markoe027d722019-02-05 10:13:49 +00001117ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001118 // Is the field in this class, or any of its superclasses?
1119 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001120 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -07001121 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -07001122 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001123 return f;
1124 }
1125 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001126 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001127}
1128
Vladimir Markoe027d722019-02-05 10:13:49 +00001129ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) {
1130 DCHECK(!type.empty());
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001131 LengthPrefixedArray<ArtField>* sfields = GetSFieldsPtr();
1132 if (sfields == nullptr) {
1133 return nullptr;
1134 }
1135 if (UNLIKELY(IsProxyClass())) {
1136 // Proxy fields do not have appropriate dex field indexes required by
1137 // `FindFieldByNameAndType()`. However, each proxy class has exactly
1138 // the same artificial fields created by the `ClassLinker`.
1139 DCHECK_EQ(sfields->size(), 2u);
1140 DCHECK_EQ(strcmp(sfields->At(0).GetName(), "interfaces"), 0);
1141 DCHECK_EQ(strcmp(sfields->At(0).GetTypeDescriptor(), "[Ljava/lang/Class;"), 0);
1142 DCHECK_EQ(strcmp(sfields->At(1).GetName(), "throws"), 0);
1143 DCHECK_EQ(strcmp(sfields->At(1).GetTypeDescriptor(), "[[Ljava/lang/Class;"), 0);
1144 if (name == "interfaces") {
1145 return (type == "[Ljava/lang/Class;") ? &sfields->At(0) : nullptr;
1146 } else if (name == "throws") {
1147 return (type == "[[Ljava/lang/Class;") ? &sfields->At(1) : nullptr;
1148 } else {
1149 return nullptr;
1150 }
1151 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001152 auto [success, field] = FindFieldByNameAndType(GetDexFile(), sfields, name, type);
1153 DCHECK_EQ(success, field != nullptr);
1154 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001155}
1156
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001157ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001158 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -07001159 for (ArtField& field : GetSFields()) {
1160 if (field.GetDexFieldIndex() == dex_field_idx) {
1161 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001162 }
1163 }
1164 }
Brian Carlstrom004644f2014-06-18 08:34:01 -07001165 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001166}
1167
Vladimir Markob10668c2021-06-10 09:52:53 +01001168ArtField* Class::FindStaticField(std::string_view name, std::string_view type) {
1169 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001170 // Is the field in this class (or its interfaces), or any of its
1171 // superclasses (or their interfaces)?
Vladimir Markob10668c2021-06-10 09:52:53 +01001172 for (ObjPtr<Class> k = this; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001173 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -07001174 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001175 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001176 return f;
1177 }
1178 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +00001179 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001180 ObjPtr<Class> interface = k->GetDirectInterface(i);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001181 DCHECK(interface != nullptr);
Vladimir Markob10668c2021-06-10 09:52:53 +01001182 f = interface->FindStaticField(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 }
1187 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001188 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001189}
1190
Vladimir Marko40261172021-06-14 10:59:51 +01001191// Find a field using the JLS field resolution order.
1192// Template arguments can be used to limit the search to either static or instance fields.
1193// The search should be limited only if we know that a full search would yield a field of
1194// the right type or no field at all. This can be known for field references in a method
1195// if we have previously verified that method and did not find a field type mismatch.
1196template <bool kSearchInstanceFields, bool kSearchStaticFields>
1197ALWAYS_INLINE
1198ArtField* FindFieldImpl(ObjPtr<mirror::Class> klass,
1199 ObjPtr<mirror::DexCache> dex_cache,
1200 uint32_t field_idx) REQUIRES_SHARED(Locks::mutator_lock_) {
1201 static_assert(kSearchInstanceFields || kSearchStaticFields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001202
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001203 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
Vladimir Marko40261172021-06-14 10:59:51 +01001204 DCHECK(!klass->IsProxyClass());
Vladimir Markob10668c2021-06-10 09:52:53 +01001205
1206 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001207
1208 // First try to find a declared field by `field_idx` if we have a `dex_cache` match.
Vladimir Marko40261172021-06-14 10:59:51 +01001209 ObjPtr<DexCache> klass_dex_cache = klass->GetDexCache();
1210 if (klass_dex_cache == dex_cache) {
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001211 // Lookup is always performed in the class referenced by the FieldId.
Vladimir Marko40261172021-06-14 10:59:51 +01001212 DCHECK_EQ(klass->GetDexTypeIndex(),
1213 klass_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_);
1214 ArtField* f = kSearchInstanceFields
1215 ? klass->FindDeclaredInstanceField(klass_dex_cache, field_idx)
1216 : nullptr;
1217 if (kSearchStaticFields && f == nullptr) {
1218 f = klass->FindDeclaredStaticField(klass_dex_cache, field_idx);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001219 }
1220 if (f != nullptr) {
1221 return f;
1222 }
1223 }
1224
1225 const DexFile& dex_file = *dex_cache->GetDexFile();
1226 const dex::FieldId& field_id = dex_file.GetFieldId(field_idx);
1227
1228 std::string_view name; // Do not touch the dex file string data until actually needed.
1229 std::string_view type;
1230 auto ensure_name_and_type_initialized = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
1231 if (name.empty()) {
1232 name = dex_file.GetFieldNameView(field_id);
1233 type = dex_file.GetFieldTypeDescriptorView(field_id);
1234 }
1235 };
1236
1237 auto search_direct_interfaces = [&](ObjPtr<mirror::Class> k)
1238 REQUIRES_SHARED(Locks::mutator_lock_) {
1239 // TODO: The `FindStaticField()` performs a recursive search and it's possible to
1240 // construct interface hierarchies that make the time complexity exponential in depth.
1241 // Rewrite this with a `HashSet<mirror::Class*>` to mark classes we have already
1242 // searched for the field, so that we call `FindDeclaredStaticField()` only once
1243 // on each interface. And use a work queue to avoid unlimited recursion depth.
1244 // TODO: Once we call `FindDeclaredStaticField()` directly, use search by indexes
1245 // instead of strings if the interface's dex cache matches `dex_cache`. This shall
1246 // allow delaying the `ensure_name_and_type_initialized()` call further.
1247 uint32_t num_interfaces = k->NumDirectInterfaces();
1248 if (num_interfaces != 0u) {
1249 ensure_name_and_type_initialized();
1250 for (uint32_t i = 0; i != num_interfaces; ++i) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001251 ObjPtr<Class> interface = k->GetDirectInterface(i);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001252 DCHECK(interface != nullptr);
Vladimir Markob10668c2021-06-10 09:52:53 +01001253 ArtField* f = interface->FindStaticField(name, type);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001254 if (f != nullptr) {
1255 return f;
1256 }
1257 }
1258 }
1259 return static_cast<ArtField*>(nullptr);
1260 };
1261
Vladimir Marko70e126a2021-06-24 14:51:34 +01001262 auto find_field_by_name_and_type = [&](ObjPtr<mirror::Class> k, ObjPtr<DexCache> k_dex_cache)
1263 REQUIRES_SHARED(Locks::mutator_lock_) -> std::tuple<bool, ArtField*> {
1264 if ((!kSearchInstanceFields || k->GetIFieldsPtr() == nullptr) &&
1265 (!kSearchStaticFields || k->GetSFieldsPtr() == nullptr)) {
1266 return {false, nullptr};
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001267 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001268 ensure_name_and_type_initialized();
1269 const DexFile& k_dex_file = *k_dex_cache->GetDexFile();
1270 if (kSearchInstanceFields && k->GetIFieldsPtr() != nullptr) {
1271 auto [success, field] = FindFieldByNameAndType(k_dex_file, k->GetIFieldsPtr(), name, type);
1272 DCHECK_EQ(success, field != nullptr);
1273 if (success) {
1274 return {true, field};
1275 }
1276 }
1277 if (kSearchStaticFields && k->GetSFieldsPtr() != nullptr) {
1278 auto [success, field] = FindFieldByNameAndType(k_dex_file, k->GetSFieldsPtr(), name, type);
1279 DCHECK_EQ(success, field != nullptr);
1280 if (success) {
1281 return {true, field};
1282 }
1283 }
1284 return {false, nullptr};
1285 };
1286
1287 // If we had a dex cache mismatch, search declared fields by name and type.
1288 if (klass_dex_cache != dex_cache) {
1289 auto [success, field] = find_field_by_name_and_type(klass, klass_dex_cache);
1290 DCHECK_EQ(success, field != nullptr);
1291 if (success) {
1292 return field;
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001293 }
1294 }
1295
Vladimir Marko40261172021-06-14 10:59:51 +01001296 // Search direct interfaces for static fields.
1297 if (kSearchStaticFields) {
1298 ArtField* f = search_direct_interfaces(klass);
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001299 if (f != nullptr) {
1300 return f;
1301 }
1302 }
1303
1304 // Continue searching in superclasses.
Vladimir Marko40261172021-06-14 10:59:51 +01001305 for (ObjPtr<Class> k = klass->GetSuperClass(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001306 // Is the field in this class?
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001307 ObjPtr<DexCache> k_dex_cache = k->GetDexCache();
1308 if (k_dex_cache == dex_cache) {
1309 // Matching dex_cache. We cannot compare the `field_idx` anymore because
1310 // the type index differs, so compare the name index and type index.
Vladimir Marko40261172021-06-14 10:59:51 +01001311 if (kSearchInstanceFields) {
1312 for (ArtField& field : k->GetIFields()) {
1313 const dex::FieldId& other_field_id = dex_file.GetFieldId(field.GetDexFieldIndex());
1314 if (other_field_id.name_idx_ == field_id.name_idx_ &&
1315 other_field_id.type_idx_ == field_id.type_idx_) {
1316 return &field;
1317 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001318 }
1319 }
Vladimir Marko40261172021-06-14 10:59:51 +01001320 if (kSearchStaticFields) {
1321 for (ArtField& field : k->GetSFields()) {
1322 const dex::FieldId& other_field_id = dex_file.GetFieldId(field.GetDexFieldIndex());
1323 if (other_field_id.name_idx_ == field_id.name_idx_ &&
1324 other_field_id.type_idx_ == field_id.type_idx_) {
1325 return &field;
1326 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001327 }
1328 }
Vladimir Marko70e126a2021-06-24 14:51:34 +01001329 } else {
1330 auto [success, field] = find_field_by_name_and_type(k, k_dex_cache);
1331 DCHECK_EQ(success, field != nullptr);
1332 if (success) {
1333 return field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001334 }
1335 }
Vladimir Marko40261172021-06-14 10:59:51 +01001336 if (kSearchStaticFields) {
1337 // Is this field in any of this class' interfaces?
1338 ArtField* f = search_direct_interfaces(k);
1339 if (f != nullptr) {
1340 return f;
1341 }
Vladimir Markoe300c4e2021-06-08 16:00:05 +01001342 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001343 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001344 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001345}
1346
Vladimir Marko40261172021-06-14 10:59:51 +01001347FLATTEN
1348ArtField* Class::FindField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1349 return FindFieldImpl</*kSearchInstanceFields=*/ true,
1350 /*kSearchStaticFields*/ true>(this, dex_cache, field_idx);
1351}
1352
1353FLATTEN
1354ArtField* Class::FindInstanceField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1355 return FindFieldImpl</*kSearchInstanceFields=*/ true,
1356 /*kSearchStaticFields*/ false>(this, dex_cache, field_idx);
1357}
1358
1359FLATTEN
1360ArtField* Class::FindStaticField(ObjPtr<mirror::DexCache> dex_cache, uint32_t field_idx) {
1361 return FindFieldImpl</*kSearchInstanceFields=*/ false,
1362 /*kSearchStaticFields*/ true>(this, dex_cache, field_idx);
1363}
1364
Alex Lightb1eebde2019-10-22 16:30:47 +00001365void Class::ClearSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
1366 DCHECK(IsVerified());
1367 for (auto& m : GetMethods(pointer_size)) {
1368 if (!m.IsNative() && m.IsInvokable()) {
1369 m.ClearSkipAccessChecks();
1370 }
1371 }
1372}
1373
Alex Lightc2d0c962019-10-23 14:14:25 -07001374void Class::ClearMustCountLocksFlagOnAllMethods(PointerSize pointer_size) {
1375 DCHECK(IsVerified());
1376 for (auto& m : GetMethods(pointer_size)) {
1377 if (!m.IsNative() && m.IsInvokable()) {
1378 m.ClearMustCountLocks();
1379 }
1380 }
1381}
1382
1383void Class::ClearDontCompileFlagOnAllMethods(PointerSize pointer_size) {
1384 DCHECK(IsVerified());
1385 for (auto& m : GetMethods(pointer_size)) {
1386 if (!m.IsNative() && m.IsInvokable()) {
1387 m.ClearDontCompile();
1388 }
1389 }
1390}
1391
Andreas Gampe542451c2016-07-26 09:02:02 -07001392void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001393 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001394 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001395 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001396 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001397 }
1398 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001399}
1400
Ian Rogers1ff3c982014-08-12 02:30:58 -07001401const char* Class::GetDescriptor(std::string* storage) {
Vladimir Marko3892e622019-03-15 15:22:18 +00001402 size_t dim = 0u;
1403 ObjPtr<mirror::Class> klass = this;
1404 while (klass->IsArrayClass()) {
1405 ++dim;
Vladimir Markod355acf2019-03-21 17:09:40 +00001406 // No read barrier needed, we're reading a chain of constant references for comparison
1407 // with null. Then we follow up below with reading constant references to read constant
1408 // primitive data in both proxy and non-proxy paths. See ReadBarrierOption.
1409 klass = klass->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001410 }
Vladimir Marko3892e622019-03-15 15:22:18 +00001411 if (klass->IsProxyClass()) {
1412 // No read barrier needed, the `name` field is constant for proxy classes and
1413 // the contents of the String are also constant. See ReadBarrierOption.
1414 ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>();
1415 DCHECK(name != nullptr);
1416 *storage = DotToDescriptor(name->ToModifiedUtf8().c_str());
1417 } else {
1418 const char* descriptor;
1419 if (klass->IsPrimitive()) {
1420 descriptor = Primitive::Descriptor(klass->GetPrimitiveType());
1421 } else {
1422 const DexFile& dex_file = klass->GetDexFile();
1423 const dex::TypeId& type_id = dex_file.GetTypeId(klass->GetDexTypeIndex());
1424 descriptor = dex_file.GetTypeDescriptor(type_id);
1425 }
1426 if (dim == 0) {
1427 return descriptor;
1428 }
1429 *storage = descriptor;
1430 }
1431 storage->insert(0u, dim, '[');
Ian Rogers1ff3c982014-08-12 02:30:58 -07001432 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001433}
1434
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001435const dex::ClassDef* Class::GetClassDef() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001436 uint16_t class_def_idx = GetDexClassDefIndex();
1437 if (class_def_idx == DexFile::kDexNoIndex16) {
1438 return nullptr;
1439 }
1440 return &GetDexFile().GetClassDef(class_def_idx);
1441}
1442
Andreas Gampea5b09a62016-11-17 15:21:22 -08001443dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001444 DCHECK(!IsPrimitive());
1445 DCHECK(!IsArrayClass());
1446 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1447}
1448
Vladimir Markob10668c2021-06-10 09:52:53 +01001449ObjPtr<Class> Class::GetDirectInterface(uint32_t idx) {
1450 DCHECK(!IsPrimitive());
1451 if (IsArrayClass()) {
1452 ObjPtr<IfTable> iftable = GetIfTable();
1453 DCHECK(iftable != nullptr);
1454 DCHECK_EQ(iftable->Count(), 2u);
1455 DCHECK_LT(idx, 2u);
1456 ObjPtr<Class> interface = iftable->GetInterface(idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001457 DCHECK(interface != nullptr);
1458 return interface;
Vladimir Markob10668c2021-06-10 09:52:53 +01001459 } else if (IsProxyClass()) {
1460 ObjPtr<ObjectArray<Class>> interfaces = GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001461 DCHECK(interfaces != nullptr);
1462 return interfaces->Get(idx);
1463 } else {
Vladimir Markob10668c2021-06-10 09:52:53 +01001464 dex::TypeIndex type_idx = GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001465 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Markob10668c2021-06-10 09:52:53 +01001466 type_idx, GetDexCache(), GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001467 return interface;
1468 }
1469}
1470
Vladimir Marko19a4d372016-12-08 14:41:46 +00001471ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
Vladimir Markob10668c2021-06-10 09:52:53 +01001472 ObjPtr<Class> interface = klass->GetDirectInterface(idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001473 if (interface == nullptr) {
1474 DCHECK(!klass->IsArrayClass());
1475 DCHECK(!klass->IsProxyClass());
1476 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001477 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001478 CHECK(interface != nullptr || self->IsExceptionPending());
1479 }
1480 return interface;
1481}
1482
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001483ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001484 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001485 DCHECK(!klass->IsInterface());
1486 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001487 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001488 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001489 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001490 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001491 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001492 }
Calin Juravle52503d82015-11-11 16:58:31 +00001493 return common_super_class;
1494}
1495
Mathieu Chartierf8322842014-05-16 10:59:25 -07001496const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001497 const DexFile& dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001498 const dex::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001499 if (dex_class_def == nullptr) {
1500 // Generated classes have no class def.
1501 return nullptr;
1502 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001503 return dex_file.GetSourceFile(*dex_class_def);
1504}
1505
1506std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001507 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001508 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001509 return dex_cache->GetLocation()->ToModifiedUtf8();
1510 }
1511 // Arrays and proxies are generated and have no corresponding dex file location.
1512 return "generated class";
1513}
1514
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001515const dex::TypeList* Class::GetInterfaceTypeList() {
1516 const dex::ClassDef* class_def = GetClassDef();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001517 if (class_def == nullptr) {
1518 return nullptr;
1519 }
1520 return GetDexFile().GetInterfacesList(*class_def);
1521}
1522
Andreas Gampe542451c2016-07-26 09:02:02 -07001523void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00001524 ObjPtr<PointerArray> table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001525 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001526 const size_t table_length = table->GetLength();
1527 SetEmbeddedVTableLength(table_length);
1528 for (size_t i = 0; i < table_length; i++) {
1529 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001530 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001531 // Keep java.lang.Object class's vtable around for since it's easier
1532 // to be reused by array classes during their linking.
1533 if (!IsObjectClass()) {
1534 SetVTable(nullptr);
1535 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001536}
1537
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001538class ReadBarrierOnNativeRootsVisitor {
1539 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001540 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001541 MemberOffset offset ATTRIBUTE_UNUSED,
1542 bool is_static ATTRIBUTE_UNUSED) const {}
1543
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001544 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001545 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001546 if (!root->IsNull()) {
1547 VisitRoot(root);
1548 }
1549 }
1550
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001551 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001552 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001553 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1554 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001555 if (old_ref != new_ref) {
1556 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1557 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001558 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001559 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001560 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1561 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001562 }
1563 }
1564};
1565
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001566// The pre-fence visitor for Class::CopyOf().
1567class CopyClassVisitor {
1568 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001569 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001570 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001571 size_t new_length,
1572 size_t copy_bytes,
1573 ImTable* imt,
1574 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001575 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001576 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001577 }
1578
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001579 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001580 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001581 StackHandleScope<1> hs(self_);
1582 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001583 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001584 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001585 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1586 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001587 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001588 // Visit all of the references to make sure there is no from space references in the native
1589 // roots.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001590 h_new_class_obj->Object::VisitReferences(ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001591 }
1592
1593 private:
1594 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001595 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001596 const size_t new_length_;
1597 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001598 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001599 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001600 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1601};
1602
Vladimir Marko3068d582019-05-28 16:39:29 +01001603ObjPtr<Class> Class::CopyOf(Handle<Class> h_this,
1604 Thread* self,
1605 int32_t new_length,
1606 ImTable* imt,
1607 PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001608 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1609 // We may get copied by a compacting GC.
Vladimir Marko317892b2018-05-31 11:11:32 +01001610 Runtime* runtime = Runtime::Current();
1611 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001612 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1613 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001615 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001616 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko991cd5c2019-05-30 14:23:39 +01001617 heap->AllocObject(self, java_lang_Class, new_length, visitor) :
1618 heap->AllocNonMovableObject(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001619 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001620 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001621 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001622 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001623 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001624}
1625
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001626bool Class::ProxyDescriptorEquals(const char* match) {
1627 DCHECK(IsProxyClass());
Vladimir Marko3892e622019-03-15 15:22:18 +00001628 std::string storage;
1629 const char* descriptor = GetDescriptor(&storage);
1630 DCHECK(descriptor == storage.c_str());
1631 return storage == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001632}
1633
Mathieu Chartiere401d142015-04-22 13:56:20 -07001634// TODO: Move this to java_lang_Class.cc?
1635ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001636 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001637 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001638 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001640 continue;
1641 }
1642 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001643 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001644 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001645 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001646 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001647 return nullptr;
1648 }
1649 }
1650 return nullptr;
1651}
1652
Mathieu Chartiere401d142015-04-22 13:56:20 -07001653uint32_t Class::Depth() {
1654 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001655 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001656 depth++;
1657 }
1658 return depth;
1659}
1660
Andreas Gampea5b09a62016-11-17 15:21:22 -08001661dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001662 std::string temp;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001663 const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001664 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001665}
1666
David Brazdil4bcd6572019-02-02 20:08:44 +00001667ALWAYS_INLINE
1668static bool IsMethodPreferredOver(ArtMethod* orig_method,
1669 bool orig_method_hidden,
1670 ArtMethod* new_method,
1671 bool new_method_hidden) {
1672 DCHECK(new_method != nullptr);
1673
1674 // Is this the first result?
1675 if (orig_method == nullptr) {
1676 return true;
1677 }
1678
1679 // Original method is hidden, the new one is not?
1680 if (orig_method_hidden && !new_method_hidden) {
1681 return true;
1682 }
1683
1684 // We iterate over virtual methods first and then over direct ones,
1685 // so we can never be in situation where `orig_method` is direct and
1686 // `new_method` is virtual.
1687 DCHECK(!orig_method->IsDirect() || new_method->IsDirect());
1688
1689 // Original method is synthetic, the new one is not?
1690 if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) {
1691 return true;
1692 }
1693
1694 return false;
1695}
1696
Vladimir Markob6f4c792020-05-04 15:37:29 +01001697template <PointerSize kPointerSize>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001698ObjPtr<Method> Class::GetDeclaredMethodInternal(
1699 Thread* self,
1700 ObjPtr<Class> klass,
1701 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001702 ObjPtr<ObjectArray<Class>> args,
1703 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) {
1704 // Covariant return types (or smali) permit the class to define
1705 // multiple methods with the same name and parameter types.
1706 // Prefer (in decreasing order of importance):
1707 // 1) non-hidden method over hidden
1708 // 2) virtual methods over direct
1709 // 3) non-synthetic methods over synthetic
1710 // We never return miranda methods that were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001711 StackHandleScope<3> hs(self);
1712 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001713 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001714 ThrowNullPointerException("name == null");
1715 return nullptr;
1716 }
1717 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001718 Handle<Class> h_klass = hs.NewHandle(klass);
David Brazdil4bcd6572019-02-02 20:08:44 +00001719 constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001720 ArtMethod* result = nullptr;
David Brazdil4bcd6572019-02-02 20:08:44 +00001721 bool result_hidden = false;
Andreas Gampee01e3642016-07-25 13:06:04 -07001722 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
David Brazdil4bcd6572019-02-02 20:08:44 +00001723 if (m.IsMiranda()) {
1724 continue;
1725 }
Nicolas Geoffray8fddc752021-07-06 14:16:40 +01001726 ArtMethod* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001727 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001728 ObjPtr<String> np_name = np_method->ResolveNameString();
Nicolas Geoffray8fddc752021-07-06 14:16:40 +01001729 if (np_name == nullptr) {
1730 // OOME
1731 DCHECK(self->IsExceptionPending());
1732 return nullptr;
1733 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001734 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1735 if (UNLIKELY(self->IsExceptionPending())) {
1736 return nullptr;
1737 }
1738 continue;
1739 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001740 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1741 if (!m_hidden && !m.IsSynthetic()) {
1742 // Non-hidden, virtual, non-synthetic. Best possible result, exit early.
Vladimir Markob6f4c792020-05-04 15:37:29 +01001743 return Method::CreateFromArtMethod<kPointerSize>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001744 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1745 // Remember as potential result.
1746 result = &m;
1747 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001748 }
1749 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001750
1751 if ((result != nullptr) && !result_hidden) {
1752 // We have not found a non-hidden, virtual, non-synthetic method, but
1753 // if we have found a non-hidden, virtual, synthetic method, we cannot
1754 // do better than that later.
1755 DCHECK(!result->IsDirect());
1756 DCHECK(result->IsSynthetic());
1757 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -07001758 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001759 auto modifiers = m.GetAccessFlags();
1760 if ((modifiers & kAccConstructor) != 0) {
1761 continue;
1762 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001763 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001764 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001765 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001766 if (np_name == nullptr) {
1767 self->AssertPendingException();
1768 return nullptr;
1769 }
1770 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1771 if (UNLIKELY(self->IsExceptionPending())) {
1772 return nullptr;
1773 }
1774 continue;
1775 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001776 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
David Brazdil4bcd6572019-02-02 20:08:44 +00001777 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1778 if (!m_hidden && !m.IsSynthetic()) {
1779 // Non-hidden, direct, non-synthetic. Any virtual result could only have been
1780 // hidden, therefore this is the best possible match. Exit now.
1781 DCHECK((result == nullptr) || result_hidden);
Vladimir Markob6f4c792020-05-04 15:37:29 +01001782 return Method::CreateFromArtMethod<kPointerSize>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001783 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1784 // Remember as potential result.
1785 result = &m;
1786 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001787 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001788 }
1789 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001790
Andreas Gampebc4d2182016-02-22 10:03:12 -08001791 return result != nullptr
Vladimir Markob6f4c792020-05-04 15:37:29 +01001792 ? Method::CreateFromArtMethod<kPointerSize>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001793 : nullptr;
1794}
1795
1796template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001797ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001798 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001799 ObjPtr<Class> klass,
1800 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001801 ObjPtr<ObjectArray<Class>> args,
1802 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001803template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001804ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001805 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001806 ObjPtr<Class> klass,
1807 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001808 ObjPtr<ObjectArray<Class>> args,
1809 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001810
Vladimir Markob6f4c792020-05-04 15:37:29 +01001811template <PointerSize kPointerSize>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001812ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001813 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001814 ObjPtr<Class> klass,
1815 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001816 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001817 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001818 return result != nullptr
Vladimir Markob6f4c792020-05-04 15:37:29 +01001819 ? Constructor::CreateFromArtMethod<kPointerSize>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001820 : nullptr;
1821}
1822
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001823// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001824
Andreas Gampe542451c2016-07-26 09:02:02 -07001825template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001826ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001827 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001828 ObjPtr<Class> klass,
1829 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001830template
Vladimir Markob6f4c792020-05-04 15:37:29 +01001831ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001832 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001833 ObjPtr<Class> klass,
1834 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001835
Andreas Gampe715fdc22016-04-18 17:07:30 -07001836int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1837 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1838 return default_value;
1839 }
1840 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001841 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001842 return default_value;
1843 }
1844 return flags;
1845}
1846
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001847void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1848 if (Runtime::Current()->IsActiveTransaction()) {
1849 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1850 } else {
1851 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1852 }
1853}
1854
David Sehr709b0702016-10-13 09:12:37 -07001855std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1856 if (klass == nullptr) {
1857 return "null";
1858 }
1859 return klass->PrettyDescriptor();
1860}
1861
1862std::string Class::PrettyDescriptor() {
1863 std::string temp;
1864 return art::PrettyDescriptor(GetDescriptor(&temp));
1865}
1866
1867std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1868 if (c == nullptr) {
1869 return "null";
1870 }
1871 return c->PrettyClass();
1872}
1873
1874std::string Class::PrettyClass() {
1875 std::string result;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +00001876 if (IsObsoleteObject()) {
1877 result += "(Obsolete)";
1878 }
1879 if (IsRetired()) {
1880 result += "(Retired)";
1881 }
David Sehr709b0702016-10-13 09:12:37 -07001882 result += "java.lang.Class<";
1883 result += PrettyDescriptor();
1884 result += ">";
1885 return result;
1886}
1887
1888std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1889 if (c == nullptr) {
1890 return "null";
1891 }
1892 return c->PrettyClassAndClassLoader();
1893}
1894
1895std::string Class::PrettyClassAndClassLoader() {
1896 std::string result;
1897 result += "java.lang.Class<";
1898 result += PrettyDescriptor();
1899 result += ",";
1900 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1901 // TODO: add an identifying hash value for the loader
1902 result += ">";
1903 return result;
1904}
1905
Andreas Gampe90b936d2017-01-31 08:58:55 -08001906template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1907 // Check class is loaded/retired or this is java.lang.String that has a
1908 // circularity issue during loading the names of its members
1909 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1910 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001911 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001912 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1913 << " IsRetired=" << IsRetired<kVerifyFlags>()
1914 << " IsErroneous=" <<
1915 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001916 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001917 << " status= " << GetStatus<kVerifyFlags>()
1918 << " descriptor=" << PrettyDescriptor();
1919}
1920// Instantiate the common cases.
1921template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1922template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1923template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1924template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1925template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1926
Alex Lightbc19b752019-12-02 18:54:13 +00001927ObjPtr<Object> Class::GetMethodIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001928 ObjPtr<ClassExt> ext(GetExtData());
1929 if (ext.IsNull()) {
1930 return nullptr;
1931 } else {
1932 return ext->GetJMethodIDs();
1933 }
1934}
Alex Lightbc19b752019-12-02 18:54:13 +00001935bool Class::EnsureMethodIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001936 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001937 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001938 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1939 if (ext.IsNull()) {
1940 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001941 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001942 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001943 return ext->EnsureJMethodIDsArrayPresent(h_this->NumMethods());
Alex Light79d6c802019-06-27 15:50:11 +00001944}
1945
Alex Lightbc19b752019-12-02 18:54:13 +00001946ObjPtr<Object> Class::GetStaticFieldIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001947 ObjPtr<ClassExt> ext(GetExtData());
1948 if (ext.IsNull()) {
1949 return nullptr;
1950 } else {
1951 return ext->GetStaticJFieldIDs();
1952 }
1953}
Alex Lightbc19b752019-12-02 18:54:13 +00001954bool Class::EnsureStaticFieldIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001955 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001956 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001957 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1958 if (ext.IsNull()) {
1959 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001960 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001961 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001962 return ext->EnsureStaticJFieldIDsArrayPresent(h_this->NumStaticFields());
Alex Light79d6c802019-06-27 15:50:11 +00001963}
Alex Lightbc19b752019-12-02 18:54:13 +00001964ObjPtr<Object> Class::GetInstanceFieldIds() {
Alex Light79d6c802019-06-27 15:50:11 +00001965 ObjPtr<ClassExt> ext(GetExtData());
1966 if (ext.IsNull()) {
1967 return nullptr;
1968 } else {
1969 return ext->GetInstanceJFieldIDs();
1970 }
1971}
Alex Lightbc19b752019-12-02 18:54:13 +00001972bool Class::EnsureInstanceFieldIds(Handle<Class> h_this) {
Alex Lightf3677472019-06-26 16:31:53 -07001973 DCHECK_NE(Runtime::Current()->GetJniIdType(), JniIdType::kPointer) << "JNI Ids are pointers!";
Alex Light79d6c802019-06-27 15:50:11 +00001974 Thread* self = Thread::Current();
Alex Light79d6c802019-06-27 15:50:11 +00001975 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
1976 if (ext.IsNull()) {
1977 self->AssertPendingOOMException();
Alex Lightbc19b752019-12-02 18:54:13 +00001978 return false;
Alex Light79d6c802019-06-27 15:50:11 +00001979 }
Alex Lightbb5b4f32019-07-09 02:31:48 -07001980 return ext->EnsureInstanceJFieldIDsArrayPresent(h_this->NumInstanceFields());
Alex Light79d6c802019-06-27 15:50:11 +00001981}
1982
1983size_t Class::GetStaticFieldIdOffset(ArtField* field) {
1984 DCHECK_LT(reinterpret_cast<uintptr_t>(field),
1985 reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->end()))
1986 << "field not part of the current class. " << field->PrettyField() << " class is "
1987 << PrettyClass();
1988 DCHECK_GE(reinterpret_cast<uintptr_t>(field),
1989 reinterpret_cast<uintptr_t>(&*GetSFieldsPtr()->begin()))
1990 << "field not part of the current class. " << field->PrettyField() << " class is "
1991 << PrettyClass();
1992 uintptr_t start = reinterpret_cast<uintptr_t>(&GetSFieldsPtr()->At(0));
1993 uintptr_t fld = reinterpret_cast<uintptr_t>(field);
1994 size_t res = (fld - start) / sizeof(ArtField);
1995 DCHECK_EQ(&GetSFieldsPtr()->At(res), field)
1996 << "Incorrect field computation expected: " << field->PrettyField()
1997 << " got: " << GetSFieldsPtr()->At(res).PrettyField();
1998 return res;
1999}
2000
2001size_t Class::GetInstanceFieldIdOffset(ArtField* field) {
2002 DCHECK_LT(reinterpret_cast<uintptr_t>(field),
2003 reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->end()))
2004 << "field not part of the current class. " << field->PrettyField() << " class is "
2005 << PrettyClass();
2006 DCHECK_GE(reinterpret_cast<uintptr_t>(field),
2007 reinterpret_cast<uintptr_t>(&*GetIFieldsPtr()->begin()))
2008 << "field not part of the current class. " << field->PrettyField() << " class is "
2009 << PrettyClass();
2010 uintptr_t start = reinterpret_cast<uintptr_t>(&GetIFieldsPtr()->At(0));
2011 uintptr_t fld = reinterpret_cast<uintptr_t>(field);
2012 size_t res = (fld - start) / sizeof(ArtField);
2013 DCHECK_EQ(&GetIFieldsPtr()->At(res), field)
2014 << "Incorrect field computation expected: " << field->PrettyField()
2015 << " got: " << GetIFieldsPtr()->At(res).PrettyField();
2016 return res;
2017}
2018
2019size_t Class::GetMethodIdOffset(ArtMethod* method, PointerSize pointer_size) {
2020 DCHECK(GetMethodsSlice(kRuntimePointerSize).Contains(method))
2021 << "method not part of the current class. " << method->PrettyMethod() << "( " << reinterpret_cast<void*>(method) << ")" << " class is "
2022 << PrettyClass() << [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
2023 std::ostringstream os;
2024 os << " Methods are [";
2025 for (ArtMethod& m : GetMethodsSlice(kRuntimePointerSize)) {
2026 os << m.PrettyMethod() << "( " << reinterpret_cast<void*>(&m) << "), ";
2027 }
2028 os << "]";
2029 return os.str();
2030 }();
2031 uintptr_t start = reinterpret_cast<uintptr_t>(&*GetMethodsSlice(pointer_size).begin());
2032 uintptr_t fld = reinterpret_cast<uintptr_t>(method);
2033 size_t art_method_size = ArtMethod::Size(pointer_size);
2034 size_t art_method_align = ArtMethod::Alignment(pointer_size);
2035 size_t res = (fld - start) / art_method_size;
2036 DCHECK_EQ(&GetMethodsPtr()->At(res, art_method_size, art_method_align), method)
2037 << "Incorrect method computation expected: " << method->PrettyMethod()
2038 << " got: " << GetMethodsPtr()->At(res, art_method_size, art_method_align).PrettyMethod();
2039 return res;
2040}
2041
Nicolas Geoffrayaf61f502021-03-31 16:03:50 +00002042ArtMethod* Class::FindAccessibleInterfaceMethod(ArtMethod* implementation_method,
2043 PointerSize pointer_size)
2044 REQUIRES_SHARED(Locks::mutator_lock_) {
2045 ObjPtr<mirror::IfTable> iftable = GetIfTable();
2046 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
2047 ObjPtr<mirror::PointerArray> methods = iftable->GetMethodArrayOrNull(i);
2048 if (methods == nullptr) {
2049 continue;
2050 }
2051 for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) {
2052 if (implementation_method == methods->GetElementPtrSize<ArtMethod*>(j, pointer_size)) {
2053 ObjPtr<mirror::Class> iface = iftable->GetInterface(i);
2054 ArtMethod* interface_method = &iface->GetVirtualMethodsSlice(pointer_size)[j];
2055 // If the interface method is part of the public SDK, return it.
2056 if ((hiddenapi::GetRuntimeFlags(interface_method) & kAccPublicApi) != 0) {
2057 return interface_method;
2058 }
2059 }
2060 }
2061 }
2062 return nullptr;
2063}
2064
2065
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002066} // namespace mirror
2067} // namespace art