| 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" |
| David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/utils.h" |
| Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 23 | #include "class-alloc-inl.h" |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 24 | #include "class-inl.h" |
| Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 25 | #include "class_root.h" |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file-inl.h" |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 27 | #include "gc/accounting/card_table-inl.h" |
| 28 | #include "object-inl.h" |
| Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 29 | #include "object_array-alloc-inl.h" |
| 30 | #include "object_array-inl.h" |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 31 | #include "stack_trace_element.h" |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 32 | #include "well_known_classes.h" |
| 33 | |
| 34 | namespace art { |
| 35 | namespace mirror { |
| 36 | |
| Alex Light | 4f2e957 | 2017-03-16 13:13:31 -0700 | [diff] [blame] | 37 | uint32_t ClassExt::ClassSize(PointerSize pointer_size) { |
| 38 | uint32_t vtable_entries = Object::kVTableLength; |
| 39 | return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size); |
| 40 | } |
| 41 | |
| Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 42 | void ClassExt::SetObsoleteArrays(ObjPtr<PointerArray> methods, |
| 43 | ObjPtr<ObjectArray<DexCache>> dex_caches) { |
| Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 44 | CHECK_EQ(methods.IsNull(), dex_caches.IsNull()); |
| 45 | auto obsolete_dex_cache_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_dex_caches_); |
| 46 | auto obsolete_methods_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_methods_); |
| 47 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 48 | SetFieldObject<false>(obsolete_dex_cache_off, dex_caches); |
| 49 | SetFieldObject<false>(obsolete_methods_off, methods); |
| Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 52 | // We really need to be careful how we update this. If we ever in the future make it so that |
| 53 | // these arrays are written into without all threads being suspended we have a race condition! This |
| 54 | // race could cause obsolete methods to be missed. |
| Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 55 | bool ClassExt::ExtendObsoleteArrays(Handle<ClassExt> h_this, Thread* self, uint32_t increase) { |
| Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 56 | // TODO It would be good to check that we have locked the class associated with this ClassExt. |
| Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 57 | StackHandleScope<4> hs(self); |
| Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 58 | Handle<PointerArray> old_methods(hs.NewHandle(h_this->GetObsoleteMethods())); |
| 59 | Handle<ObjectArray<DexCache>> old_dex_caches(hs.NewHandle(h_this->GetObsoleteDexCaches())); |
| 60 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 61 | size_t new_len; |
| Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 62 | if (old_methods == nullptr) { |
| 63 | CHECK(old_dex_caches == nullptr); |
| Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 64 | new_len = increase; |
| 65 | } else { |
| 66 | CHECK_EQ(old_methods->GetLength(), old_dex_caches->GetLength()); |
| 67 | new_len = increase + old_methods->GetLength(); |
| 68 | } |
| 69 | Handle<PointerArray> new_methods(hs.NewHandle<PointerArray>( |
| 70 | cl->AllocPointerArray(self, new_len))); |
| 71 | if (new_methods.IsNull()) { |
| 72 | // Fail. |
| 73 | self->AssertPendingOOMException(); |
| 74 | return false; |
| 75 | } |
| 76 | Handle<ObjectArray<DexCache>> new_dex_caches(hs.NewHandle<ObjectArray<DexCache>>( |
| 77 | ObjectArray<DexCache>::Alloc(self, |
| 78 | cl->FindClass(self, |
| 79 | "[Ljava/lang/DexCache;", |
| 80 | ScopedNullHandle<ClassLoader>()), |
| 81 | new_len))); |
| 82 | if (new_dex_caches.IsNull()) { |
| 83 | // Fail. |
| 84 | self->AssertPendingOOMException(); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if (!old_methods.IsNull()) { |
| 89 | // Copy the old contents. |
| 90 | new_methods->Memcpy(0, |
| 91 | old_methods.Get(), |
| 92 | 0, |
| 93 | old_methods->GetLength(), |
| 94 | cl->GetImagePointerSize()); |
| 95 | new_dex_caches->AsObjectArray<Object>()->AssignableCheckingMemcpy<false>( |
| 96 | 0, old_dex_caches->AsObjectArray<Object>(), 0, old_dex_caches->GetLength(), false); |
| 97 | } |
| 98 | // Set the fields. |
| 99 | h_this->SetObsoleteArrays(new_methods.Get(), new_dex_caches.Get()); |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 104 | ObjPtr<ClassExt> ClassExt::Alloc(Thread* self) { |
| 105 | return ObjPtr<ClassExt>::DownCast(GetClassRoot<ClassExt>()->AllocObject(self)); |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 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 | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 116 | void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) { |
| Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 117 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 118 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes); |
| Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 121 | void ClassExt::SetPreRedefineClassDefIndex(uint16_t index) { |
| 122 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 123 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_class_def_index_), |
| 124 | static_cast<int32_t>(index)); |
| 125 | } |
| 126 | |
| 127 | void ClassExt::SetPreRedefineDexFile(const DexFile* dex_file) { |
| 128 | DCHECK(!Runtime::Current()->IsActiveTransaction()); |
| 129 | SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_dex_file_ptr_), |
| 130 | static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file))); |
| 131 | } |
| 132 | |
| Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 133 | } // namespace mirror |
| 134 | } // namespace art |