From: Mathieu Chartier Date: Thu, 6 Oct 2016 01:32:08 +0000 (-0700) Subject: Move remaining jobject related functions to use ObjPtr X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=c4f3925490a73da8dc74884a1deb965d4ecaf14e;p=android-x86%2Fart.git Move remaining jobject related functions to use ObjPtr Also added ObjPtr::DownCast. Bug: 31113334 Test: test-art-host Change-Id: I59c253211dc435579ffdfd49f856861ab13d262c --- diff --git a/compiler/driver/compiler_driver_test.cc b/compiler/driver/compiler_driver_test.cc index 96f17acca..e323b1684 100644 --- a/compiler/driver/compiler_driver_test.cc +++ b/compiler/driver/compiler_driver_test.cc @@ -207,8 +207,8 @@ TEST_F(CompilerDriverMethodsTest, Selection) { ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); ScopedObjectAccess soa(self); StackHandleScope<1> hs(self); - Handle h_loader(hs.NewHandle( - reinterpret_cast(self->DecodeJObject(class_loader)))); + Handle h_loader( + hs.NewHandle(soa.Decode(class_loader))); mirror::Class* klass = class_linker->FindClass(self, "LStaticLeafMethods;", h_loader); ASSERT_NE(klass, nullptr); @@ -265,8 +265,8 @@ class CompilerDriverProfileTest : public CompilerDriverTest { Thread* self = Thread::Current(); ScopedObjectAccess soa(self); StackHandleScope<1> hs(self); - Handle h_loader(hs.NewHandle( - reinterpret_cast(self->DecodeJObject(class_loader)))); + Handle h_loader( + hs.NewHandle(soa.Decode(class_loader))); mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader); ASSERT_NE(klass, nullptr); diff --git a/compiler/image_writer.cc b/compiler/image_writer.cc index 66938b2e0..8ae04a1e4 100644 --- a/compiler/image_writer.cc +++ b/compiler/image_writer.cc @@ -433,9 +433,9 @@ void ImageWriter::PrepareDexCacheArraySlots() { Thread* const self = Thread::Current(); ReaderMutexLock mu(self, *class_linker->DexLock()); for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); - if (dex_cache == nullptr || IsInBootImage(dex_cache)) { + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); + if (dex_cache == nullptr || IsInBootImage(dex_cache.Ptr())) { continue; } const DexFile* dex_file = dex_cache->GetDexFile(); @@ -464,7 +464,9 @@ void ImageWriter::PrepareDexCacheArraySlots() { } } -void ImageWriter::AddDexCacheArrayRelocation(void* array, size_t offset, DexCache* dex_cache) { +void ImageWriter::AddDexCacheArrayRelocation(void* array, + size_t offset, + ObjPtr dex_cache) { if (array != nullptr) { DCHECK(!IsInBootImage(array)); size_t oat_index = GetOatIndexForDexCache(dex_cache); @@ -878,7 +880,7 @@ void ImageWriter::PruneNonImageClasses() { if (self->IsJWeakCleared(data.weak_root)) { continue; } - mirror::DexCache* dex_cache = self->DecodeJObject(data.weak_root)->AsDexCache(); + ObjPtr dex_cache = self->DecodeJObject(data.weak_root)->AsDexCache(); for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { Class* klass = dex_cache->GetResolvedType(i); if (klass != nullptr && !KeepClass(klass)) { @@ -1005,13 +1007,13 @@ ObjectArray* ImageWriter::CreateImageRoots(size_t oat_index) const { ReaderMutexLock mu(self, *class_linker->DexLock()); // Count number of dex caches not in the boot image. for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache == nullptr) { continue; } const DexFile* dex_file = dex_cache->GetDexFile(); - if (!IsInBootImage(dex_cache)) { + if (!IsInBootImage(dex_cache.Ptr())) { dex_cache_count += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; } } @@ -1024,13 +1026,13 @@ ObjectArray* ImageWriter::CreateImageRoots(size_t oat_index) const { size_t non_image_dex_caches = 0; // Re-count number of non image dex caches. for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache == nullptr) { continue; } const DexFile* dex_file = dex_cache->GetDexFile(); - if (!IsInBootImage(dex_cache)) { + if (!IsInBootImage(dex_cache.Ptr())) { non_image_dex_caches += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; } } @@ -1038,14 +1040,15 @@ ObjectArray* ImageWriter::CreateImageRoots(size_t oat_index) const { << "The number of non-image dex caches changed."; size_t i = 0; for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache == nullptr) { continue; } const DexFile* dex_file = dex_cache->GetDexFile(); - if (!IsInBootImage(dex_cache) && image_dex_files.find(dex_file) != image_dex_files.end()) { - dex_caches->Set(i, dex_cache); + if (!IsInBootImage(dex_cache.Ptr()) && + image_dex_files.find(dex_file) != image_dex_files.end()) { + dex_caches->Set(i, dex_cache.Ptr()); ++i; } } @@ -2384,12 +2387,10 @@ size_t ImageWriter::GetOatIndexForDexFile(const DexFile* dex_file) const { return it->second; } -size_t ImageWriter::GetOatIndexForDexCache(mirror::DexCache* dex_cache) const { - if (dex_cache == nullptr) { - return GetDefaultOatIndex(); - } else { - return GetOatIndexForDexFile(dex_cache->GetDexFile()); - } +size_t ImageWriter::GetOatIndexForDexCache(ObjPtr dex_cache) const { + return (dex_cache == nullptr) + ? GetDefaultOatIndex() + : GetOatIndexForDexFile(dex_cache->GetDexFile()); } void ImageWriter::UpdateOatFileLayout(size_t oat_index, diff --git a/compiler/image_writer.h b/compiler/image_writer.h index acd16813c..c9cf4cbc1 100644 --- a/compiler/image_writer.h +++ b/compiler/image_writer.h @@ -132,7 +132,7 @@ class ImageWriter FINAL { size_t GetOatIndexForDexFile(const DexFile* dex_file) const; // Get the index of the oat file containing the dex file served by the dex cache. - size_t GetOatIndexForDexCache(mirror::DexCache* dex_cache) const + size_t GetOatIndexForDexCache(ObjPtr dex_cache) const REQUIRES_SHARED(Locks::mutator_lock_); // Update the oat layout for the given oat file. @@ -334,7 +334,7 @@ class ImageWriter FINAL { REQUIRES_SHARED(Locks::mutator_lock_); BinSlot GetImageBinSlot(mirror::Object* object) const REQUIRES_SHARED(Locks::mutator_lock_); - void AddDexCacheArrayRelocation(void* array, size_t offset, mirror::DexCache* dex_cache) + void AddDexCacheArrayRelocation(void* array, size_t offset, ObjPtr dex_cache) REQUIRES_SHARED(Locks::mutator_lock_); void AddMethodPointerArray(mirror::PointerArray* arr) REQUIRES_SHARED(Locks::mutator_lock_); diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc index d6006b242..f75841415 100644 --- a/oatdump/oatdump.cc +++ b/oatdump/oatdump.cc @@ -1575,10 +1575,10 @@ class ImageDumper { { ReaderMutexLock mu(self, *class_linker->DexLock()); for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache != nullptr) { - dex_caches_.insert(dex_cache); + dex_caches_.insert(dex_cache.Ptr()); } } } diff --git a/runtime/check_jni.cc b/runtime/check_jni.cc index 4dc7b317c..8a51dc2c0 100644 --- a/runtime/check_jni.cc +++ b/runtime/check_jni.cc @@ -772,7 +772,7 @@ class ScopedCheck { okay = false; } else { obj = soa.Vm()->DecodeWeakGlobal(soa.Self(), ref); - okay = Runtime::Current()->IsClearedJniWeakGlobal(obj.Ptr()); + okay = Runtime::Current()->IsClearedJniWeakGlobal(obj); } if (!okay) { AbortF("%s is an invalid %s: %p (%p)", diff --git a/runtime/class_linker-inl.h b/runtime/class_linker-inl.h index 378da57bb..fa971c4c2 100644 --- a/runtime/class_linker-inl.h +++ b/runtime/class_linker-inl.h @@ -26,6 +26,7 @@ #include "mirror/iftable.h" #include "mirror/object_array.h" #include "handle_scope-inl.h" +#include "scoped_thread_state_change-inl.h" #include @@ -247,8 +248,8 @@ ArtMethod* ClassLinker::FindMethodForProxy(mirror::Class* proxy_class, ArtMethod if (!self->IsJWeakCleared(data.weak_root) && proxy_method->HasSameDexCacheResolvedTypes(data.resolved_types, image_pointer_size_)) { - mirror::DexCache* dex_cache = down_cast( - self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache != nullptr) { ArtMethod* resolved_method = dex_cache->GetResolvedMethod( proxy_method->GetDexMethodIndex(), image_pointer_size_); diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc index ed0f0c023..48d31a4c3 100644 --- a/runtime/class_linker.cc +++ b/runtime/class_linker.cc @@ -3341,13 +3341,12 @@ mirror::DexCache* ClassLinker::FindDexCacheLocked(Thread* self, for (const DexCacheData& data : dex_caches_) { // Avoid decoding (and read barriers) other unrelated dex caches. if (data.dex_file == &dex_file) { - mirror::DexCache* dex_cache = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache != nullptr) { - return dex_cache; - } else { - break; + return dex_cache.Ptr(); } + break; } } if (allow_failure) { @@ -3356,7 +3355,8 @@ mirror::DexCache* ClassLinker::FindDexCacheLocked(Thread* self, std::string location(dex_file.GetLocation()); // Failure, dump diagnostic and abort. for (const DexCacheData& data : dex_caches_) { - mirror::DexCache* dex_cache = down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (dex_cache != nullptr) { LOG(ERROR) << "Registered dex file " << dex_cache->GetDexFile()->GetLocation(); } @@ -3370,7 +3370,7 @@ void ClassLinker::FixupDexCaches(ArtMethod* resolution_method) { ReaderMutexLock mu(self, dex_lock_); for (const DexCacheData& data : dex_caches_) { if (!self->IsJWeakCleared(data.weak_root)) { - mirror::DexCache* dex_cache = down_cast( + ObjPtr dex_cache = ObjPtr::DownCast( self->DecodeJObject(data.weak_root)); if (dex_cache != nullptr) { dex_cache->Fixup(resolution_method, image_pointer_size_); @@ -8341,9 +8341,10 @@ void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { Thread* const self = Thread::Current(); for (const ClassLoaderData& data : class_loaders_) { // Need to use DecodeJObject so that we get null for cleared JNI weak globals. - auto* const class_loader = down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr class_loader = ObjPtr::DownCast( + self->DecodeJObject(data.weak_root)); if (class_loader != nullptr) { - visitor->Visit(class_loader); + visitor->Visit(class_loader.Ptr()); } } } @@ -8371,8 +8372,8 @@ void ClassLinker::CleanupClassLoaders() { for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { const ClassLoaderData& data = *it; // Need to use DecodeJObject so that we get null for cleared JNI weak globals. - auto* const class_loader = - down_cast(self->DecodeJObject(data.weak_root)); + ObjPtr class_loader = + ObjPtr::DownCast(self->DecodeJObject(data.weak_root)); if (class_loader != nullptr) { ++it; } else { @@ -8400,8 +8401,7 @@ std::set ClassLinker::GetResolvedClasses(bool ignore_bo if (soa.Self()->IsJWeakCleared(data.weak_root)) { continue; } - mirror::DexCache* dex_cache = - down_cast(soa.Self()->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = soa.Decode(data.weak_root); if (dex_cache == nullptr) { continue; } @@ -8468,8 +8468,7 @@ std::unordered_set ClassLinker::GetClassDescriptorsForProfileKeys( ReaderMutexLock mu(self, *DexLock()); for (const ClassLinker::DexCacheData& data : GetDexCachesData()) { if (!self->IsJWeakCleared(data.weak_root)) { - mirror::DexCache* dex_cache = - down_cast(soa.Self()->DecodeJObject(data.weak_root)); + ObjPtr dex_cache = soa.Decode(data.weak_root); if (dex_cache != nullptr) { const DexFile* dex_file = dex_cache->GetDexFile(); // There could be duplicates if two dex files with the same location are mapped. diff --git a/runtime/class_linker_test.cc b/runtime/class_linker_test.cc index d45495cf6..e51411238 100644 --- a/runtime/class_linker_test.cc +++ b/runtime/class_linker_test.cc @@ -1298,7 +1298,7 @@ TEST_F(ClassLinkerTest, RegisterDexFileName) { { ReaderMutexLock mu(soa.Self(), *class_linker->DexLock()); for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { - dex_cache.Assign(down_cast(soa.Self()->DecodeJObject(data.weak_root))); + dex_cache.Assign(soa.Self()->DecodeJObject(data.weak_root)->AsDexCache()); if (dex_cache.Get() != nullptr) { break; } diff --git a/runtime/common_throws.cc b/runtime/common_throws.cc index 1e4c7725b..7fa8cf932 100644 --- a/runtime/common_throws.cc +++ b/runtime/common_throws.cc @@ -752,7 +752,7 @@ void ThrowStackOverflowError(Thread* self) { error_msg = "Could not create stack trace."; } // Throw the exception. - self->SetException(reinterpret_cast(self->DecodeJObject(exc.get()))); + self->SetException(self->DecodeJObject(exc.get())->AsThrowable()); } else { // Could not allocate a string object. error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed."; diff --git a/runtime/dex_file_annotations.cc b/runtime/dex_file_annotations.cc index 367603e1f..f0d3909bf 100644 --- a/runtime/dex_file_annotations.cc +++ b/runtime/dex_file_annotations.cc @@ -611,7 +611,7 @@ mirror::Object* CreateAnnotationMember(Handle klass, } Handle value_object(hs.NewHandle(annotation_value.value_.GetL())); - mirror::Class* annotation_member_class = + ObjPtr annotation_member_class = WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember); Handle new_member(hs.NewHandle(annotation_member_class->AllocObject(self))); mirror::Method* method_obj_ptr; diff --git a/runtime/entrypoints/quick/quick_jni_entrypoints.cc b/runtime/entrypoints/quick/quick_jni_entrypoints.cc index 446e3431a..7c7e2da74 100644 --- a/runtime/entrypoints/quick/quick_jni_entrypoints.cc +++ b/runtime/entrypoints/quick/quick_jni_entrypoints.cc @@ -132,17 +132,20 @@ static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result, Thread* self) NO_THREAD_SAFETY_ANALYSIS { // Must decode before pop. The 'result' may not be valid in case of an exception, though. - mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result); + ObjPtr o; + if (!self->IsExceptionPending()) { + o = self->DecodeJObject(result); + } PopLocalReferences(saved_local_ref_cookie, self); // Process result. if (UNLIKELY(self->GetJniEnv()->check_jni)) { // CheckReferenceResult can resolve types. StackHandleScope<1> hs(self); - HandleWrapper h_obj(hs.NewHandleWrapper(&o)); + HandleWrapperObjPtr h_obj(hs.NewHandleWrapper(&o)); CheckReferenceResult(h_obj, self); } - VerifyObject(o); - return o; + VerifyObject(o.Ptr()); + return o.Ptr(); } extern mirror::Object* JniMethodEndWithReference(jobject result, diff --git a/runtime/gc/collector/concurrent_copying.cc b/runtime/gc/collector/concurrent_copying.cc index 8b910750b..dabb6da11 100644 --- a/runtime/gc/collector/concurrent_copying.cc +++ b/runtime/gc/collector/concurrent_copying.cc @@ -1949,10 +1949,11 @@ void ConcurrentCopying::FillWithDummyObject(mirror::Object* dummy_obj, size_t by size_t data_offset = mirror::Array::DataOffset(component_size).SizeValue(); if (data_offset > byte_size) { // An int array is too big. Use java.lang.Object. - mirror::Class* java_lang_Object = WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object); - AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object); + ObjPtr java_lang_Object = + WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object); + AssertToSpaceInvariant(nullptr, MemberOffset(0), java_lang_Object.Ptr()); CHECK_EQ(byte_size, (java_lang_Object->GetObjectSize())); - dummy_obj->SetClass(java_lang_Object); + dummy_obj->SetClass(java_lang_Object.Ptr()); CHECK_EQ(byte_size, (dummy_obj->SizeOf())); } else { // Use an int array. diff --git a/runtime/gc/space/space_test.h b/runtime/gc/space/space_test.h index 17d7c87bd..777887106 100644 --- a/runtime/gc/space/space_test.h +++ b/runtime/gc/space/space_test.h @@ -62,7 +62,7 @@ class SpaceTest : public Super { byte_array_class_ = self->GetJniEnv()->NewLocalRef(byte_array_class); EXPECT_TRUE(byte_array_class_ != nullptr); } - return reinterpret_cast(self->DecodeJObject(byte_array_class_)); + return self->DecodeJObject(byte_array_class_)->AsClass(); } mirror::Object* Alloc(space::MallocSpace* alloc_space, diff --git a/runtime/interpreter/unstarted_runtime.cc b/runtime/interpreter/unstarted_runtime.cc index ac5401f5d..845fc60b1 100644 --- a/runtime/interpreter/unstarted_runtime.cc +++ b/runtime/interpreter/unstarted_runtime.cc @@ -914,7 +914,7 @@ void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits( result->SetJ(bit_cast(in)); } -static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) +static ObjPtr GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) REQUIRES_SHARED(Locks::mutator_lock_) { const DexFile* dex_file = dex_cache->GetDexFile(); if (dex_file == nullptr) { @@ -949,10 +949,10 @@ void UnstartedRuntime::UnstartedDexCacheGetDexNative( mirror::Object* src = shadow_frame->GetVRegReference(arg_offset); bool have_dex = false; if (src != nullptr) { - mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast(src)); + ObjPtr dex = GetDexFromDexCache(self, src->AsDexCache()); if (dex != nullptr) { have_dex = true; - result->SetL(dex); + result->SetL(dex.Ptr()); } } if (!have_dex) { @@ -1456,7 +1456,7 @@ void UnstartedRuntime::UnstartedMethodInvoke( ScopedLocalRef result_jobj(env, InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get())); - result->SetL(self->DecodeJObject(result_jobj.get())); + result->SetL(self->DecodeJObject(result_jobj.get()).Ptr()); // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap // InvocationTargetExceptions. diff --git a/runtime/java_vm_ext.cc b/runtime/java_vm_ext.cc index 101c14607..c5f95eb6d 100644 --- a/runtime/java_vm_ext.cc +++ b/runtime/java_vm_ext.cc @@ -640,11 +640,11 @@ void JavaVMExt::BroadcastForNewWeakGlobals() { weak_globals_add_condition_.Broadcast(self); } -mirror::Object* JavaVMExt::DecodeGlobal(IndirectRef ref) { - return globals_.SynchronizedGet(ref).Ptr(); +ObjPtr JavaVMExt::DecodeGlobal(IndirectRef ref) { + return globals_.SynchronizedGet(ref); } -void JavaVMExt::UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result) { +void JavaVMExt::UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr result) { WriterMutexLock mu(self, globals_lock_); globals_.Update(ref, result); } @@ -660,7 +660,7 @@ inline bool JavaVMExt::MayAccessWeakGlobalsUnlocked(Thread* self) const { allow_accessing_weak_globals_.LoadSequentiallyConsistent(); } -mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) { +ObjPtr JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) { // It is safe to access GetWeakRefAccessEnabled without the lock since CC uses checkpoints to call // SetWeakRefAccessEnabled, and the other collectors only modify allow_accessing_weak_globals_ // when the mutators are paused. @@ -669,23 +669,23 @@ mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) { // if MayAccessWeakGlobals is false. DCHECK_EQ(GetIndirectRefKind(ref), kWeakGlobal); if (LIKELY(MayAccessWeakGlobalsUnlocked(self))) { - return weak_globals_.SynchronizedGet(ref).Ptr(); + return weak_globals_.SynchronizedGet(ref); } MutexLock mu(self, weak_globals_lock_); return DecodeWeakGlobalLocked(self, ref); } -mirror::Object* JavaVMExt::DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) { +ObjPtr JavaVMExt::DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) { if (kDebugLocking) { weak_globals_lock_.AssertHeld(self); } while (UNLIKELY(!MayAccessWeakGlobals(self))) { weak_globals_add_condition_.WaitHoldingLocks(self); } - return weak_globals_.Get(ref).Ptr(); + return weak_globals_.Get(ref); } -mirror::Object* JavaVMExt::DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) { +ObjPtr JavaVMExt::DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) { DCHECK_EQ(GetIndirectRefKind(ref), kWeakGlobal); DCHECK(Runtime::Current()->IsShuttingDown(self)); if (self != nullptr) { @@ -695,7 +695,7 @@ mirror::Object* JavaVMExt::DecodeWeakGlobalDuringShutdown(Thread* self, Indirect if (!kUseReadBarrier) { DCHECK(allow_accessing_weak_globals_.LoadSequentiallyConsistent()); } - return weak_globals_.SynchronizedGet(ref).Ptr(); + return weak_globals_.SynchronizedGet(ref); } bool JavaVMExt::IsWeakGlobalCleared(Thread* self, IndirectRef ref) { @@ -711,7 +711,7 @@ bool JavaVMExt::IsWeakGlobalCleared(Thread* self, IndirectRef ref) { return Runtime::Current()->IsClearedJniWeakGlobal(weak_globals_.Get(ref)); } -void JavaVMExt::UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result) { +void JavaVMExt::UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr result) { MutexLock mu(self, weak_globals_lock_); weak_globals_.Update(ref, result); } diff --git a/runtime/java_vm_ext.h b/runtime/java_vm_ext.h index 558ffffe5..2e59a9d65 100644 --- a/runtime/java_vm_ext.h +++ b/runtime/java_vm_ext.h @@ -137,23 +137,23 @@ class JavaVMExt : public JavaVM { void SweepJniWeakGlobals(IsMarkedVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); - mirror::Object* DecodeGlobal(IndirectRef ref) + ObjPtr DecodeGlobal(IndirectRef ref) REQUIRES_SHARED(Locks::mutator_lock_); - void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result) + void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr result) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!globals_lock_); - mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref) + ObjPtr DecodeWeakGlobal(Thread* self, IndirectRef ref) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); - mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) + ObjPtr DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(weak_globals_lock_); // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be // null. - mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) + ObjPtr DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); @@ -166,7 +166,7 @@ class JavaVMExt : public JavaVM { return weak_globals_lock_; } - void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result) + void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr result) REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); const JNIInvokeInterface* GetUncheckedFunctions() const { diff --git a/runtime/jdwp/object_registry.cc b/runtime/jdwp/object_registry.cc index dc3bf16a0..170887e39 100644 --- a/runtime/jdwp/object_registry.cc +++ b/runtime/jdwp/object_registry.cc @@ -180,7 +180,7 @@ mirror::Object* ObjectRegistry::InternalGet(JDWP::ObjectId id, JDWP::JdwpError* } ObjectRegistryEntry& entry = *it->second; *error = JDWP::ERR_NONE; - return self->DecodeJObject(entry.jni_reference); + return self->DecodeJObject(entry.jni_reference).Ptr(); } jobject ObjectRegistry::GetJObject(JDWP::ObjectId id) { diff --git a/runtime/jit/profile_compilation_info_test.cc b/runtime/jit/profile_compilation_info_test.cc index 764458aec..1dd1e36e7 100644 --- a/runtime/jit/profile_compilation_info_test.cc +++ b/runtime/jit/profile_compilation_info_test.cc @@ -38,8 +38,8 @@ class ProfileCompilationInfoTest : public CommonRuntimeTest { Thread* self = Thread::Current(); ScopedObjectAccess soa(self); StackHandleScope<1> hs(self); - Handle h_loader(hs.NewHandle( - reinterpret_cast(self->DecodeJObject(class_loader)))); + Handle h_loader( + hs.NewHandle(self->DecodeJObject(class_loader)->AsClassLoader())); mirror::Class* klass = class_linker->FindClass(self, clazz.c_str(), h_loader); const auto pointer_size = class_linker->GetImagePointerSize(); diff --git a/runtime/jni_env_ext.cc b/runtime/jni_env_ext.cc index 0358494be..3c749d047 100644 --- a/runtime/jni_env_ext.cc +++ b/runtime/jni_env_ext.cc @@ -176,13 +176,13 @@ void JNIEnvExt::RecordMonitorEnter(jobject obj) { static std::string ComputeMonitorDescription(Thread* self, jobject obj) REQUIRES_SHARED(Locks::mutator_lock_) { - mirror::Object* o = self->DecodeJObject(obj); + ObjPtr o = self->DecodeJObject(obj); if ((o->GetLockWord(false).GetState() == LockWord::kThinLocked) && Locks::mutator_lock_->IsExclusiveHeld(self)) { // Getting the identity hashcode here would result in lock inflation and suspension of the // current thread, which isn't safe if this is the only runnable thread. return StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)", - reinterpret_cast(o), + reinterpret_cast(o.Ptr()), PrettyTypeOf(o).c_str()); } else { // IdentityHashCode can cause thread suspension, which would invalidate o if it moved. So @@ -203,7 +203,7 @@ static void RemoveMonitors(Thread* self, [self, frame, monitors](const std::pair& pair) REQUIRES_SHARED(Locks::mutator_lock_) { if (frame == pair.first) { - mirror::Object* o = self->DecodeJObject(pair.second); + ObjPtr o = self->DecodeJObject(pair.second); monitors->Remove(o); return true; } @@ -221,7 +221,7 @@ void JNIEnvExt::CheckMonitorRelease(jobject obj) { locked_objects_.erase(it); } else { // Check whether this monitor was locked in another JNI "session." - mirror::Object* mirror_obj = self->DecodeJObject(obj); + ObjPtr mirror_obj = self->DecodeJObject(obj); for (std::pair& pair : locked_objects_) { if (self->DecodeJObject(pair.second) == mirror_obj) { std::string monitor_descr = ComputeMonitorDescription(self, pair.second); diff --git a/runtime/jni_internal.cc b/runtime/jni_internal.cc index 621e2df31..f0a7c1614 100644 --- a/runtime/jni_internal.cc +++ b/runtime/jni_internal.cc @@ -528,7 +528,7 @@ class JNI { static jobject NewGlobalRef(JNIEnv* env, jobject obj) { ScopedObjectAccess soa(env); ObjPtr decoded_obj = soa.Decode(obj); - return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj.Ptr()); + return soa.Vm()->AddGlobalRef(soa.Self(), decoded_obj); } static void DeleteGlobalRef(JNIEnv* env, jobject obj) { @@ -540,7 +540,7 @@ class JNI { static jweak NewWeakGlobalRef(JNIEnv* env, jobject obj) { ScopedObjectAccess soa(env); ObjPtr decoded_obj = soa.Decode(obj); - return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj.Ptr()); + return soa.Vm()->AddWeakGlobalRef(soa.Self(), decoded_obj); } static void DeleteWeakGlobalRef(JNIEnv* env, jweak obj) { @@ -2295,7 +2295,7 @@ class JNI { if (soa.Self()->IsExceptionPending()) { return JNI_ERR; } - soa.Env()->monitors.Add(o.Ptr()); + soa.Env()->monitors.Add(o); return JNI_OK; } @@ -2307,7 +2307,7 @@ class JNI { if (soa.Self()->IsExceptionPending()) { return JNI_ERR; } - soa.Env()->monitors.Remove(o.Ptr()); + soa.Env()->monitors.Remove(o); return JNI_OK; } diff --git a/runtime/obj_ptr.h b/runtime/obj_ptr.h index 6688ba77e..74be44eb2 100644 --- a/runtime/obj_ptr.h +++ b/runtime/obj_ptr.h @@ -132,6 +132,14 @@ class ObjPtr { } } + // Static function to be friendly with null pointers. + template + static ObjPtr DownCast(ObjPtr ptr) REQUIRES_SHARED(Locks::mutator_lock_) { + static_assert(std::is_base_of::value, + "Target type must be a subtype of source type"); + return static_cast(ptr.Ptr()); + } + private: // Trim off high bits of thread local cookie. ALWAYS_INLINE static uintptr_t TrimCookie(uintptr_t cookie) { diff --git a/runtime/openjdkjvmti/transform.cc b/runtime/openjdkjvmti/transform.cc index f59e01e3d..3443aea74 100644 --- a/runtime/openjdkjvmti/transform.cc +++ b/runtime/openjdkjvmti/transform.cc @@ -327,8 +327,7 @@ jvmtiError MoveTransformedFileIntoRuntime(jclass jklass, class_linker->FindClass(self, dex_file_name, null_loader) ->FindDeclaredInstanceField("mInternalCookie", "Ljava/lang/Object;"); CHECK(dex_file_cookie_field != nullptr); - art::Handle klass( - hs.NewHandle(art::down_cast(self->DecodeJObject(jklass)))); + art::Handle klass(hs.NewHandle(self->DecodeJObject(jklass)->AsClass())); art::mirror::Object* dex_file_ptr = nullptr; art::mirror::ClassLoader* class_loader_ptr = nullptr; // Find dalvik.system.DexFile that represents the dex file we are changing. diff --git a/runtime/reference_table.cc b/runtime/reference_table.cc index 0be79efb7..e0ba8e748 100644 --- a/runtime/reference_table.cc +++ b/runtime/reference_table.cc @@ -39,9 +39,9 @@ ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max ReferenceTable::~ReferenceTable() { } -void ReferenceTable::Add(mirror::Object* obj) { +void ReferenceTable::Add(ObjPtr obj) { DCHECK(obj != nullptr); - VerifyObject(obj); + VerifyObject(obj.Ptr()); if (entries_.size() >= max_size_) { LOG(FATAL) << "ReferenceTable '" << name_ << "' " << "overflowed (" << max_size_ << " entries)"; @@ -49,10 +49,10 @@ void ReferenceTable::Add(mirror::Object* obj) { entries_.push_back(GcRoot(obj)); } -void ReferenceTable::Remove(mirror::Object* obj) { +void ReferenceTable::Remove(ObjPtr obj) { // We iterate backwards on the assumption that references are LIFO. for (int i = entries_.size() - 1; i >= 0; --i) { - mirror::Object* entry = entries_[i].Read(); + ObjPtr entry = entries_[i].Read(); if (entry == obj) { entries_.erase(entries_.begin() + i); return; @@ -62,7 +62,7 @@ void ReferenceTable::Remove(mirror::Object* obj) { // If "obj" is an array, return the number of elements in the array. // Otherwise, return zero. -static size_t GetElementCount(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { +static size_t GetElementCount(ObjPtr obj) REQUIRES_SHARED(Locks::mutator_lock_) { // We assume the special cleared value isn't an array in the if statement below. DCHECK(!Runtime::Current()->GetClearedJniWeakGlobal()->IsArrayInstance()); if (obj == nullptr || !obj->IsArrayInstance()) { @@ -76,7 +76,7 @@ static size_t GetElementCount(mirror::Object* obj) REQUIRES_SHARED(Locks::mutato // Pass in the number of elements in the array (or 0 if this is not an // array object), and the number of additional objects that are identical // or equivalent to the original. -static void DumpSummaryLine(std::ostream& os, mirror::Object* obj, size_t element_count, +static void DumpSummaryLine(std::ostream& os, ObjPtr obj, size_t element_count, int identical, int equiv) REQUIRES_SHARED(Locks::mutator_lock_) { if (obj == nullptr) { @@ -126,8 +126,8 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { // are no suspend points which can happen during the sorting process. This works since // we are guaranteed that the addresses of obj1, obj2, obj1->GetClass, obj2->GetClass wont // change during the sorting process. The classes are forwarded by ref->GetClass(). - mirror::Object* obj1 = root1.Read(); - mirror::Object* obj2 = root2.Read(); + ObjPtr obj1 = root1.Read(); + ObjPtr obj2 = root2.Read(); DCHECK(obj1 != nullptr); DCHECK(obj2 != nullptr); Runtime* runtime = Runtime::Current(); @@ -144,7 +144,7 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { return size1 < size2; } // ...and finally by address. - return obj1 < obj2; + return obj1.Ptr() < obj2.Ptr(); } }; @@ -163,7 +163,7 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { os << " Last " << (count - first) << " entries (of " << count << "):\n"; Runtime* runtime = Runtime::Current(); for (int idx = count - 1; idx >= first; --idx) { - mirror::Object* ref = entries[idx].Read(); + ObjPtr ref = entries[idx].Read(); if (ref == nullptr) { continue; } @@ -174,7 +174,7 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { if (ref->GetClass() == nullptr) { // should only be possible right after a plain dvmMalloc(). size_t size = ref->SizeOf(); - os << StringPrintf(" %5d: %p (raw) (%zd bytes)\n", idx, ref, size); + os << StringPrintf(" %5d: %p (raw) (%zd bytes)\n", idx, ref.Ptr(), size); continue; } @@ -185,7 +185,7 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { if (element_count != 0) { StringAppendF(&extras, " (%zd elements)", element_count); } else if (ref->GetClass()->IsStringClass()) { - mirror::String* s = ref->AsString(); + ObjPtr s = ref->AsString(); std::string utf8(s->ToModifiedUtf8()); if (s->GetLength() <= 16) { StringAppendF(&extras, " \"%s\"", utf8.c_str()); @@ -193,7 +193,7 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { StringAppendF(&extras, " \"%.16s... (%d chars)", utf8.c_str(), s->GetLength()); } } else if (ref->IsReferenceInstance()) { - mirror::Object* referent = ref->AsReference()->GetReferent(); + ObjPtr referent = ref->AsReference()->GetReferent(); if (referent == nullptr) { extras = " (referent is null)"; } else { @@ -219,9 +219,9 @@ void ReferenceTable::Dump(std::ostream& os, Table& entries) { os << " Summary:\n"; size_t equiv = 0; size_t identical = 0; - mirror::Object* prev = nullptr; + ObjPtr prev = nullptr; for (GcRoot& root : sorted_entries) { - mirror::Object* current = root.Read(); + ObjPtr current = root.Read(); if (prev != nullptr) { const size_t element_count = GetElementCount(prev); if (current == prev) { diff --git a/runtime/reference_table.h b/runtime/reference_table.h index 992ded0ea..8423e04e8 100644 --- a/runtime/reference_table.h +++ b/runtime/reference_table.h @@ -25,6 +25,7 @@ #include "base/allocator.h" #include "base/mutex.h" #include "gc_root.h" +#include "obj_ptr.h" #include "object_callbacks.h" namespace art { @@ -41,9 +42,9 @@ class ReferenceTable { ReferenceTable(const char* name, size_t initial_size, size_t max_size); ~ReferenceTable(); - void Add(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); + void Add(ObjPtr obj) REQUIRES_SHARED(Locks::mutator_lock_); - void Remove(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); + void Remove(ObjPtr obj) REQUIRES_SHARED(Locks::mutator_lock_); size_t Size() const; diff --git a/runtime/reflection.cc b/runtime/reflection.cc index d34b701e3..098eb0377 100644 --- a/runtime/reflection.cc +++ b/runtime/reflection.cc @@ -914,10 +914,10 @@ void UpdateReference(Thread* self, jobject obj, ObjPtr result) { } else if (kind == kHandleScopeOrInvalid) { LOG(FATAL) << "Unsupported UpdateReference for kind kHandleScopeOrInvalid"; } else if (kind == kGlobal) { - self->GetJniEnv()->vm->UpdateGlobal(self, ref, result.Ptr()); + self->GetJniEnv()->vm->UpdateGlobal(self, ref, result); } else { DCHECK_EQ(kind, kWeakGlobal); - self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result.Ptr()); + self->GetJniEnv()->vm->UpdateWeakGlobal(self, ref, result); } } diff --git a/runtime/scoped_thread_state_change-inl.h b/runtime/scoped_thread_state_change-inl.h index ac2575715..1ebfd30ff 100644 --- a/runtime/scoped_thread_state_change-inl.h +++ b/runtime/scoped_thread_state_change-inl.h @@ -83,7 +83,7 @@ template inline ObjPtr ScopedObjectAccessAlreadyRunnable::Decode(jobject obj) const { Locks::mutator_lock_->AssertSharedHeld(Self()); DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states. - return down_cast(Self()->DecodeJObject(obj)); + return down_cast(Self()->DecodeJObject(obj).Ptr()); } inline ArtField* ScopedObjectAccessAlreadyRunnable::DecodeField(jfieldID fid) const { diff --git a/runtime/thread.cc b/runtime/thread.cc index 7335e409b..3e2ecfe55 100644 --- a/runtime/thread.cc +++ b/runtime/thread.cc @@ -1856,7 +1856,7 @@ void Thread::HandleScopeVisitRoots(RootVisitor* visitor, uint32_t thread_id) { } } -mirror::Object* Thread::DecodeJObject(jobject obj) const { +ObjPtr Thread::DecodeJObject(jobject obj) const { if (obj == nullptr) { return nullptr; } @@ -1897,7 +1897,7 @@ mirror::Object* Thread::DecodeJObject(jobject obj) const { tlsPtr_.jni_env->vm->JniAbortF(nullptr, "use of deleted %s %p", ToStr(kind).c_str(), obj); } - return result.Ptr(); + return result; } bool Thread::IsJWeakCleared(jweak obj) const { @@ -2318,17 +2318,17 @@ void Thread::ThrowNewWrappedException(const char* exception_class_descriptor, // case in the compiler. We won't be able to invoke the constructor of the exception, so set // the exception fields directly. if (msg != nullptr) { - exception->SetDetailMessage(down_cast(DecodeJObject(msg_string.get()))); + exception->SetDetailMessage(DecodeJObject(msg_string.get())->AsString()); } if (cause.get() != nullptr) { - exception->SetCause(down_cast(DecodeJObject(cause.get()))); + exception->SetCause(DecodeJObject(cause.get())->AsThrowable()); } ScopedLocalRef trace(GetJniEnv(), Runtime::Current()->IsActiveTransaction() ? CreateInternalStackTrace(soa) : CreateInternalStackTrace(soa)); if (trace.get() != nullptr) { - exception->SetStackState(down_cast(DecodeJObject(trace.get()))); + exception->SetStackState(DecodeJObject(trace.get()).Ptr()); } SetException(exception.Get()); } else { diff --git a/runtime/thread.h b/runtime/thread.h index 97053decc..20b4cc144 100644 --- a/runtime/thread.h +++ b/runtime/thread.h @@ -452,7 +452,7 @@ class Thread { } // Convert a jobject into a Object* - mirror::Object* DecodeJObject(jobject obj) const REQUIRES_SHARED(Locks::mutator_lock_); + ObjPtr DecodeJObject(jobject obj) const REQUIRES_SHARED(Locks::mutator_lock_); // Checks if the weak global ref has been cleared by the GC without decoding it. bool IsJWeakCleared(jweak obj) const REQUIRES_SHARED(Locks::mutator_lock_); diff --git a/runtime/well_known_classes.cc b/runtime/well_known_classes.cc index 4dcf58fc9..78fc53ac3 100644 --- a/runtime/well_known_classes.cc +++ b/runtime/well_known_classes.cc @@ -24,6 +24,7 @@ #include "entrypoints/quick/quick_entrypoints_enum.h" #include "mirror/class.h" #include "mirror/throwable.h" +#include "obj_ptr-inl.h" #include "ScopedLocalRef.h" #include "scoped_thread_state_change-inl.h" #include "thread-inl.h" @@ -385,8 +386,8 @@ void WellKnownClasses::LateInit(JNIEnv* env) { "Ljava/lang/String;"); } -mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) { - return reinterpret_cast(Thread::Current()->DecodeJObject(global_jclass)); +ObjPtr WellKnownClasses::ToClass(jclass global_jclass) { + return ObjPtr::DownCast(Thread::Current()->DecodeJObject(global_jclass)); } } // namespace art diff --git a/runtime/well_known_classes.h b/runtime/well_known_classes.h index ddfc5b80f..248ba7f43 100644 --- a/runtime/well_known_classes.h +++ b/runtime/well_known_classes.h @@ -19,6 +19,7 @@ #include "base/mutex.h" #include "jni.h" +#include "obj_ptr.h" namespace art { @@ -41,8 +42,7 @@ struct WellKnownClasses { static ArtMethod* StringInitToStringFactory(ArtMethod* method); static uint32_t StringInitToEntryPoint(ArtMethod* method); - static mirror::Class* ToClass(jclass global_jclass) - REQUIRES_SHARED(Locks::mutator_lock_); + static ObjPtr ToClass(jclass global_jclass) REQUIRES_SHARED(Locks::mutator_lock_); static jclass com_android_dex_Dex; static jclass dalvik_annotation_optimization_CriticalNative;