Change dex caches to be weak roots
Changed dex caches to be weak roots. This is necessary for class
unloading since the resolved types arrays would keep classes live
when they should be unloaded. Currently the dex caches still don't
get freed due to the class loader roots.
Also deleted some unused functionality in image writer.
Bug: 22720414
Change-Id: If22cb3cad7e3baabc8158a77d7f20799faf4c341
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index 44b78ff..1950d56 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -812,11 +812,15 @@
DumpDexCode(vios->Stream(), dex_file, code_item);
}
+ std::unique_ptr<StackHandleScope<1>> hs;
std::unique_ptr<verifier::MethodVerifier> verifier;
if (Runtime::Current() != nullptr) {
+ // We need to have the handle scope stay live until after the verifier since the verifier has
+ // a handle to the dex cache from hs.
+ hs.reset(new StackHandleScope<1>(Thread::Current()));
vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
ScopedIndentation indent2(vios);
- verifier.reset(DumpVerifier(vios,
+ verifier.reset(DumpVerifier(vios, hs.get(),
dex_method_idx, &dex_file, class_def, code_item,
method_access_flags));
}
@@ -1389,6 +1393,7 @@
}
verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
+ StackHandleScope<1>* hs,
uint32_t dex_method_idx,
const DexFile* dex_file,
const DexFile::ClassDef& class_def,
@@ -1396,9 +1401,8 @@
uint32_t method_access_flags) {
if ((method_access_flags & kAccNative) == 0) {
ScopedObjectAccess soa(Thread::Current());
- StackHandleScope<1> hs(soa.Self());
Handle<mirror::DexCache> dex_cache(
- hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
+ hs->NewHandle(Runtime::Current()->GetClassLinker()->RegisterDexFile(*dex_file)));
DCHECK(options_.class_loader_ != nullptr);
return verifier::MethodVerifier::VerifyMethodAndDump(
soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
@@ -1599,10 +1603,13 @@
dex_cache_arrays_.clear();
{
ReaderMutexLock mu(self, *class_linker->DexLock());
- for (size_t i = 0; i < class_linker->GetDexCacheCount(); ++i) {
- auto* dex_cache = class_linker->GetDexCache(i);
- dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
- dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
+ for (jobject weak_root : class_linker->GetDexCaches()) {
+ mirror::DexCache* dex_cache =
+ down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root));
+ if (dex_cache != nullptr) {
+ dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
+ dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
+ }
}
}
ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);