class_ext.cc revision 1e3926ae224c6418c6e1209bb134c28116a2c1fb
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 "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 "stack_trace_element.h"
28#include "utils.h"
29#include "well_known_classes.h"
30
31namespace art {
32namespace mirror {
33
34GcRoot<Class> ClassExt::dalvik_system_ClassExt_;
35
36uint32_t ClassExt::ClassSize(PointerSize pointer_size) {
37  uint32_t vtable_entries = Object::kVTableLength;
38  return Class::ComputeClassSize(true, vtable_entries, 0, 0, 0, 0, 0, pointer_size);
39}
40
41void ClassExt::SetObsoleteArrays(ObjPtr<PointerArray> methods,
42                                 ObjPtr<ObjectArray<DexCache>> dex_caches) {
43  CHECK_EQ(methods.IsNull(), dex_caches.IsNull());
44  auto obsolete_dex_cache_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_dex_caches_);
45  auto obsolete_methods_off = OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_methods_);
46  DCHECK(!Runtime::Current()->IsActiveTransaction());
47  SetFieldObject<false>(obsolete_dex_cache_off, dex_caches.Ptr());
48  SetFieldObject<false>(obsolete_methods_off, methods.Ptr());
49}
50
51// We really need to be careful how we update this. If we ever in the future make it so that
52// these arrays are written into without all threads being suspended we have a race condition! This
53// race could cause obsolete methods to be missed.
54bool ClassExt::ExtendObsoleteArrays(Thread* self, uint32_t increase) {
55  // TODO It would be good to check that we have locked the class associated with this ClassExt.
56  StackHandleScope<5> hs(self);
57  Handle<ClassExt> h_this(hs.NewHandle(this));
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;
62  if (old_methods == nullptr) {
63    CHECK(old_dex_caches == nullptr);
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
104ClassExt* ClassExt::Alloc(Thread* self) {
105  DCHECK(dalvik_system_ClassExt_.Read() != nullptr);
106  return down_cast<ClassExt*>(dalvik_system_ClassExt_.Read()->AllocObject(self).Ptr());
107}
108
109void 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
117void ClassExt::SetOriginalDexFile(ObjPtr<Object> bytes) {
118  DCHECK(!Runtime::Current()->IsActiveTransaction());
119  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ClassExt, original_dex_file_), bytes);
120}
121
122void ClassExt::SetClass(ObjPtr<Class> dalvik_system_ClassExt) {
123  CHECK(dalvik_system_ClassExt != nullptr);
124  dalvik_system_ClassExt_ = GcRoot<Class>(dalvik_system_ClassExt);
125}
126
127void ClassExt::ResetClass() {
128  CHECK(!dalvik_system_ClassExt_.IsNull());
129  dalvik_system_ClassExt_ = GcRoot<Class>(nullptr);
130}
131
132void ClassExt::VisitRoots(RootVisitor* visitor) {
133  dalvik_system_ClassExt_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
134}
135
136}  // namespace mirror
137}  // namespace art
138