From c971eafeff43e4e26959a6e86b62ab0a8f1a6e1c Mon Sep 17 00:00:00 2001 From: Alex Light Date: Tue, 13 Aug 2019 10:50:38 -0700 Subject: Basic structural redefinition support This adds basic support for adding methods and fields to already loaded classes using redefinition. This 'structural class redefinition' is currently limited to classes without any virtual methods or instance fields. One cannot currently structurally redefine multiple classes at once nor will structural redefinition trigger the standard redefinition events. After structural redefinition all references to the old class, and its fields and methods are atomically updated. Any memory associated with the static fields of the old class is zeroed. Offsets for field access might change. If there are any active stack frames for methods from the redefined class the original (obsolete method) code will continue to execute. The identity hash code of the redefined class will not change. Any locks being held, waited or blocked on by the old class will be transferred to the new class. To use this feature the process must be debuggable and running with -Xopaque-jni-ids:true. For device testing use a wrap.sh that adds the following flags: '-Xopaque-jni-ids:true -Xcompiler-option --debuggable -XjdwpProvider:adbconnection' Structural redefinition only available using the "com.android.art.UNSAFE.class.structurally_redefine_class_direct" extension. This will not trigger the normal class-redefinition events. Only one class may be redefined at a time. NB There are still some holes in this potentially allowing obsolete methods/fields to be visible. Most notably during jni-id, MethodHandle and VarHandle creation as well as potentially other places in the runtime. These holes will be closed by later CLs. Until then the extension to access structural class redefinition will remain tagged as UNSAFE. Test: ./test.py --host --all-compiler Bug: 134162467 Change-Id: I825d3a4bdb9594c0147223ae69f433ce9bbfc307 --- runtime/mirror/class_ext-inl.h | 66 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'runtime/mirror/class_ext-inl.h') diff --git a/runtime/mirror/class_ext-inl.h b/runtime/mirror/class_ext-inl.h index ead02eed3b..fd81a2a3ed 100644 --- a/runtime/mirror/class_ext-inl.h +++ b/runtime/mirror/class_ext-inl.h @@ -21,8 +21,11 @@ #include "array-inl.h" #include "art_method-inl.h" +#include "base/enums.h" #include "handle_scope.h" +#include "mirror/object.h" #include "object-inl.h" +#include "verify_object.h" namespace art { namespace mirror { @@ -88,6 +91,12 @@ inline ObjPtr ClassExt::GetStaticJFieldIDs() { OFFSET_OF_OBJECT_MEMBER(ClassExt, static_jfield_ids_)); } +template +inline ObjPtr ClassExt::GetObsoleteClass() { + return GetFieldObject( + OFFSET_OF_OBJECT_MEMBER(ClassExt, obsolete_class_)); +} + template inline ObjPtr ClassExt::GetJMethodIDs() { return GetFieldObject( @@ -116,15 +125,58 @@ inline ObjPtr ClassExt::GetOriginalDexFile() { template void ClassExt::VisitNativeRoots(Visitor& visitor, PointerSize pointer_size) { + VisitMethods([&](ArtMethod* method) { + method->VisitRoots(visitor, pointer_size); + }, pointer_size); +} + +template +void ClassExt::VisitMethods(Visitor visitor, PointerSize pointer_size) { ObjPtr arr(GetObsoleteMethods()); - if (arr.IsNull()) { - return; + if (!arr.IsNull()) { + int32_t len = arr->GetLength(); + for (int32_t i = 0; i < len; i++) { + ArtMethod* method = arr->GetElementPtrSize(i, pointer_size); + if (method != nullptr) { + visitor(method); + } + } + } +} + +template +void ClassExt::VisitJMethodIDs(Visitor v) { + ObjPtr marr(GetJMethodIDs()); + if (!marr.IsNull()) { + int32_t len = marr->GetLength(); + for (int32_t i = 0; i < len; i++) { + jmethodID id = marr->GetElementPtrSize(i, kRuntimePointerSize); + if (id != nullptr) { + v(id, i); + } + } + } +} +template +void ClassExt::VisitJFieldIDs(Visitor v) { + ObjPtr sarr(GetStaticJFieldIDs()); + if (!sarr.IsNull()) { + int32_t len = sarr->GetLength(); + for (int32_t i = 0; i < len; i++) { + jfieldID id = sarr->GetElementPtrSize(i, kRuntimePointerSize); + if (id != nullptr) { + v(id, i, true); + } + } } - int32_t len = arr->GetLength(); - for (int32_t i = 0; i < len; i++) { - ArtMethod* method = arr->GetElementPtrSize(i, pointer_size); - if (method != nullptr) { - method->VisitRoots(visitor, pointer_size); + ObjPtr iarr(GetInstanceJFieldIDs()); + if (!iarr.IsNull()) { + int32_t len = iarr->GetLength(); + for (int32_t i = 0; i < len; i++) { + jfieldID id = iarr->GetElementPtrSize(i, kRuntimePointerSize); + if (id != nullptr) { + v(id, i, false); + } } } } -- cgit v1.2.3-59-g8ed1b