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