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