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-inl.h"
18
19 #include "art_method-inl.h"
20 #include "base/casts.h"
21 #include "base/pointer_size.h"
22 #include "base/utils.h"
23 #include "class-alloc-inl.h"
24 #include "class-inl.h"
25 #include "class_root-inl.h"
26 #include "dex/dex_file-inl.h"
27 #include "gc/accounting/card_table-inl.h"
28 #include "mirror/object.h"
29 #include "mirror/object_array.h"
30 #include "object-inl.h"
31 #include "object_array-alloc-inl.h"
32 #include "object_array-inl.h"
33 #include "stack_trace_element.h"
34 #include "well_known_classes.h"
35
36 namespace art HIDDEN {
37 namespace mirror {
38
ClassSize(PointerSize pointer_size)39 uint32_t ClassExt::ClassSize(PointerSize pointer_size) {
40 uint32_t vtable_entries = Object::kVTableLength;
41 return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, 0, pointer_size);
42 }
43
SetObsoleteArrays(ObjPtr<PointerArray> methods,ObjPtr<ObjectArray<DexCache>> dex_caches)44 void ClassExt::SetObsoleteArrays(ObjPtr<PointerArray> methods,
45 ObjPtr<ObjectArray<DexCache>> dex_caches) {
46 CHECK_EQ(methods.IsNull(), dex_caches.IsNull());
47 auto obsolete_dex_cache_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_dex_caches_);
48 auto obsolete_methods_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_methods_);
49 DCHECK(!Runtime::Current()->IsActiveTransaction());
50 SetFieldObject<false>(obsolete_dex_cache_off, dex_caches);
51 SetFieldObject<false>(obsolete_methods_off, methods);
52 }
53
SetIdsArraysForClassExtExtData(ObjPtr<Object> marker)54 void ClassExt::SetIdsArraysForClassExtExtData(ObjPtr<Object> marker) {
55 CHECK(!marker.IsNull());
56 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, instance_jfield_ids_), marker);
57 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, static_jfield_ids_), marker);
58 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, jmethod_ids_), marker);
59 }
60
61 // We really need to be careful how we update this. If we ever in the future make it so that
62 // these arrays are written into without all threads being suspended we have a race condition! This
63 // race could cause obsolete methods to be missed.
ExtendObsoleteArrays(Handle<ClassExt> h_this,Thread * self,uint32_t increase)64 bool ClassExt::ExtendObsoleteArrays(Handle<ClassExt> h_this, Thread* self, uint32_t increase) {
65 // TODO It would be good to check that we have locked the class associated with this ClassExt.
66 StackHandleScope<4> hs(self);
67 Handle<PointerArray> old_methods(hs.NewHandle(h_this->GetObsoleteMethods()));
68 Handle<ObjectArray<DexCache>> old_dex_caches(hs.NewHandle(h_this->GetObsoleteDexCaches()));
69 ClassLinker* cl = Runtime::Current()->GetClassLinker();
70 size_t new_len;
71 if (old_methods == nullptr) {
72 CHECK(old_dex_caches == nullptr);
73 new_len = increase;
74 } else {
75 CHECK_EQ(old_methods->GetLength(), old_dex_caches->GetLength());
76 new_len = increase + old_methods->GetLength();
77 }
78 Handle<PointerArray> new_methods(hs.NewHandle<PointerArray>(
79 cl->AllocPointerArray(self, new_len)));
80 if (new_methods.IsNull()) {
81 // Fail.
82 self->AssertPendingOOMException();
83 return false;
84 }
85 Handle<ObjectArray<DexCache>> new_dex_caches(hs.NewHandle<ObjectArray<DexCache>>(
86 ObjectArray<DexCache>::Alloc(self,
87 cl->FindSystemClass(self, "[Ljava/lang/DexCache;"),
88 new_len)));
89 if (new_dex_caches.IsNull()) {
90 // Fail.
91 self->AssertPendingOOMException();
92 return false;
93 }
94
95 if (!old_methods.IsNull()) {
96 // Copy the old contents.
97 new_methods->Memcpy(0,
98 old_methods.Get(),
99 0,
100 old_methods->GetLength(),
101 cl->GetImagePointerSize());
102 new_dex_caches->AsObjectArray<Object>()->AssignableCheckingMemcpy<false>(
103 0, old_dex_caches->AsObjectArray<Object>(), 0, old_dex_caches->GetLength(), false);
104 }
105 // Set the fields.
106 h_this->SetObsoleteArrays(new_methods.Get(), new_dex_caches.Get());
107
108 return true;
109 }
110
SetObsoleteClass(ObjPtr<Class> klass)111 void ClassExt::SetObsoleteClass(ObjPtr<Class> klass) {
112 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_class_), klass);
113 }
114
Alloc(Thread * self)115 ObjPtr<ClassExt> ClassExt::Alloc(Thread* self) {
116 return ObjPtr<ClassExt>::DownCast(GetClassRoot<ClassExt>()->AllocObject(self));
117 }
118
SetErroneousStateError(ObjPtr<Throwable> err)119 void ClassExt::SetErroneousStateError(ObjPtr<Throwable> err) {
120 if (Runtime::Current()->IsActiveTransaction()) {
121 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(ClassExt, erroneous_state_error_), err);
122 } else {
123 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, erroneous_state_error_), err);
124 }
125 }
126
SetOriginalDexFile(ObjPtr<Object> bytes)127 void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) {
128 DCHECK(!Runtime::Current()->IsActiveTransaction());
129 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes);
130 }
131
SetPreRedefineClassDefIndex(uint16_t index)132 void ClassExt::SetPreRedefineClassDefIndex(uint16_t index) {
133 DCHECK(!Runtime::Current()->IsActiveTransaction());
134 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_class_def_index_),
135 static_cast<int32_t>(index));
136 }
137
SetPreRedefineDexFile(const DexFile * dex_file)138 void ClassExt::SetPreRedefineDexFile(const DexFile* dex_file) {
139 DCHECK(!Runtime::Current()->IsActiveTransaction());
140 SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_dex_file_ptr_),
141 static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file)));
142 }
143
144 } // namespace mirror
145 } // namespace art
146