1 /*
2  * Copyright (C) 2015 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_table.h"
18 
19 #include "mirror/class-inl.h"
20 
21 namespace art {
22 
ClassTable()23 ClassTable::ClassTable() : lock_("Class loader classes", kClassLoaderClassesLock) {
24   Runtime* const runtime = Runtime::Current();
25   classes_.push_back(ClassSet(runtime->GetHashTableMinLoadFactor(),
26                               runtime->GetHashTableMaxLoadFactor()));
27 }
28 
FreezeSnapshot()29 void ClassTable::FreezeSnapshot() {
30   WriterMutexLock mu(Thread::Current(), lock_);
31   classes_.push_back(ClassSet());
32 }
33 
Contains(mirror::Class * klass)34 bool ClassTable::Contains(mirror::Class* klass) {
35   ReaderMutexLock mu(Thread::Current(), lock_);
36   for (ClassSet& class_set : classes_) {
37     auto it = class_set.Find(GcRoot<mirror::Class>(klass));
38     if (it != class_set.end()) {
39       return it->Read() == klass;
40     }
41   }
42   return false;
43 }
44 
LookupByDescriptor(mirror::Class * klass)45 mirror::Class* ClassTable::LookupByDescriptor(mirror::Class* klass) {
46   ReaderMutexLock mu(Thread::Current(), lock_);
47   for (ClassSet& class_set : classes_) {
48     auto it = class_set.Find(GcRoot<mirror::Class>(klass));
49     if (it != class_set.end()) {
50       return it->Read();
51     }
52   }
53   return nullptr;
54 }
55 
UpdateClass(const char * descriptor,mirror::Class * klass,size_t hash)56 mirror::Class* ClassTable::UpdateClass(const char* descriptor, mirror::Class* klass, size_t hash) {
57   WriterMutexLock mu(Thread::Current(), lock_);
58   // Should only be updating latest table.
59   auto existing_it = classes_.back().FindWithHash(descriptor, hash);
60   if (kIsDebugBuild && existing_it == classes_.back().end()) {
61     for (const ClassSet& class_set : classes_) {
62       if (class_set.FindWithHash(descriptor, hash) != class_set.end()) {
63         LOG(FATAL) << "Updating class found in frozen table " << descriptor;
64       }
65     }
66     LOG(FATAL) << "Updating class not found " << descriptor;
67   }
68   mirror::Class* const existing = existing_it->Read();
69   CHECK_NE(existing, klass) << descriptor;
70   CHECK(!existing->IsResolved()) << descriptor;
71   CHECK_EQ(klass->GetStatus(), mirror::Class::kStatusResolving) << descriptor;
72   CHECK(!klass->IsTemp()) << descriptor;
73   VerifyObject(klass);
74   // Update the element in the hash set with the new class. This is safe to do since the descriptor
75   // doesn't change.
76   *existing_it = GcRoot<mirror::Class>(klass);
77   return existing;
78 }
79 
NumZygoteClasses() const80 size_t ClassTable::NumZygoteClasses() const {
81   ReaderMutexLock mu(Thread::Current(), lock_);
82   size_t sum = 0;
83   for (size_t i = 0; i < classes_.size() - 1; ++i) {
84     sum += classes_[i].Size();
85   }
86   return sum;
87 }
88 
NumNonZygoteClasses() const89 size_t ClassTable::NumNonZygoteClasses() const {
90   ReaderMutexLock mu(Thread::Current(), lock_);
91   return classes_.back().Size();
92 }
93 
Lookup(const char * descriptor,size_t hash)94 mirror::Class* ClassTable::Lookup(const char* descriptor, size_t hash) {
95   ReaderMutexLock mu(Thread::Current(), lock_);
96   for (ClassSet& class_set : classes_) {
97     auto it = class_set.FindWithHash(descriptor, hash);
98     if (it != class_set.end()) {
99      return it->Read();
100     }
101   }
102   return nullptr;
103 }
104 
Insert(mirror::Class * klass)105 void ClassTable::Insert(mirror::Class* klass) {
106   WriterMutexLock mu(Thread::Current(), lock_);
107   classes_.back().Insert(GcRoot<mirror::Class>(klass));
108 }
109 
InsertWithHash(mirror::Class * klass,size_t hash)110 void ClassTable::InsertWithHash(mirror::Class* klass, size_t hash) {
111   WriterMutexLock mu(Thread::Current(), lock_);
112   classes_.back().InsertWithHash(GcRoot<mirror::Class>(klass), hash);
113 }
114 
Remove(const char * descriptor)115 bool ClassTable::Remove(const char* descriptor) {
116   WriterMutexLock mu(Thread::Current(), lock_);
117   for (ClassSet& class_set : classes_) {
118     auto it = class_set.Find(descriptor);
119     if (it != class_set.end()) {
120       class_set.Erase(it);
121       return true;
122     }
123   }
124   return false;
125 }
126 
operator ()(const GcRoot<mirror::Class> & root) const127 uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& root)
128     const {
129   std::string temp;
130   return ComputeModifiedUtf8Hash(root.Read()->GetDescriptor(&temp));
131 }
132 
operator ()(const GcRoot<mirror::Class> & a,const GcRoot<mirror::Class> & b) const133 bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
134                                                        const GcRoot<mirror::Class>& b) const {
135   DCHECK_EQ(a.Read()->GetClassLoader(), b.Read()->GetClassLoader());
136   std::string temp;
137   return a.Read()->DescriptorEquals(b.Read()->GetDescriptor(&temp));
138 }
139 
operator ()(const GcRoot<mirror::Class> & a,const char * descriptor) const140 bool ClassTable::ClassDescriptorHashEquals::operator()(const GcRoot<mirror::Class>& a,
141                                                        const char* descriptor) const {
142   return a.Read()->DescriptorEquals(descriptor);
143 }
144 
operator ()(const char * descriptor) const145 uint32_t ClassTable::ClassDescriptorHashEquals::operator()(const char* descriptor) const {
146   return ComputeModifiedUtf8Hash(descriptor);
147 }
148 
InsertStrongRoot(mirror::Object * obj)149 bool ClassTable::InsertStrongRoot(mirror::Object* obj) {
150   WriterMutexLock mu(Thread::Current(), lock_);
151   DCHECK(obj != nullptr);
152   for (GcRoot<mirror::Object>& root : strong_roots_) {
153     if (root.Read() == obj) {
154       return false;
155     }
156   }
157   strong_roots_.push_back(GcRoot<mirror::Object>(obj));
158   return true;
159 }
160 
WriteToMemory(uint8_t * ptr) const161 size_t ClassTable::WriteToMemory(uint8_t* ptr) const {
162   ReaderMutexLock mu(Thread::Current(), lock_);
163   ClassSet combined;
164   // Combine all the class sets in case there are multiple, also adjusts load factor back to
165   // default in case classes were pruned.
166   for (const ClassSet& class_set : classes_) {
167     for (const GcRoot<mirror::Class>& root : class_set) {
168       combined.Insert(root);
169     }
170   }
171   const size_t ret = combined.WriteToMemory(ptr);
172   // Sanity check.
173   if (kIsDebugBuild && ptr != nullptr) {
174     size_t read_count;
175     ClassSet class_set(ptr, /*make copy*/false, &read_count);
176     class_set.Verify();
177   }
178   return ret;
179 }
180 
ReadFromMemory(uint8_t * ptr)181 size_t ClassTable::ReadFromMemory(uint8_t* ptr) {
182   size_t read_count = 0;
183   AddClassSet(ClassSet(ptr, /*make copy*/false, &read_count));
184   return read_count;
185 }
186 
AddClassSet(ClassSet && set)187 void ClassTable::AddClassSet(ClassSet&& set) {
188   WriterMutexLock mu(Thread::Current(), lock_);
189   classes_.insert(classes_.begin(), std::move(set));
190 }
191 
ClearStrongRoots()192 void ClassTable::ClearStrongRoots() {
193   WriterMutexLock mu(Thread::Current(), lock_);
194   strong_roots_.clear();
195 }
196 }  // namespace art
197