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/enums.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 {
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, 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->FindClass(self,
88                                                  "[Ljava/lang/DexCache;",
89                                                  ScopedNullHandle<ClassLoader>()),
90                                    new_len)));
91   if (new_dex_caches.IsNull()) {
92     // Fail.
93     self->AssertPendingOOMException();
94     return false;
95   }
96 
97   if (!old_methods.IsNull()) {
98     // Copy the old contents.
99     new_methods->Memcpy(0,
100                         old_methods.Get(),
101                         0,
102                         old_methods->GetLength(),
103                         cl->GetImagePointerSize());
104     new_dex_caches->AsObjectArray<Object>()->AssignableCheckingMemcpy<false>(
105         0, old_dex_caches->AsObjectArray<Object>(), 0, old_dex_caches->GetLength(), false);
106   }
107   // Set the fields.
108   h_this->SetObsoleteArrays(new_methods.Get(), new_dex_caches.Get());
109 
110   return true;
111 }
112 
SetObsoleteClass(ObjPtr<Class> klass)113 void ClassExt::SetObsoleteClass(ObjPtr<Class> klass) {
114   SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_class_), klass);
115 }
116 
Alloc(Thread * self)117 ObjPtr<ClassExt> ClassExt::Alloc(Thread* self) {
118   return ObjPtr<ClassExt>::DownCast(GetClassRoot<ClassExt>()->AllocObject(self));
119 }
120 
SetVerifyError(ObjPtr<Object> err)121 void ClassExt::SetVerifyError(ObjPtr<Object> err) {
122   if (Runtime::Current()->IsActiveTransaction()) {
123     SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err);
124   } else {
125     SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err);
126   }
127 }
128 
SetOriginalDexFile(ObjPtr<Object> bytes)129 void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) {
130   DCHECK(!Runtime::Current()->IsActiveTransaction());
131   SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes);
132 }
133 
SetPreRedefineClassDefIndex(uint16_t index)134 void ClassExt::SetPreRedefineClassDefIndex(uint16_t index) {
135   DCHECK(!Runtime::Current()->IsActiveTransaction());
136   SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_class_def_index_),
137       static_cast<int32_t>(index));
138 }
139 
SetPreRedefineDexFile(const DexFile * dex_file)140 void ClassExt::SetPreRedefineDexFile(const DexFile* dex_file) {
141   DCHECK(!Runtime::Current()->IsActiveTransaction());
142   SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_dex_file_ptr_),
143       static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file)));
144 }
145 
146 }  // namespace mirror
147 }  // namespace art
148