blob: e18c526fc338e2de9b2ad8fbbbca782e22f25cbc [file] [log] [blame]
Elliott Hughes64f574f2013-02-20 14:57:12 -08001/*
2 * Copyright (C) 2013 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 "object_registry.h"
18
Sebastien Hertze2d628b2014-10-23 15:39:33 +020019#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010020#include "jni/jni_internal.h"
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "mirror/class.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080022#include "mirror/throwable.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070023#include "obj_ptr-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070024#include "scoped_thread_state_change-inl.h"
Elliott Hughes64f574f2013-02-20 14:57:12 -080025
26namespace art {
27
Elliott Hughes64f574f2013-02-20 14:57:12 -080028std::ostream& operator<<(std::ostream& os, const ObjectRegistryEntry& rhs) {
29 os << "ObjectRegistryEntry[" << rhs.jni_reference_type
30 << ",reference=" << rhs.jni_reference
31 << ",count=" << rhs.reference_count
32 << ",id=" << rhs.id << "]";
33 return os;
34}
35
Elliott Hughes0f827162013-02-26 12:12:58 -080036ObjectRegistry::ObjectRegistry()
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070037 : lock_("ObjectRegistry lock", kJdwpObjectRegistryLock), next_id_(1) {
Hiroshi Yamauchi8a433242017-03-07 14:39:22 -080038 Locks::AddToExpectedMutexesOnWeakRefAccess(&lock_);
39}
40
41ObjectRegistry::~ObjectRegistry() {
42 Locks::RemoveFromExpectedMutexesOnWeakRefAccess(&lock_);
Elliott Hughes64f574f2013-02-20 14:57:12 -080043}
44
Mathieu Chartier3398c782016-09-30 10:27:43 -070045JDWP::RefTypeId ObjectRegistry::AddRefType(ObjPtr<mirror::Class> c) {
Sebastien Hertz261bc042015-04-08 09:36:07 +020046 return Add(c);
47}
48
49JDWP::RefTypeId ObjectRegistry::AddRefType(Handle<mirror::Class> c_h) {
50 return Add(c_h);
Elliott Hughes64f574f2013-02-20 14:57:12 -080051}
52
Mathieu Chartier3398c782016-09-30 10:27:43 -070053JDWP::ObjectId ObjectRegistry::Add(ObjPtr<mirror::Object> o) {
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070054 if (o == nullptr) {
Elliott Hughes64f574f2013-02-20 14:57:12 -080055 return 0;
56 }
Sebastien Hertz261bc042015-04-08 09:36:07 +020057 Thread* const self = Thread::Current();
58 StackHandleScope<1> hs(self);
59 return InternalAdd(hs.NewHandle(o));
60}
61
62// Template instantiations must be declared below.
63template<class T>
64JDWP::ObjectId ObjectRegistry::Add(Handle<T> obj_h) {
Andreas Gampefa4333d2017-02-14 11:10:34 -080065 if (obj_h == nullptr) {
Sebastien Hertz261bc042015-04-08 09:36:07 +020066 return 0;
67 }
68 return InternalAdd(obj_h);
69}
70
71// Explicit template instantiation.
72template
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070073REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070074REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Sebastien Hertz261bc042015-04-08 09:36:07 +020075JDWP::ObjectId ObjectRegistry::Add(Handle<mirror::Object> obj_h);
76
77template
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070078REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -070079REQUIRES(!Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_)
Sebastien Hertz261bc042015-04-08 09:36:07 +020080JDWP::ObjectId ObjectRegistry::Add(Handle<mirror::Throwable> obj_h);
81
82template<class T>
83JDWP::ObjectId ObjectRegistry::InternalAdd(Handle<T> obj_h) {
Andreas Gampefa4333d2017-02-14 11:10:34 -080084 CHECK(obj_h != nullptr);
Elliott Hughes64f574f2013-02-20 14:57:12 -080085
Sebastien Hertze2d628b2014-10-23 15:39:33 +020086 Thread* const self = Thread::Current();
Sebastien Hertze4266c52014-10-29 12:06:51 +010087 self->AssertNoPendingException();
Sebastien Hertz69206392015-04-07 15:54:25 +020088 // Object::IdentityHashCode may cause these locks to be held so check we do not already
89 // hold them.
90 Locks::thread_list_lock_->AssertNotHeld(self);
91 Locks::thread_suspend_count_lock_->AssertNotHeld(self);
Sebastien Hertze4266c52014-10-29 12:06:51 +010092
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070093 // Call IdentityHashCode here to avoid a lock level violation between lock_ and monitor_lock.
Sebastien Hertze2d628b2014-10-23 15:39:33 +020094 int32_t identity_hash_code = obj_h->IdentityHashCode();
95
96 ScopedObjectAccessUnchecked soa(self);
Elliott Hughes64f574f2013-02-20 14:57:12 -080097 MutexLock mu(soa.Self(), lock_);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -070098 ObjectRegistryEntry* entry = nullptr;
Sebastien Hertze2d628b2014-10-23 15:39:33 +020099 if (ContainsLocked(soa.Self(), obj_h.Get(), identity_hash_code, &entry)) {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800100 // This object was already in our map.
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800101 ++entry->reference_count;
102 } else {
103 entry = new ObjectRegistryEntry;
104 entry->jni_reference_type = JNIWeakGlobalRefType;
105 entry->jni_reference = nullptr;
106 entry->reference_count = 0;
107 entry->id = 0;
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700108 entry->identity_hash_code = identity_hash_code;
109 object_to_entry_.insert(std::make_pair(identity_hash_code, entry));
Elliott Hughes64f574f2013-02-20 14:57:12 -0800110
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800111 // This object isn't in the registry yet, so add it.
112 JNIEnv* env = soa.Env();
Elliott Hughes64f574f2013-02-20 14:57:12 -0800113
Sebastien Hertze2d628b2014-10-23 15:39:33 +0200114 jobject local_reference = soa.AddLocalReference<jobject>(obj_h.Get());
Elliott Hughes64f574f2013-02-20 14:57:12 -0800115
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800116 entry->jni_reference_type = JNIWeakGlobalRefType;
117 entry->jni_reference = env->NewWeakGlobalRef(local_reference);
118 entry->reference_count = 1;
119 entry->id = next_id_++;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800120
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800121 id_to_entry_.Put(entry->id, entry);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800122
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800123 env->DeleteLocalRef(local_reference);
124 }
125 return entry->id;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800126}
127
Mathieu Chartier3398c782016-09-30 10:27:43 -0700128bool ObjectRegistry::ContainsLocked(Thread* self,
129 ObjPtr<mirror::Object> o,
130 int32_t identity_hash_code,
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700131 ObjectRegistryEntry** out_entry) {
132 DCHECK(o != nullptr);
133 for (auto it = object_to_entry_.lower_bound(identity_hash_code), end = object_to_entry_.end();
134 it != end && it->first == identity_hash_code; ++it) {
135 ObjectRegistryEntry* entry = it->second;
136 if (o == self->DecodeJObject(entry->jni_reference)) {
137 if (out_entry != nullptr) {
138 *out_entry = entry;
139 }
140 return true;
141 }
142 }
143 return false;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800144}
145
146void ObjectRegistry::Clear() {
Sebastien Hertz55f65342015-01-13 22:48:34 +0100147 Thread* const self = Thread::Current();
148
149 // We must not hold the mutator lock exclusively if we want to delete weak global
150 // references. Otherwise this can lead to a deadlock with a running GC:
151 // 1. GC thread disables access to weak global references, then releases
152 // mutator lock.
153 // 2. JDWP thread takes mutator lock exclusively after suspending all
154 // threads.
155 // 3. GC thread waits for shared mutator lock which is held by JDWP
156 // thread.
157 // 4. JDWP thread clears weak global references but need to wait for GC
158 // thread to re-enable access to them.
159 Locks::mutator_lock_->AssertNotExclusiveHeld(self);
160
Elliott Hughes64f574f2013-02-20 14:57:12 -0800161 MutexLock mu(self, lock_);
162 VLOG(jdwp) << "Object registry contained " << object_to_entry_.size() << " entries";
Elliott Hughes64f574f2013-02-20 14:57:12 -0800163 // Delete all the JNI references.
164 JNIEnv* env = self->GetJniEnv();
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800165 for (const auto& pair : object_to_entry_) {
Sebastien Hertza0328702014-06-25 22:06:12 +0200166 const ObjectRegistryEntry* entry = pair.second;
167 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
168 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800169 } else {
Sebastien Hertza0328702014-06-25 22:06:12 +0200170 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800171 }
Sebastien Hertza0328702014-06-25 22:06:12 +0200172 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800173 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800174 // Clear the maps.
175 object_to_entry_.clear();
176 id_to_entry_.clear();
177}
178
Vladimir Marko83114892019-04-11 13:05:50 +0100179ObjPtr<mirror::Object> ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* error) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800180 Thread* self = Thread::Current();
181 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800182 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800183 if (it == id_to_entry_.end()) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700184 *error = JDWP::ERR_INVALID_OBJECT;
185 return nullptr;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800186 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800187 ObjectRegistryEntry& entry = *it->second;
Ian Rogersc0542af2014-09-03 16:16:56 -0700188 *error = JDWP::ERR_NONE;
Vladimir Marko83114892019-04-11 13:05:50 +0100189 return self->DecodeJObject(entry.jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800190}
191
Jeff Hao449db332013-04-12 18:30:52 -0700192jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) {
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100193 if (id == 0) {
Sebastien Hertz7d955652014-10-22 10:57:10 +0200194 return nullptr;
Sebastien Hertz0630ab52013-11-28 18:53:35 +0100195 }
Jeff Hao449db332013-04-12 18:30:52 -0700196 Thread* self = Thread::Current();
197 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800198 auto it = id_to_entry_.find(id);
Jeff Hao449db332013-04-12 18:30:52 -0700199 CHECK(it != id_to_entry_.end()) << id;
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800200 ObjectRegistryEntry& entry = *it->second;
Jeff Hao449db332013-04-12 18:30:52 -0700201 return entry.jni_reference;
202}
203
Elliott Hughes64f574f2013-02-20 14:57:12 -0800204void ObjectRegistry::DisableCollection(JDWP::ObjectId id) {
205 Thread* self = Thread::Current();
206 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800207 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100208 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800209 Promote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800210}
211
212void ObjectRegistry::EnableCollection(JDWP::ObjectId id) {
213 Thread* self = Thread::Current();
214 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800215 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100216 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800217 Demote(*it->second);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800218}
219
220void ObjectRegistry::Demote(ObjectRegistryEntry& entry) {
221 if (entry.jni_reference_type == JNIGlobalRefType) {
222 Thread* self = Thread::Current();
223 JNIEnv* env = self->GetJniEnv();
224 jobject global = entry.jni_reference;
225 entry.jni_reference = env->NewWeakGlobalRef(entry.jni_reference);
226 entry.jni_reference_type = JNIWeakGlobalRefType;
227 env->DeleteGlobalRef(global);
228 }
229}
230
231void ObjectRegistry::Promote(ObjectRegistryEntry& entry) {
232 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
233 Thread* self = Thread::Current();
234 JNIEnv* env = self->GetJniEnv();
235 jobject weak = entry.jni_reference;
236 entry.jni_reference = env->NewGlobalRef(entry.jni_reference);
237 entry.jni_reference_type = JNIGlobalRefType;
238 env->DeleteWeakGlobalRef(weak);
239 }
240}
241
242bool ObjectRegistry::IsCollected(JDWP::ObjectId id) {
243 Thread* self = Thread::Current();
244 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800245 auto it = id_to_entry_.find(id);
Sebastien Hertze96060a2013-12-11 12:06:28 +0100246 CHECK(it != id_to_entry_.end());
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800247 ObjectRegistryEntry& entry = *it->second;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800248 if (entry.jni_reference_type == JNIWeakGlobalRefType) {
249 JNIEnv* env = self->GetJniEnv();
Sebastien Hertz7d955652014-10-22 10:57:10 +0200250 return env->IsSameObject(entry.jni_reference, nullptr); // Has the jweak been collected?
Elliott Hughes64f574f2013-02-20 14:57:12 -0800251 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700252 return false; // We hold a strong reference, so we know this is live.
Elliott Hughes64f574f2013-02-20 14:57:12 -0800253 }
254}
255
256void ObjectRegistry::DisposeObject(JDWP::ObjectId id, uint32_t reference_count) {
257 Thread* self = Thread::Current();
258 MutexLock mu(self, lock_);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800259 auto it = id_to_entry_.find(id);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800260 if (it == id_to_entry_.end()) {
261 return;
262 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800263 ObjectRegistryEntry* entry = it->second;
264 entry->reference_count -= reference_count;
265 if (entry->reference_count <= 0) {
Elliott Hughes64f574f2013-02-20 14:57:12 -0800266 JNIEnv* env = self->GetJniEnv();
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700267 // Erase the object from the maps. Note object may be null if it's
268 // a weak ref and the GC has cleared it.
269 int32_t hash_code = entry->identity_hash_code;
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800270 for (auto inner_it = object_to_entry_.lower_bound(hash_code), end = object_to_entry_.end();
271 inner_it != end && inner_it->first == hash_code; ++inner_it) {
272 if (entry == inner_it->second) {
273 object_to_entry_.erase(inner_it);
Hiroshi Yamauchib5a9e3d2014-06-09 12:11:20 -0700274 break;
275 }
276 }
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800277 if (entry->jni_reference_type == JNIWeakGlobalRefType) {
278 env->DeleteWeakGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800279 } else {
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800280 env->DeleteGlobalRef(entry->jni_reference);
Elliott Hughes64f574f2013-02-20 14:57:12 -0800281 }
Elliott Hughes64f574f2013-02-20 14:57:12 -0800282 id_to_entry_.erase(id);
Mathieu Chartier412c7fc2014-02-07 12:18:39 -0800283 delete entry;
Elliott Hughes64f574f2013-02-20 14:57:12 -0800284 }
285}
286
Elliott Hughes64f574f2013-02-20 14:57:12 -0800287} // namespace art