From d2fe10a3a34af171bf1631219cd2d6ff6b7778b5 Mon Sep 17 00:00:00 2001 From: Sebastien Hertz Date: Wed, 15 Jan 2014 10:20:56 +0100 Subject: Remove blacklist Removes the class initialization blacklist and use transaction to detect and revert class initialization attempting to invoke native method. This only concerns class initialization happening at compilation time when generating an image (like boot.art for the system). In transactional mode, we log every object's field assignment and array update. Therefore we're able to abort a transaction to restore values of fields and array as they were before the transaction starts. We also log changes to the intern string table so we can restore its state prior to transaction start. Since transactional mode only happens at compilation time, we don't need to log all these changes at runtime. In order to reduce the overhead of testing if transactional mode is on/off, we templatize interfaces of mirror::Object and mirror::Array, respectively responsible for setting a field and setting an array element. For various reasons, we skip some specific fields from transaction: - Object's class and array's length must remain unchanged so garbage collector can compute object's size. - Immutable fields only set during class loading: list of fields, method, dex caches, vtables, ... as all classes have been loaded and verified before a transaction occurs. - Object's monitor for performance reason. Before generating the image, we browse the heap to collect objects that need to be written into it. Since the heap may still holds references to unreachable objects due to aborted transactions, we trigger one collection at the end of the class preinitialization phase. Since the transaction is held by the runtime and all compilation threads share the same runtime, we need to ensure only one compilation thread has exclusive access to the runtime. To workaround this issue, we force class initialization phase to run with only one thread. Note this is only done when generating image so application compilation is not impacted. This issue will be addressed in a separate CL. Bug: 9676614 Change-Id: I221910a9183a5ba6c2b99a277f5a5a68bc69b5f9 --- runtime/runtime.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) (limited to 'runtime/runtime.cc') diff --git a/runtime/runtime.cc b/runtime/runtime.cc index 6ca45e8..e1b0ed4 100644 --- a/runtime/runtime.cc +++ b/runtime/runtime.cc @@ -60,6 +60,7 @@ #include "thread.h" #include "thread_list.h" #include "trace.h" +#include "transaction.h" #include "profiler.h" #include "UniquePtr.h" #include "verifier/method_verifier.h" @@ -120,7 +121,8 @@ Runtime::Runtime() main_thread_group_(nullptr), system_thread_group_(nullptr), system_class_loader_(nullptr), - dump_gc_performance_on_shutdown_(false) { + dump_gc_performance_on_shutdown_(false), + preinitialization_transaction(nullptr) { for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { callee_save_methods_[i] = nullptr; } @@ -826,7 +828,8 @@ jobject CreateSystemClassLoader() { thread_class->FindDeclaredInstanceField("contextClassLoader", "Ljava/lang/ClassLoader;"); CHECK(contextClassLoader != NULL); - contextClassLoader->SetObject(soa.Self()->GetPeer(), class_loader.get()); + // We can't run in a transaction yet. + contextClassLoader->SetObject(soa.Self()->GetPeer(), class_loader.get()); return env->NewGlobalRef(system_class_loader.get()); } @@ -1305,6 +1308,10 @@ void Runtime::VisitConcurrentRoots(RootCallback* callback, void* arg, bool only_ bool clean_dirty) { intern_table_->VisitRoots(callback, arg, only_dirty, clean_dirty); class_linker_->VisitRoots(callback, arg, only_dirty, clean_dirty); + // TODO: is it the right place ? + if (preinitialization_transaction != nullptr) { + preinitialization_transaction->VisitRoots(callback, arg); + } } void Runtime::VisitNonThreadRoots(RootCallback* callback, void* arg) { @@ -1371,7 +1378,7 @@ mirror::ObjectArray* Runtime::CreateDefaultImt(ClassLinker* c SirtRef > imtable(self, cl->AllocArtMethodArray(self, 64)); mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod(); for (size_t i = 0; i < static_cast(imtable->GetLength()); i++) { - imtable->Set(i, imt_conflict_method); + imtable->Set(i, imt_conflict_method); } return imtable.get(); } @@ -1547,4 +1554,74 @@ void Runtime::StartProfiler(const char *appDir, bool startImmediately) { BackgroundMethodSamplingProfiler::Start(profile_period_s_, profile_duration_s_, appDir, profile_interval_us_, profile_backoff_coefficient_, startImmediately); } + +// Transaction support. +// TODO move them to header file for inlining. +bool Runtime::IsActiveTransaction() const { + return preinitialization_transaction != nullptr; +} + +void Runtime::EnterTransactionMode(Transaction* transaction) { + DCHECK(IsCompiler()); + DCHECK(transaction != nullptr); + DCHECK(!IsActiveTransaction()); + preinitialization_transaction = transaction; +} + +void Runtime::ExitTransactionMode() { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction = nullptr; +} + +void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, + uint32_t value, bool is_volatile) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWriteField32(obj, field_offset, value, is_volatile); +} + +void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, + uint64_t value, bool is_volatile) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWriteField64(obj, field_offset, value, is_volatile); +} + +void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset, + mirror::Object* value, bool is_volatile) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWriteFieldReference(obj, field_offset, value, is_volatile); +} + +void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWriteArray(array, index, value); +} + +void Runtime::RecordStrongStringInsertion(mirror::String* s, uint32_t hash_code) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordStrongStringInsertion(s, hash_code); +} + +void Runtime::RecordWeakStringInsertion(mirror::String* s, uint32_t hash_code) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWeakStringInsertion(s, hash_code); +} + +void Runtime::RecordStrongStringRemoval(mirror::String* s, uint32_t hash_code) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordStrongStringRemoval(s, hash_code); +} + +void Runtime::RecordWeakStringRemoval(mirror::String* s, uint32_t hash_code) const { + DCHECK(IsCompiler()); + DCHECK(IsActiveTransaction()); + preinitialization_transaction->RecordWeakStringRemoval(s, hash_code); +} } // namespace art -- cgit v1.1