blob: 0cfe29bed9048fdb635749c9ffc05869b9f1e559 [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) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700279 ObjPtr<Class> interface = GetDirectInterface(self, h_this, 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,
802 Handle<Class> klass,
803 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)?
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700807 for (ObjPtr<Class> k = klass.Get(); 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 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700813 // Wrap k incase it moves during GetDirectInterface.
814 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700815 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800816 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700817 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800818 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700819 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700820 f = FindStaticField(self, interface, name, type);
821 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800822 return f;
823 }
824 }
825 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700826 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800827}
828
Vladimir Markobb268b12016-06-30 15:52:56 +0100829ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700830 ObjPtr<Class> klass,
831 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700832 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700833 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700835 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700836 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800837 return f;
838 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100839 // Though GetDirectInterface() should not cause thread suspension when called
840 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700841 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700842 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700843 Handle<Class> h_k(hs.NewHandle(k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800844 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700845 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700846 ObjPtr<Class> interface = GetDirectInterface(self, h_k, i);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700847 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
848 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800849 return f;
850 }
851 }
852 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700853 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800854}
855
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700856ArtField* Class::FindField(Thread* self,
857 Handle<Class> klass,
858 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700859 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800860 // Find a field using the JLS field resolution order
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700861 for (ObjPtr<Class> k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700863 ArtField* f = k->FindDeclaredInstanceField(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 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700868 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869 return f;
870 }
871 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700872 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700873 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700874 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800875 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700876 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700877 f = interface->FindStaticField(self, interface, name, type);
878 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800879 return f;
880 }
881 }
882 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700883 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884}
885
Andreas Gampe542451c2016-07-26 09:02:02 -0700886void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800888 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700889 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800890 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 }
892 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200893}
894
Ian Rogers1ff3c982014-08-12 02:30:58 -0700895const char* Class::GetDescriptor(std::string* storage) {
896 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700897 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700898 } else if (IsArrayClass()) {
899 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000900 } else if (IsProxyClass()) {
901 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700902 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700903 } else {
904 const DexFile& dex_file = GetDexFile();
905 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
906 return dex_file.GetTypeDescriptor(type_id);
907 }
908}
909
Ian Rogers1ff3c982014-08-12 02:30:58 -0700910const char* Class::GetArrayDescriptor(std::string* storage) {
911 std::string temp;
912 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
913 *storage = "[";
914 *storage += elem_desc;
915 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700916}
917
918const DexFile::ClassDef* Class::GetClassDef() {
919 uint16_t class_def_idx = GetDexClassDefIndex();
920 if (class_def_idx == DexFile::kDexNoIndex16) {
921 return nullptr;
922 }
923 return &GetDexFile().GetClassDef(class_def_idx);
924}
925
Andreas Gampea5b09a62016-11-17 15:21:22 -0800926dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700927 DCHECK(!IsPrimitive());
928 DCHECK(!IsArrayClass());
929 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
930}
931
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700932ObjPtr<Class> Class::GetDirectInterface(Thread* self,
933 Handle<Class> klass,
934 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700935 DCHECK(klass.Get() != nullptr);
936 DCHECK(!klass->IsPrimitive());
937 if (klass->IsArrayClass()) {
938 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
939 if (idx == 0) {
940 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
941 } else {
942 DCHECK_EQ(1U, idx);
943 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
944 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000945 } else if (klass->IsProxyClass()) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700946 ObjPtr<ObjectArray<Class>> interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700947 DCHECK(interfaces != nullptr);
948 return interfaces->Get(idx);
949 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800950 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700951 ObjPtr<Class> interface = klass->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700952 if (interface == nullptr) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800953 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(),
954 type_idx,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700955 klass.Get());
956 CHECK(interface != nullptr || self->IsExceptionPending());
957 }
958 return interface;
959 }
960}
961
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700962ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Calin Juravle52503d82015-11-11 16:58:31 +0000963 DCHECK(klass.Get() != nullptr);
964 DCHECK(!klass->IsInterface());
965 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700966 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +0000967 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700968 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -0700969 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -0700970 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +0000971 }
Calin Juravle52503d82015-11-11 16:58:31 +0000972 return common_super_class;
973}
974
Mathieu Chartierf8322842014-05-16 10:59:25 -0700975const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700976 const DexFile& dex_file = GetDexFile();
977 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200978 if (dex_class_def == nullptr) {
979 // Generated classes have no class def.
980 return nullptr;
981 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700982 return dex_file.GetSourceFile(*dex_class_def);
983}
984
985std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700986 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000987 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700988 return dex_cache->GetLocation()->ToModifiedUtf8();
989 }
990 // Arrays and proxies are generated and have no corresponding dex file location.
991 return "generated class";
992}
993
994const DexFile::TypeList* Class::GetInterfaceTypeList() {
995 const DexFile::ClassDef* class_def = GetClassDef();
996 if (class_def == nullptr) {
997 return nullptr;
998 }
999 return GetDexFile().GetInterfacesList(*class_def);
1000}
1001
Andreas Gampe542451c2016-07-26 09:02:02 -07001002void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001003 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001004 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001005 const size_t table_length = table->GetLength();
1006 SetEmbeddedVTableLength(table_length);
1007 for (size_t i = 0; i < table_length; i++) {
1008 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001009 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001010 // Keep java.lang.Object class's vtable around for since it's easier
1011 // to be reused by array classes during their linking.
1012 if (!IsObjectClass()) {
1013 SetVTable(nullptr);
1014 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001015}
1016
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001017class ReadBarrierOnNativeRootsVisitor {
1018 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001019 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001020 MemberOffset offset ATTRIBUTE_UNUSED,
1021 bool is_static ATTRIBUTE_UNUSED) const {}
1022
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001023 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001024 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001025 if (!root->IsNull()) {
1026 VisitRoot(root);
1027 }
1028 }
1029
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001030 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001031 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001032 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1033 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001034 if (old_ref != new_ref) {
1035 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1036 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001037 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001038 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001039 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1040 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001041 }
1042 }
1043};
1044
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001045// The pre-fence visitor for Class::CopyOf().
1046class CopyClassVisitor {
1047 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001048 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001049 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001050 size_t new_length,
1051 size_t copy_bytes,
1052 ImTable* imt,
1053 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001054 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001055 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001056 }
1057
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001058 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001059 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001060 StackHandleScope<1> hs(self_);
1061 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001062 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1063 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001064 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1065 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001066 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001067 // Visit all of the references to make sure there is no from space references in the native
1068 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001069 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001070 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001071 }
1072
1073 private:
1074 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001075 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001076 const size_t new_length_;
1077 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001078 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001079 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001080 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1081};
1082
Andreas Gampe542451c2016-07-26 09:02:02 -07001083Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001084 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1085 // We may get copied by a compacting GC.
1086 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001087 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001088 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001089 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1090 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001091 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001092 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001093 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1094 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001095 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001096 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001097 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001098 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001099 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001100}
1101
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001102bool Class::ProxyDescriptorEquals(const char* match) {
1103 DCHECK(IsProxyClass());
1104 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001105}
1106
Mathieu Chartiere401d142015-04-22 13:56:20 -07001107// TODO: Move this to java_lang_Class.cc?
1108ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001109 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001110 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001111 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001112 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001113 continue;
1114 }
1115 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001116 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001118 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001119 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001120 return nullptr;
1121 }
1122 }
1123 return nullptr;
1124}
1125
Mathieu Chartiere401d142015-04-22 13:56:20 -07001126uint32_t Class::Depth() {
1127 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001128 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 depth++;
1130 }
1131 return depth;
1132}
1133
Andreas Gampea5b09a62016-11-17 15:21:22 -08001134dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001135 std::string temp;
1136 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampea5b09a62016-11-17 15:21:22 -08001137 return (type_id == nullptr)
1138 ? dex::TypeIndex(DexFile::kDexNoIndex)
1139 : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001140}
1141
Andreas Gampe542451c2016-07-26 09:02:02 -07001142template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001143ObjPtr<Method> Class::GetDeclaredMethodInternal(
1144 Thread* self,
1145 ObjPtr<Class> klass,
1146 ObjPtr<String> name,
1147 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001148 // Covariant return types permit the class to define multiple
1149 // methods with the same name and parameter types. Prefer to
1150 // return a non-synthetic method in such situations. We may
1151 // still return a synthetic method to handle situations like
1152 // escalated visibility. We never return miranda methods that
1153 // were synthesized by the runtime.
1154 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1155 StackHandleScope<3> hs(self);
1156 auto h_method_name = hs.NewHandle(name);
1157 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1158 ThrowNullPointerException("name == null");
1159 return nullptr;
1160 }
1161 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001162 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001163 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001164 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1165 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001166 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001167 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001168 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1169 if (UNLIKELY(self->IsExceptionPending())) {
1170 return nullptr;
1171 }
1172 continue;
1173 }
1174 auto modifiers = m.GetAccessFlags();
1175 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001176 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001177 }
1178 if ((modifiers & kAccMiranda) == 0) {
1179 result = &m; // Remember as potential result if it's not a miranda method.
1180 }
1181 }
1182 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001183 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001184 auto modifiers = m.GetAccessFlags();
1185 if ((modifiers & kAccConstructor) != 0) {
1186 continue;
1187 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001188 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001189 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001190 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001191 if (np_name == nullptr) {
1192 self->AssertPendingException();
1193 return nullptr;
1194 }
1195 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1196 if (UNLIKELY(self->IsExceptionPending())) {
1197 return nullptr;
1198 }
1199 continue;
1200 }
1201 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001202 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001203 }
1204 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1205 result = &m;
1206 }
1207 }
1208 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001209 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001210 : nullptr;
1211}
1212
1213template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001214ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001215 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001216 ObjPtr<Class> klass,
1217 ObjPtr<String> name,
1218 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001219template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001220ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001221 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001222 ObjPtr<Class> klass,
1223 ObjPtr<String> name,
1224 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001225template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001226ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001227 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001228 ObjPtr<Class> klass,
1229 ObjPtr<String> name,
1230 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001231template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001232ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001233 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001234 ObjPtr<Class> klass,
1235 ObjPtr<String> name,
1236 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001237
Andreas Gampe542451c2016-07-26 09:02:02 -07001238template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001239ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001240 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001241 ObjPtr<Class> klass,
1242 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001243 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001244 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001245 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001246 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001247 : nullptr;
1248}
1249
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001250// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001251
Andreas Gampe542451c2016-07-26 09:02:02 -07001252template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001253ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001254 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001255 ObjPtr<Class> klass,
1256 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001257template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001258ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001259 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001260 ObjPtr<Class> klass,
1261 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001262template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001263ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001264 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001265 ObjPtr<Class> klass,
1266 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001267template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001268ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001269 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001270 ObjPtr<Class> klass,
1271 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001272
Andreas Gampe715fdc22016-04-18 17:07:30 -07001273int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1274 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1275 return default_value;
1276 }
1277 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001278 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001279 return default_value;
1280 }
1281 return flags;
1282}
1283
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001284void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1285 if (Runtime::Current()->IsActiveTransaction()) {
1286 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1287 } else {
1288 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1289 }
1290}
1291
David Sehr709b0702016-10-13 09:12:37 -07001292std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1293 if (klass == nullptr) {
1294 return "null";
1295 }
1296 return klass->PrettyDescriptor();
1297}
1298
1299std::string Class::PrettyDescriptor() {
1300 std::string temp;
1301 return art::PrettyDescriptor(GetDescriptor(&temp));
1302}
1303
1304std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1305 if (c == nullptr) {
1306 return "null";
1307 }
1308 return c->PrettyClass();
1309}
1310
1311std::string Class::PrettyClass() {
1312 std::string result;
1313 result += "java.lang.Class<";
1314 result += PrettyDescriptor();
1315 result += ">";
1316 return result;
1317}
1318
1319std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1320 if (c == nullptr) {
1321 return "null";
1322 }
1323 return c->PrettyClassAndClassLoader();
1324}
1325
1326std::string Class::PrettyClassAndClassLoader() {
1327 std::string result;
1328 result += "java.lang.Class<";
1329 result += PrettyDescriptor();
1330 result += ",";
1331 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1332 // TODO: add an identifying hash value for the loader
1333 result += ">";
1334 return result;
1335}
1336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001337} // namespace mirror
1338} // namespace art