Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Alex Light | 4f2e957 | 2017-03-16 13:13:31 -0700 | [diff] [blame] | 17 | #include "class_ext-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include "art_method-inl.h" |
| 20 | #include "base/casts.h" |
| 21 | #include "base/enums.h" |
| 22 | #include "class-inl.h" |
| 23 | #include "dex_file-inl.h" |
| 24 | #include "gc/accounting/card_table-inl.h" |
| 25 | #include "object-inl.h" |
| 26 | #include "object_array.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 27 | #include "stack_trace_element.h" |
| 28 | #include "utils.h" |
| 29 | #include "well_known_classes.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace mirror { |
| 33 | |
| 34 | GcRoot<Class> ClassExt::dalvik_system_ClassExt_; |
| 35 | |
Alex Light | 4f2e957 | 2017-03-16 13:13:31 -0700 | [diff] [blame] | 36 | uint32_t ClassExt::ClassSize(PointerSize pointer_size) { |
| 37 | uint32_t vtable_entries = Object::kVTableLength; |
| 38 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size); |
| 39 | } |
| 40 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 41 | void ClassExt::SetObsoleteArrays(ObjPtr<PointerArray> methods, |
| 42 | ObjPtr<ObjectArray<DexCache>> dex_caches) { |
| 43 | DCHECK_EQ(GetLockOwnerThreadId(), Thread::Current()->GetThreadId()) |
| 44 | << "Obsolete arrays are set without synchronization!"; |
| 45 | CHECK_EQ(methods.IsNull(), dex_caches.IsNull()); |
| 46 | auto obsolete_dex_cache_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_dex_caches_); |
| 47 | auto obsolete_methods_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_methods_); |
| 48 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 49 | SetFieldObject<false>(obsolete_dex_cache_off, dex_caches.Ptr()); |
| 50 | SetFieldObject<false>(obsolete_methods_off, methods.Ptr()); |
| 51 | } |
| 52 | |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 53 | // We really need to be careful how we update this. If we ever in the future make it so that |
| 54 | // these arrays are written into without all threads being suspended we have a race condition! This |
| 55 | // race could cause obsolete methods to be missed. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 56 | bool ClassExt::ExtendObsoleteArrays(Thread* self, uint32_t increase) { |
| 57 | DCHECK_EQ(GetLockOwnerThreadId(), Thread::Current()->GetThreadId()) |
| 58 | << "Obsolete arrays are set without synchronization!"; |
| 59 | StackHandleScope<5> hs(self); |
| 60 | Handle<ClassExt> h_this(hs.NewHandle(this)); |
| 61 | Handle<PointerArray> old_methods(hs.NewHandle(h_this->GetObsoleteMethods())); |
| 62 | Handle<ObjectArray<DexCache>> old_dex_caches(hs.NewHandle(h_this->GetObsoleteDexCaches())); |
| 63 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 64 | size_t new_len; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 65 | if (old_methods == nullptr) { |
| 66 | CHECK(old_dex_caches == nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 67 | new_len = increase; |
| 68 | } else { |
| 69 | CHECK_EQ(old_methods->GetLength(), old_dex_caches->GetLength()); |
| 70 | new_len = increase + old_methods->GetLength(); |
| 71 | } |
| 72 | Handle<PointerArray> new_methods(hs.NewHandle<PointerArray>( |
| 73 | cl->AllocPointerArray(self, new_len))); |
| 74 | if (new_methods.IsNull()) { |
| 75 | // Fail. |
| 76 | self->AssertPendingOOMException(); |
| 77 | return false; |
| 78 | } |
| 79 | Handle<ObjectArray<DexCache>> new_dex_caches(hs.NewHandle<ObjectArray<DexCache>>( |
| 80 | ObjectArray<DexCache>::Alloc(self, |
| 81 | cl->FindClass(self, |
| 82 | "[Ljava/lang/DexCache;", |
| 83 | ScopedNullHandle<ClassLoader>()), |
| 84 | new_len))); |
| 85 | if (new_dex_caches.IsNull()) { |
| 86 | // Fail. |
| 87 | self->AssertPendingOOMException(); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (!old_methods.IsNull()) { |
| 92 | // Copy the old contents. |
| 93 | new_methods->Memcpy(0, |
| 94 | old_methods.Get(), |
| 95 | 0, |
| 96 | old_methods->GetLength(), |
| 97 | cl->GetImagePointerSize()); |
| 98 | new_dex_caches->AsObjectArray<Object>()->AssignableCheckingMemcpy<false>( |
| 99 | 0, old_dex_caches->AsObjectArray<Object>(), 0, old_dex_caches->GetLength(), false); |
| 100 | } |
| 101 | // Set the fields. |
| 102 | h_this->SetObsoleteArrays(new_methods.Get(), new_dex_caches.Get()); |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 107 | ClassExt* ClassExt::Alloc(Thread* self) { |
| 108 | DCHECK(dalvik_system_ClassExt_.Read() != nullptr); |
| 109 | return down_cast<ClassExt*>(dalvik_system_ClassExt_.Read()->AllocObject(self).Ptr()); |
| 110 | } |
| 111 | |
| 112 | void ClassExt::SetVerifyError(ObjPtr<Object> err) { |
| 113 | if (Runtime::Current()->IsActiveTransaction()) { |
| 114 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err); |
| 115 | } else { |
| 116 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err); |
| 117 | } |
| 118 | } |
| 119 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame^] | 120 | void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 121 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame^] | 122 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 125 | void ClassExt::SetClass(ObjPtr<Class> dalvik_system_ClassExt) { |
| 126 | CHECK(dalvik_system_ClassExt != nullptr); |
| 127 | dalvik_system_ClassExt_ = GcRoot<Class>(dalvik_system_ClassExt); |
| 128 | } |
| 129 | |
| 130 | void ClassExt::ResetClass() { |
| 131 | CHECK(!dalvik_system_ClassExt_.IsNull()); |
| 132 | dalvik_system_ClassExt_ = GcRoot<Class>(nullptr); |
| 133 | } |
| 134 | |
| 135 | void ClassExt::VisitRoots(RootVisitor* visitor) { |
| 136 | dalvik_system_ClassExt_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
| 137 | } |
| 138 | |
| 139 | } // namespace mirror |
| 140 | } // namespace art |