• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
26 #include "dex/dex_file-inl.h"
27 #include "gc/accounting/card_table-inl.h"
28 #include "object-inl.h"
29 #include "object_array-alloc-inl.h"
30 #include "object_array-inl.h"
31 #include "stack_trace_element.h"
32 #include "well_known_classes.h"
33 
34 namespace art {
35 namespace mirror {
36 
ClassSize(PointerSize pointer_size)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 
SetObsoleteArrays(ObjPtr<PointerArray> methods,ObjPtr<ObjectArray<DexCache>> dex_caches)42 void ClassExt::SetObsoleteArrays(ObjPtr<PointerArray> methods,
43                                  ObjPtr<ObjectArray<DexCache>> dex_caches) {
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());
48   SetFieldObject<false>(obsolete_dex_cache_off, dex_caches);
49   SetFieldObject<false>(obsolete_methods_off, methods);
50 }
51 
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.
ExtendObsoleteArrays(Thread * self,uint32_t increase)55 bool ClassExt::ExtendObsoleteArrays(Thread* self, uint32_t increase) {
56   // TODO It would be good to check that we have locked the class associated with this ClassExt.
57   StackHandleScope<5> hs(self);
58   Handle<ClassExt> h_this(hs.NewHandle(this));
59   Handle<PointerArray> old_methods(hs.NewHandle(h_this->GetObsoleteMethods()));
60   Handle<ObjectArray<DexCache>> old_dex_caches(hs.NewHandle(h_this->GetObsoleteDexCaches()));
61   ClassLinker* cl = Runtime::Current()->GetClassLinker();
62   size_t new_len;
63   if (old_methods == nullptr) {
64     CHECK(old_dex_caches == nullptr);
65     new_len = increase;
66   } else {
67     CHECK_EQ(old_methods->GetLength(), old_dex_caches->GetLength());
68     new_len = increase + old_methods->GetLength();
69   }
70   Handle<PointerArray> new_methods(hs.NewHandle<PointerArray>(
71       cl->AllocPointerArray(self, new_len)));
72   if (new_methods.IsNull()) {
73     // Fail.
74     self->AssertPendingOOMException();
75     return false;
76   }
77   Handle<ObjectArray<DexCache>> new_dex_caches(hs.NewHandle<ObjectArray<DexCache>>(
78       ObjectArray<DexCache>::Alloc(self,
79                                    cl->FindClass(self,
80                                                  "[Ljava/lang/DexCache;",
81                                                  ScopedNullHandle<ClassLoader>()),
82                                    new_len)));
83   if (new_dex_caches.IsNull()) {
84     // Fail.
85     self->AssertPendingOOMException();
86     return false;
87   }
88 
89   if (!old_methods.IsNull()) {
90     // Copy the old contents.
91     new_methods->Memcpy(0,
92                         old_methods.Get(),
93                         0,
94                         old_methods->GetLength(),
95                         cl->GetImagePointerSize());
96     new_dex_caches->AsObjectArray<Object>()->AssignableCheckingMemcpy<false>(
97         0, old_dex_caches->AsObjectArray<Object>(), 0, old_dex_caches->GetLength(), false);
98   }
99   // Set the fields.
100   h_this->SetObsoleteArrays(new_methods.Get(), new_dex_caches.Get());
101 
102   return true;
103 }
104 
Alloc(Thread * self)105 ObjPtr<ClassExt> ClassExt::Alloc(Thread* self) {
106   return ObjPtr<ClassExt>::DownCast(GetClassRoot<ClassExt>()->AllocObject(self));
107 }
108 
SetVerifyError(ObjPtr<Object> err)109 void ClassExt::SetVerifyError(ObjPtr<Object> err) {
110   if (Runtime::Current()->IsActiveTransaction()) {
111     SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err);
112   } else {
113     SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, verify_error_), err);
114   }
115 }
116 
SetOriginalDexFile(ObjPtr<Object> bytes)117 void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) {
118   DCHECK(!Runtime::Current()->IsActiveTransaction());
119   SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes);
120 }
121 
SetPreRedefineClassDefIndex(uint16_t index)122 void ClassExt::SetPreRedefineClassDefIndex(uint16_t index) {
123   DCHECK(!Runtime::Current()->IsActiveTransaction());
124   SetField32<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_class_def_index_),
125       static_cast<int32_t>(index));
126 }
127 
SetPreRedefineDexFile(const DexFile * dex_file)128 void ClassExt::SetPreRedefineDexFile(const DexFile* dex_file) {
129   DCHECK(!Runtime::Current()->IsActiveTransaction());
130   SetField64<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, pre_redefine_dex_file_ptr_),
131       static_cast<int64_t>(reinterpret_cast<uintptr_t>(dex_file)));
132 }
133 
134 }  // namespace mirror
135 }  // namespace art
136