blob: a862c9798ab4ec8b8e99127dc1b1572891955bc4 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_field-inl.h"
20#include "art_method-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070021#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class_loader.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070024#include "class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070027#include "dex_file_annotations.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070031#include "object_array-inl.h"
32#include "object-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070033#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070034#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "thread.h"
36#include "throwable.h"
37#include "utils.h"
38#include "well_known_classes.h"
39
40namespace art {
41namespace mirror {
42
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070043GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070045void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070046 CHECK(java_lang_Class_.IsNull())
47 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070048 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070049 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070050 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
54void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 CHECK(!java_lang_Class_.IsNull());
56 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057}
58
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070059void Class::VisitRoots(RootVisitor* visitor) {
60 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080061}
62
Alex Lightd6251582016-10-31 11:12:30 -070063ClassExt* Class::GetExtData() {
64 return GetFieldObject<ClassExt>(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
65}
66
Alex Light0273ad12016-11-02 11:19:31 -070067ClassExt* Class::EnsureExtDataPresent(Thread* self) {
68 ObjPtr<ClassExt> existing(GetExtData());
69 if (!existing.IsNull()) {
70 return existing.Ptr();
71 }
72 StackHandleScope<3> hs(self);
73 // Handlerize 'this' since we are allocating here.
74 Handle<Class> h_this(hs.NewHandle(this));
75 // Clear exception so we can allocate.
76 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
77 self->ClearException();
78 // Allocate the ClassExt
79 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
80 if (new_ext.Get() == nullptr) {
81 // OOM allocating the classExt.
82 // TODO Should we restore the suppressed exception?
83 self->AssertPendingOOMException();
84 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -080085 } else {
Alex Light0273ad12016-11-02 11:19:31 -070086 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
87 bool set;
88 // Set the ext_data_ field using CAS semantics.
89 if (Runtime::Current()->IsActiveTransaction()) {
90 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
91 ObjPtr<ClassExt>(nullptr),
92 new_ext.Get());
93 } else {
94 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
95 ObjPtr<ClassExt>(nullptr),
96 new_ext.Get());
97 }
98 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
99 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
100 CHECK(!ret.IsNull());
101 // Restore the exception if there was one.
102 if (throwable.Get() != nullptr) {
103 self->SetException(throwable.Get());
104 }
105 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800106 }
107}
108
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700109void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
110 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700111 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
112 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700113 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700114 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
115 new_status != kStatusRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700116 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700117 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700118 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700119 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
120 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700121 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700122 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700123 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700124 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 }
Ian Rogers98379392014-02-24 16:53:16 -0800126 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700127 CHECK_NE(h_this->GetStatus(), kStatusError)
128 << "Attempt to set as erroneous an already erroneous class "
David Sehr709b0702016-10-13 09:12:37 -0700129 << h_this->PrettyClass();
Andreas Gampe31decb12015-08-24 21:09:05 -0700130 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700131 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700132 if (self->IsExceptionPending()) {
133 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
134 }
135 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136
Alex Light0273ad12016-11-02 11:19:31 -0700137 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
138 if (!ext.IsNull()) {
139 self->AssertPendingException();
140 ext->SetVerifyError(self->GetException());
141 } else {
142 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700143 }
144 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 }
Alex Light0273ad12016-11-02 11:19:31 -0700146
Andreas Gampe575e78c2014-11-03 23:41:03 -0800147 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700149 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700151 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
152 }
153
154 // Setting the object size alloc fast path needs to be after the status write so that if the
155 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
156 // load-acquire/fake dependency.
157 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700158 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
159 // Finalizable objects must always go slow path.
160 if (!h_this->IsFinalizable()) {
161 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700162 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100163 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700164
165 if (!class_linker_initialized) {
166 // When the class linker is being initialized its single threaded and by definition there can be
167 // no waiters. During initialization classes may appear temporary but won't be retired as their
168 // size was statically computed.
169 } else {
170 // Classes that are being resolved or initialized need to notify waiters that the class status
171 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700172 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700173 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
174 // so that they can grab the new version of the class from the class linker's table.
David Sehr709b0702016-10-13 09:12:37 -0700175 CHECK_LT(new_status, kStatusResolved) << h_this->PrettyDescriptor();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700176 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700177 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700178 }
179 } else {
180 CHECK_NE(new_status, kStatusRetired);
181 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700182 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183 }
184 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700185 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186}
187
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700188void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700189 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800190 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191}
192
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700194 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700195 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
196 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700197 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700198 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700200 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201}
202
203// Return the class' name. The exact format is bizarre, but it's the specified behavior for
204// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
205// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
206// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700207String* Class::ComputeName(Handle<Class> h_this) {
208 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800209 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 return name;
211 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 std::string temp;
213 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800214 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
216 // The descriptor indicates that this is the class for
217 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700218 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 switch (descriptor[0]) {
220 case 'Z': c_name = "boolean"; break;
221 case 'B': c_name = "byte"; break;
222 case 'C': c_name = "char"; break;
223 case 'S': c_name = "short"; break;
224 case 'I': c_name = "int"; break;
225 case 'J': c_name = "long"; break;
226 case 'F': c_name = "float"; break;
227 case 'D': c_name = "double"; break;
228 case 'V': c_name = "void"; break;
229 default:
230 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
231 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800232 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 } else {
234 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
235 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700236 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700238 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 return name;
240}
241
Ian Rogersef7d42f2014-01-06 12:55:46 -0800242void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700244 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 if ((flags & kDumpClassClassLoader) != 0) {
246 os << ' ' << GetClassLoader();
247 }
248 if ((flags & kDumpClassInitialized) != 0) {
249 os << ' ' << GetStatus();
250 }
251 os << "\n";
252 return;
253 }
254
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700256 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700257 Handle<Class> h_this(hs.NewHandle(this));
258 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700260
Ian Rogers1ff3c982014-08-12 02:30:58 -0700261 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700263 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700265 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 os << StringPrintf(" access=0x%04x.%04x\n",
267 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700268 if (h_super.Get() != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700269 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700270 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 }
272 if (IsArrayClass()) {
273 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
274 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700275 const size_t num_direct_interfaces = NumDirectInterfaces();
276 if (num_direct_interfaces > 0) {
277 os << " interfaces (" << num_direct_interfaces << "):\n";
278 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000279 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700280 if (interface == nullptr) {
281 os << StringPrintf(" %2zd: nullptr!\n", i);
282 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700283 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
284 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700285 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 }
287 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700288 if (!IsLoaded()) {
289 os << " class not yet loaded";
290 } else {
291 // After this point, this may have moved due to GetDirectInterface.
292 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
293 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
294 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700295 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700298 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
299 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700300 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700302 }
303 if (h_this->NumStaticFields() > 0) {
304 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
305 if (h_this->IsResolved() || h_this->IsErroneous()) {
306 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700307 os << StringPrintf(" %2zd: %s\n", i,
308 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309 }
310 } else {
311 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313 }
314 if (h_this->NumInstanceFields() > 0) {
315 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
316 if (h_this->IsResolved() || h_this->IsErroneous()) {
317 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700318 os << StringPrintf(" %2zd: %s\n", i,
319 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 }
321 } else {
322 os << " <not yet available>";
323 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 }
325 }
326}
327
328void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 // Sanity check that the number of bits set in the reference offset bitmap
331 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700332 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700333 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 count += c->NumReferenceInstanceFieldsDuringLinking();
335 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700336 // +1 for the Class in Object.
337 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100339 // Not called within a transaction.
340 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700341 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342}
343
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
345 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700346 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
347 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 ++i;
349 }
350 if (descriptor1.find('/', i) != StringPiece::npos ||
351 descriptor2.find('/', i) != StringPiece::npos) {
352 return false;
353 } else {
354 return true;
355 }
356}
357
Mathieu Chartier3398c782016-09-30 10:27:43 -0700358bool Class::IsInSamePackage(ObjPtr<Class> that) {
359 ObjPtr<Class> klass1 = this;
360 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 if (klass1 == klass2) {
362 return true;
363 }
364 // Class loaders must match.
365 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
366 return false;
367 }
368 // Arrays are in the same package when their element classes are.
369 while (klass1->IsArrayClass()) {
370 klass1 = klass1->GetComponentType();
371 }
372 while (klass2->IsArrayClass()) {
373 klass2 = klass2->GetComponentType();
374 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700375 // trivial check again for array types
376 if (klass1 == klass2) {
377 return true;
378 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700380 std::string temp1, temp2;
381 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382}
383
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
386}
387
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700388void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100389 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700390 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100391 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700392 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394}
395
Andreas Gampe542451c2016-07-26 09:02:02 -0700396ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
397 const StringPiece& signature,
398 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700401 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 return method;
403 }
404
405 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700406 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700407 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700409 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 return method;
411 }
412 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 return nullptr;
414}
415
Andreas Gampe542451c2016-07-26 09:02:02 -0700416ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
417 const Signature& signature,
418 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700419 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700421 if (method != nullptr) {
422 return method;
423 }
424
425 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700426 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700427 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700429 if (method != nullptr) {
430 return method;
431 }
432 }
433 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434}
435
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700436ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700437 uint32_t dex_method_idx,
438 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442 return method;
443 }
444
445 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700446 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
449 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451 return method;
452 }
453 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455}
456
Andreas Gampe542451c2016-07-26 09:02:02 -0700457ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
458 const StringPiece& signature,
459 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460 for (auto& method : GetDirectMethods(pointer_size)) {
461 if (name == method.GetName() && method.GetSignature() == signature) {
462 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700463 }
464 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466}
467
Andreas Gampe542451c2016-07-26 09:02:02 -0700468ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
469 const Signature& signature,
470 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 for (auto& method : GetDirectMethods(pointer_size)) {
472 if (name == method.GetName() && signature == method.GetSignature()) {
473 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474 }
475 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477}
478
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700479ArtMethod* Class::FindDeclaredDirectMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700480 uint32_t dex_method_idx,
481 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483 for (auto& method : GetDirectMethods(pointer_size)) {
484 if (method.GetDexMethodIndex() == dex_method_idx) {
485 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 }
487 }
488 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700489 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490}
491
Andreas Gampe542451c2016-07-26 09:02:02 -0700492ArtMethod* Class::FindDirectMethod(const StringPiece& name,
493 const StringPiece& signature,
494 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700495 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700497 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 return method;
499 }
500 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502}
503
Andreas Gampe542451c2016-07-26 09:02:02 -0700504ArtMethod* Class::FindDirectMethod(const StringPiece& name,
505 const Signature& signature,
506 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700507 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700510 return method;
511 }
512 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700514}
515
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700516ArtMethod* Class::FindDirectMethod(ObjPtr<DexCache> dex_cache,
517 uint32_t dex_method_idx,
518 PointerSize pointer_size) {
519 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 return method;
523 }
524 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526}
527
Andreas Gampe542451c2016-07-26 09:02:02 -0700528ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
529 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000530 for (auto& method : GetDirectMethods(pointer_size)) {
531 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
532 if (name == np_method->GetName()) {
533 return &method;
534 }
535 }
536 return nullptr;
537}
538
Alex Lighte64300b2015-12-15 15:02:47 -0800539// TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar
540// because they do not only find 'declared' methods and will return copied methods. This behavior is
541// desired and correct but the naming can lead to confusion because in the java language declared
542// excludes interface methods which might be found by this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700543ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
544 const StringPiece& signature,
545 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700547 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
548 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700550 }
551 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700552 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700553}
554
Andreas Gampe542451c2016-07-26 09:02:02 -0700555ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
556 const Signature& signature,
557 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700559 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
560 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 }
563 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700564 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565}
566
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700567ArtMethod* Class::FindDeclaredVirtualMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700568 uint32_t dex_method_idx,
569 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 if (GetDexCache() == dex_cache) {
Alex Lighte64300b2015-12-15 15:02:47 -0800571 for (auto& method : GetDeclaredVirtualMethods(pointer_size)) {
572 if (method.GetDexMethodIndex() == dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700573 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 }
575 }
576 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700577 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578}
579
Andreas Gampe542451c2016-07-26 09:02:02 -0700580ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
581 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000582 for (auto& method : GetVirtualMethods(pointer_size)) {
583 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
584 if (name == np_method->GetName()) {
585 return &method;
586 }
587 }
588 return nullptr;
589}
590
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700591ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
592 const StringPiece& signature,
593 PointerSize pointer_size) {
594 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700596 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 return method;
598 }
599 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700600 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601}
602
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700603ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
604 const Signature& signature,
605 PointerSize pointer_size) {
606 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700607 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700608 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700609 return method;
610 }
611 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700612 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700613}
614
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700615ArtMethod* Class::FindVirtualMethod(ObjPtr<DexCache> dex_cache,
616 uint32_t dex_method_idx,
617 PointerSize pointer_size) {
618 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700619 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700620 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 return method;
622 }
623 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700624 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625}
626
Andreas Gampe542451c2016-07-26 09:02:02 -0700627ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700628 DCHECK(method->GetDeclaringClass()->IsInterface());
629 DCHECK(IsInterface()) << "Should only be called on a interface class";
630 // Check if we have one defined on this interface first. This includes searching copied ones to
631 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
632 // don't do any indirect method checks here.
633 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
634 if (method->HasSameNameAndSignature(&iface_method)) {
635 return &iface_method;
636 }
637 }
638
639 std::vector<ArtMethod*> abstract_methods;
640 // Search through the IFTable for a working version. We don't need to check for conflicts
641 // because if there was one it would appear in this classes virtual_methods_ above.
642
643 Thread* self = Thread::Current();
644 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700645 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
646 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700647 size_t iftable_count = GetIfTableCount();
648 // Find the method. We don't need to check for conflicts because they would have been in the
649 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
650 // subtypiest interfaces get visited first.
651 for (size_t k = iftable_count; k != 0;) {
652 k--;
653 DCHECK_LT(k, iftable->Count());
654 iface.Assign(iftable->GetInterface(k));
655 // Iterate through every declared method on this interface. Each direct method's name/signature
656 // is unique so the order of the inner loop doesn't matter.
657 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
658 ArtMethod* current_method = &method_iter;
659 if (current_method->HasSameNameAndSignature(method)) {
660 if (current_method->IsDefault()) {
661 // Handle JLS soft errors, a default method from another superinterface tree can
662 // "override" an abstract method(s) from another superinterface tree(s). To do this,
663 // ignore any [default] method which are dominated by the abstract methods we've seen so
664 // far. Check if overridden by any in abstract_methods. We do not need to check for
665 // default_conflicts because we would hit those before we get to this loop.
666 bool overridden = false;
667 for (ArtMethod* possible_override : abstract_methods) {
668 DCHECK(possible_override->HasSameNameAndSignature(current_method));
669 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
670 overridden = true;
671 break;
672 }
673 }
674 if (!overridden) {
675 return current_method;
676 }
677 } else {
678 // Is not default.
679 // This might override another default method. Just stash it for now.
680 abstract_methods.push_back(current_method);
681 }
682 }
683 }
684 }
685 // If we reach here we either never found any declaration of the method (in which case
686 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
687 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
688 // of these arbitrarily.
689 return abstract_methods.empty() ? nullptr : abstract_methods[0];
690}
691
Andreas Gampe542451c2016-07-26 09:02:02 -0700692ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
694 if (method.IsClassInitializer()) {
695 DCHECK_STREQ(method.GetName(), "<clinit>");
696 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
697 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700698 }
699 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700700 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700701}
702
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700703// Custom binary search to avoid double comparisons from std::binary_search.
704static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
705 const StringPiece& name,
706 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700707 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700708 if (fields == nullptr) {
709 return nullptr;
710 }
711 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100712 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700713 ArtField* ret = nullptr;
714 while (low < high) {
715 size_t mid = (low + high) / 2;
716 ArtField& field = fields->At(mid);
717 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
718 // verifier. There can be multiple fields with the same in the same class name due to proguard.
719 int result = StringPiece(field.GetName()).Compare(name);
720 if (result == 0) {
721 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
722 }
723 if (result < 0) {
724 low = mid + 1;
725 } else if (result > 0) {
726 high = mid;
727 } else {
728 ret = &field;
729 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800730 }
731 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700732 if (kIsDebugBuild) {
733 ArtField* found = nullptr;
734 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
735 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
736 found = &field;
737 break;
738 }
739 }
David Sehr709b0702016-10-13 09:12:37 -0700740 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700741 }
742 return ret;
743}
744
745ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
746 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
747 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800748}
749
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700750ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800751 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700752 for (ArtField& field : GetIFields()) {
753 if (field.GetDexFieldIndex() == dex_field_idx) {
754 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800755 }
756 }
757 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700758 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800759}
760
Brian Carlstromea46f952013-07-30 01:26:50 -0700761ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800762 // Is the field in this class, or any of its superclasses?
763 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700764 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700765 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700766 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800767 return f;
768 }
769 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700770 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800771}
772
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700773ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800774 // Is the field in this class, or any of its superclasses?
775 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700776 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700777 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700778 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800779 return f;
780 }
781 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700782 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800783}
784
Brian Carlstromea46f952013-07-30 01:26:50 -0700785ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700786 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700787 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800788}
789
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700790ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800791 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700792 for (ArtField& field : GetSFields()) {
793 if (field.GetDexFieldIndex() == dex_field_idx) {
794 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800795 }
796 }
797 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700798 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800799}
800
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700801ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000802 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700803 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700804 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800805 // Is the field in this class (or its interfaces), or any of its
806 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000807 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800808 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700809 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700810 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800811 return f;
812 }
813 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000814 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
815 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
816 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700817 f = FindStaticField(self, interface, name, type);
818 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800819 return f;
820 }
821 }
822 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700823 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800824}
825
Vladimir Markobb268b12016-06-30 15:52:56 +0100826ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700827 ObjPtr<Class> klass,
828 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700829 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700830 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800831 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700832 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700833 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834 return f;
835 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100836 // Though GetDirectInterface() should not cause thread suspension when called
837 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700838 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800839 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000840 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
841 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
842 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700843 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
844 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800845 return f;
846 }
847 }
848 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700849 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800850}
851
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700852ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000853 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700854 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700855 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800856 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000857 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800858 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700859 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700860 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800861 return f;
862 }
863 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700864 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865 return f;
866 }
867 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000868 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
869 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
870 DCHECK(interface != nullptr);
871 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700872 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800873 return f;
874 }
875 }
876 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700877 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878}
879
Andreas Gampe542451c2016-07-26 09:02:02 -0700880void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700881 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800882 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700883 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800884 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885 }
886 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200887}
888
Ian Rogers1ff3c982014-08-12 02:30:58 -0700889const char* Class::GetDescriptor(std::string* storage) {
890 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700891 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700892 } else if (IsArrayClass()) {
893 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000894 } else if (IsProxyClass()) {
895 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700896 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700897 } else {
898 const DexFile& dex_file = GetDexFile();
899 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
900 return dex_file.GetTypeDescriptor(type_id);
901 }
902}
903
Ian Rogers1ff3c982014-08-12 02:30:58 -0700904const char* Class::GetArrayDescriptor(std::string* storage) {
905 std::string temp;
906 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
907 *storage = "[";
908 *storage += elem_desc;
909 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700910}
911
912const DexFile::ClassDef* Class::GetClassDef() {
913 uint16_t class_def_idx = GetDexClassDefIndex();
914 if (class_def_idx == DexFile::kDexNoIndex16) {
915 return nullptr;
916 }
917 return &GetDexFile().GetClassDef(class_def_idx);
918}
919
Andreas Gampea5b09a62016-11-17 15:21:22 -0800920dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700921 DCHECK(!IsPrimitive());
922 DCHECK(!IsArrayClass());
923 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
924}
925
Vladimir Marko19a4d372016-12-08 14:41:46 +0000926ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
927 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700928 DCHECK(!klass->IsPrimitive());
929 if (klass->IsArrayClass()) {
930 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +0000931 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
932 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700933 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000934 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700935 } else {
936 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000937 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700938 }
Vladimir Marko19a4d372016-12-08 14:41:46 +0000939 DCHECK(interface != nullptr);
940 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000941 } else if (klass->IsProxyClass()) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000942 ObjPtr<ObjectArray<Class>> interfaces = klass->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700943 DCHECK(interfaces != nullptr);
944 return interfaces->Get(idx);
945 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800946 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700947 ObjPtr<Class> interface = klass->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700948 return interface;
949 }
950}
951
Vladimir Marko19a4d372016-12-08 14:41:46 +0000952ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
953 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
954 if (interface == nullptr) {
955 DCHECK(!klass->IsArrayClass());
956 DCHECK(!klass->IsProxyClass());
957 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
958 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(),
959 type_idx,
960 klass.Get());
961 CHECK(interface != nullptr || self->IsExceptionPending());
962 }
963 return interface;
964}
965
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700966ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Calin Juravle52503d82015-11-11 16:58:31 +0000967 DCHECK(klass.Get() != nullptr);
968 DCHECK(!klass->IsInterface());
969 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700970 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +0000971 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700972 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -0700973 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -0700974 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +0000975 }
Calin Juravle52503d82015-11-11 16:58:31 +0000976 return common_super_class;
977}
978
Mathieu Chartierf8322842014-05-16 10:59:25 -0700979const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700980 const DexFile& dex_file = GetDexFile();
981 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200982 if (dex_class_def == nullptr) {
983 // Generated classes have no class def.
984 return nullptr;
985 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700986 return dex_file.GetSourceFile(*dex_class_def);
987}
988
989std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700990 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000991 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700992 return dex_cache->GetLocation()->ToModifiedUtf8();
993 }
994 // Arrays and proxies are generated and have no corresponding dex file location.
995 return "generated class";
996}
997
998const DexFile::TypeList* Class::GetInterfaceTypeList() {
999 const DexFile::ClassDef* class_def = GetClassDef();
1000 if (class_def == nullptr) {
1001 return nullptr;
1002 }
1003 return GetDexFile().GetInterfacesList(*class_def);
1004}
1005
Andreas Gampe542451c2016-07-26 09:02:02 -07001006void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001007 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001008 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001009 const size_t table_length = table->GetLength();
1010 SetEmbeddedVTableLength(table_length);
1011 for (size_t i = 0; i < table_length; i++) {
1012 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001013 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001014 // Keep java.lang.Object class's vtable around for since it's easier
1015 // to be reused by array classes during their linking.
1016 if (!IsObjectClass()) {
1017 SetVTable(nullptr);
1018 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001019}
1020
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001021class ReadBarrierOnNativeRootsVisitor {
1022 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001023 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001024 MemberOffset offset ATTRIBUTE_UNUSED,
1025 bool is_static ATTRIBUTE_UNUSED) const {}
1026
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001027 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001028 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001029 if (!root->IsNull()) {
1030 VisitRoot(root);
1031 }
1032 }
1033
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001034 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001035 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001036 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1037 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001038 if (old_ref != new_ref) {
1039 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1040 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001041 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001042 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001043 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1044 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001045 }
1046 }
1047};
1048
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001049// The pre-fence visitor for Class::CopyOf().
1050class CopyClassVisitor {
1051 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001052 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001053 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001054 size_t new_length,
1055 size_t copy_bytes,
1056 ImTable* imt,
1057 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001058 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001060 }
1061
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001062 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001063 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001064 StackHandleScope<1> hs(self_);
1065 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001066 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1067 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001068 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1069 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001070 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001071 // Visit all of the references to make sure there is no from space references in the native
1072 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001073 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001074 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001075 }
1076
1077 private:
1078 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001079 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001080 const size_t new_length_;
1081 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001082 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001083 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001084 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1085};
1086
Andreas Gampe542451c2016-07-26 09:02:02 -07001087Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001088 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1089 // We may get copied by a compacting GC.
1090 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001091 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001092 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001093 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1094 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001095 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001096 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001097 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1098 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001099 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001100 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001101 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001102 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001103 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001104}
1105
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001106bool Class::ProxyDescriptorEquals(const char* match) {
1107 DCHECK(IsProxyClass());
1108 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001109}
1110
Mathieu Chartiere401d142015-04-22 13:56:20 -07001111// TODO: Move this to java_lang_Class.cc?
1112ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001113 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001114 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001115 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001116 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001117 continue;
1118 }
1119 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001120 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001122 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001123 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001124 return nullptr;
1125 }
1126 }
1127 return nullptr;
1128}
1129
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130uint32_t Class::Depth() {
1131 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001132 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001133 depth++;
1134 }
1135 return depth;
1136}
1137
Andreas Gampea5b09a62016-11-17 15:21:22 -08001138dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001139 std::string temp;
1140 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampea5b09a62016-11-17 15:21:22 -08001141 return (type_id == nullptr)
1142 ? dex::TypeIndex(DexFile::kDexNoIndex)
1143 : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001144}
1145
Andreas Gampe542451c2016-07-26 09:02:02 -07001146template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001147ObjPtr<Method> Class::GetDeclaredMethodInternal(
1148 Thread* self,
1149 ObjPtr<Class> klass,
1150 ObjPtr<String> name,
1151 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001152 // Covariant return types permit the class to define multiple
1153 // methods with the same name and parameter types. Prefer to
1154 // return a non-synthetic method in such situations. We may
1155 // still return a synthetic method to handle situations like
1156 // escalated visibility. We never return miranda methods that
1157 // were synthesized by the runtime.
1158 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1159 StackHandleScope<3> hs(self);
1160 auto h_method_name = hs.NewHandle(name);
1161 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1162 ThrowNullPointerException("name == null");
1163 return nullptr;
1164 }
1165 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001166 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001167 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001168 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1169 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001170 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001171 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001172 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1173 if (UNLIKELY(self->IsExceptionPending())) {
1174 return nullptr;
1175 }
1176 continue;
1177 }
1178 auto modifiers = m.GetAccessFlags();
1179 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001180 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001181 }
1182 if ((modifiers & kAccMiranda) == 0) {
1183 result = &m; // Remember as potential result if it's not a miranda method.
1184 }
1185 }
1186 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001187 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001188 auto modifiers = m.GetAccessFlags();
1189 if ((modifiers & kAccConstructor) != 0) {
1190 continue;
1191 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001192 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001193 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001194 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001195 if (np_name == nullptr) {
1196 self->AssertPendingException();
1197 return nullptr;
1198 }
1199 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1200 if (UNLIKELY(self->IsExceptionPending())) {
1201 return nullptr;
1202 }
1203 continue;
1204 }
1205 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001206 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001207 }
1208 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1209 result = &m;
1210 }
1211 }
1212 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001213 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001214 : nullptr;
1215}
1216
1217template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001218ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001219 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001220 ObjPtr<Class> klass,
1221 ObjPtr<String> name,
1222 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001223template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001224ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001225 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001226 ObjPtr<Class> klass,
1227 ObjPtr<String> name,
1228 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001229template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001230ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001231 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001232 ObjPtr<Class> klass,
1233 ObjPtr<String> name,
1234 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001235template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001236ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001237 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001238 ObjPtr<Class> klass,
1239 ObjPtr<String> name,
1240 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001241
Andreas Gampe542451c2016-07-26 09:02:02 -07001242template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001243ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001244 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001245 ObjPtr<Class> klass,
1246 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001247 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001248 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001249 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001250 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001251 : nullptr;
1252}
1253
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001254// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001255
Andreas Gampe542451c2016-07-26 09:02:02 -07001256template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001257ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001258 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001259 ObjPtr<Class> klass,
1260 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001261template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001262ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001263 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001264 ObjPtr<Class> klass,
1265 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001266template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001267ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001268 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001269 ObjPtr<Class> klass,
1270 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001271template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001272ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001273 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001274 ObjPtr<Class> klass,
1275 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001276
Andreas Gampe715fdc22016-04-18 17:07:30 -07001277int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1278 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1279 return default_value;
1280 }
1281 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001282 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001283 return default_value;
1284 }
1285 return flags;
1286}
1287
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001288void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1289 if (Runtime::Current()->IsActiveTransaction()) {
1290 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1291 } else {
1292 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1293 }
1294}
1295
David Sehr709b0702016-10-13 09:12:37 -07001296std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1297 if (klass == nullptr) {
1298 return "null";
1299 }
1300 return klass->PrettyDescriptor();
1301}
1302
1303std::string Class::PrettyDescriptor() {
1304 std::string temp;
1305 return art::PrettyDescriptor(GetDescriptor(&temp));
1306}
1307
1308std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1309 if (c == nullptr) {
1310 return "null";
1311 }
1312 return c->PrettyClass();
1313}
1314
1315std::string Class::PrettyClass() {
1316 std::string result;
1317 result += "java.lang.Class<";
1318 result += PrettyDescriptor();
1319 result += ">";
1320 return result;
1321}
1322
1323std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1324 if (c == nullptr) {
1325 return "null";
1326 }
1327 return c->PrettyClassAndClassLoader();
1328}
1329
1330std::string Class::PrettyClassAndClassLoader() {
1331 std::string result;
1332 result += "java.lang.Class<";
1333 result += PrettyDescriptor();
1334 result += ",";
1335 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1336 // TODO: add an identifying hash value for the loader
1337 result += ">";
1338 return result;
1339}
1340
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001341} // namespace mirror
1342} // namespace art