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
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 3382811..5728391 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -398,14 +398,11 @@
     // non-null value. However, because we can run without code
     // available (in the compiler, in tests), we manually assign the
     // fields the constructor should have set.
-    soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->
-        SetBoolean(opeer_, thread_is_daemon);
-    soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->
-        SetObject(opeer_, soa.Decode<mirror::Object*>(thread_group));
-    soa.DecodeField(WellKnownClasses::java_lang_Thread_name)->
-        SetObject(opeer_, soa.Decode<mirror::Object*>(thread_name.get()));
-    soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->
-        SetInt(opeer_, thread_priority);
+    if (runtime->IsActiveTransaction()) {
+      InitPeer<true>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority);
+    } else {
+      InitPeer<false>(soa, thread_is_daemon, thread_group, thread_name.get(), thread_priority);
+    }
     peer_thread_name.reset(GetThreadName(soa));
   }
   // 'thread_name' may have been null, so don't trust 'peer_thread_name' to be non-null.
@@ -414,6 +411,19 @@
   }
 }
 
+template<bool kTransactionActive>
+void Thread::InitPeer(ScopedObjectAccess& soa, jboolean thread_is_daemon, jobject thread_group,
+                      jobject thread_name, jint thread_priority) {
+  soa.DecodeField(WellKnownClasses::java_lang_Thread_daemon)->
+      SetBoolean<kTransactionActive>(opeer_, thread_is_daemon);
+  soa.DecodeField(WellKnownClasses::java_lang_Thread_group)->
+      SetObject<kTransactionActive>(opeer_, soa.Decode<mirror::Object*>(thread_group));
+  soa.DecodeField(WellKnownClasses::java_lang_Thread_name)->
+      SetObject<kTransactionActive>(opeer_, soa.Decode<mirror::Object*>(thread_name));
+  soa.DecodeField(WellKnownClasses::java_lang_Thread_priority)->
+      SetInt<kTransactionActive>(opeer_, thread_priority);
+}
+
 void Thread::SetThreadName(const char* name) {
   name_->assign(name);
   ::art::SetThreadName(name);
@@ -1020,7 +1030,11 @@
     RemoveFromThreadGroup(soa);
 
     // this.nativePeer = 0;
-    soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)->SetLong(opeer_, 0);
+    if (Runtime::Current()->IsActiveTransaction()) {
+      soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)->SetLong<true>(opeer_, 0);
+    } else {
+      soa.DecodeField(WellKnownClasses::java_lang_Thread_nativePeer)->SetLong<false>(opeer_, 0);
+    }
     Dbg::PostThreadDeath(self);
 
     // Thread.join() is implemented as an Object.wait() on the Thread.lock object. Signal anyone
@@ -1309,7 +1323,8 @@
     }
     // Save PC trace in last element of method trace, also places it into the
     // object graph.
-    method_trace->Set(depth, dex_pc_trace);
+    // We are called from native: use non-transactional mode.
+    method_trace->Set<false>(depth, dex_pc_trace);
     // Set the Object*s and assert that no thread suspension is now possible.
     const char* last_no_suspend_cause =
         self_->StartAssertNoThreadSuspension("Building internal stack trace");
@@ -1337,8 +1352,14 @@
     if (m->IsRuntimeMethod()) {
       return true;  // Ignore runtime frames (in particular callee save).
     }
-    method_trace_->Set(count_, m);
-    dex_pc_trace_->Set(count_, m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc());
+    // TODO dedup this code.
+    if (Runtime::Current()->IsActiveTransaction()) {
+      method_trace_->Set<true>(count_, m);
+      dex_pc_trace_->Set<true>(count_, m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc());
+    } else {
+      method_trace_->Set<false>(count_, m);
+      dex_pc_trace_->Set<false>(count_, m->IsProxyMethod() ? DexFile::kDexNoIndex : GetDexPc());
+    }
     ++count_;
     return true;
   }
@@ -1461,7 +1482,8 @@
     if (obj == nullptr) {
       return nullptr;
     }
-    soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->Set(i, obj);
+    // We are called from native: use non-transactional mode.
+    soa.Decode<mirror::ObjectArray<mirror::StackTraceElement>*>(result)->Set<false>(i, obj);
   }
   return result;
 }