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/interpreter/interpreter.cc b/runtime/interpreter/interpreter.cc
index c6faf44..a674571 100644
--- a/runtime/interpreter/interpreter.cc
+++ b/runtime/interpreter/interpreter.cc
@@ -15,6 +15,7 @@
*/
#include "interpreter_common.h"
+#include <limits>
namespace art {
namespace interpreter {
@@ -23,6 +24,10 @@
static void UnstartedRuntimeJni(Thread* self, ArtMethod* method,
Object* receiver, uint32_t* args, JValue* result)
SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+ DCHECK(Runtime::Current()->IsActiveTransaction()) << "Calling native method "
+ << PrettyMethod(method)
+ << " in unstarted runtime should only happen"
+ << " in a transaction";
std::string name(PrettyMethod(method));
if (name == "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()") {
result->SetL(NULL);
@@ -73,13 +78,18 @@
jint newValue = args[4];
byte* raw_addr = reinterpret_cast<byte*>(obj) + offset;
volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr);
+ // Check offset is 32bits to fit in MemberOffset.
+ CHECK_GE(offset, static_cast<jlong>(std::numeric_limits<int32_t>::min()));
+ CHECK_LE(offset, static_cast<jlong>(std::numeric_limits<int32_t>::max()));
+ Runtime::Current()->RecordWriteField32(obj, MemberOffset(offset), *address, true);
// Note: android_atomic_release_cas() returns 0 on success, not failure.
int r = android_atomic_release_cas(expectedValue, newValue, address);
result->SetZ(r == 0);
} else if (name == "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)") {
Object* obj = reinterpret_cast<Object*>(args[0]);
Object* newValue = reinterpret_cast<Object*>(args[3]);
- obj->SetFieldObject(MemberOffset((static_cast<uint64_t>(args[2]) << 32) | args[1]), newValue, false);
+ obj->SetFieldObject<true>(MemberOffset((static_cast<uint64_t>(args[2]) << 32) | args[1]),
+ newValue, false);
} else if (name == "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)") {
mirror::Class* component = reinterpret_cast<Object*>(args[0])->AsClass();
Primitive::Type primitive_type = component->GetPrimitiveType();
@@ -89,7 +99,11 @@
Primitive::Type primitive_type = component->GetPrimitiveType();
result->SetI(Primitive::ComponentSize(primitive_type));
} else {
- LOG(FATAL) << "Attempt to invoke native method in non-started runtime: " << name;
+ // Throw an exception so we can abort the transaction and undo every change.
+ ThrowLocation throw_location;
+ self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
+ "Attempt to invoke native method in non-started runtime: %s",
+ name.c_str());
}
}
@@ -293,21 +307,38 @@
DCHECK(!shadow_frame.GetMethod()->IsAbstract());
DCHECK(!shadow_frame.GetMethod()->IsNative());
+ bool transaction_active = Runtime::Current()->IsActiveTransaction();
if (LIKELY(shadow_frame.GetMethod()->IsPreverified())) {
// Enter the "without access check" interpreter.
if (kInterpreterImplKind == kSwitchImpl) {
- return ExecuteSwitchImpl<false>(self, mh, code_item, shadow_frame, result_register);
+ if (transaction_active) {
+ return ExecuteSwitchImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
+ } else {
+ return ExecuteSwitchImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
+ }
} else {
DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
- return ExecuteGotoImpl<false>(self, mh, code_item, shadow_frame, result_register);
+ if (transaction_active) {
+ return ExecuteGotoImpl<false, true>(self, mh, code_item, shadow_frame, result_register);
+ } else {
+ return ExecuteGotoImpl<false, false>(self, mh, code_item, shadow_frame, result_register);
+ }
}
} else {
// Enter the "with access check" interpreter.
if (kInterpreterImplKind == kSwitchImpl) {
- return ExecuteSwitchImpl<true>(self, mh, code_item, shadow_frame, result_register);
+ if (transaction_active) {
+ return ExecuteSwitchImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
+ } else {
+ return ExecuteSwitchImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
+ }
} else {
DCHECK_EQ(kInterpreterImplKind, kComputedGotoImplKind);
- return ExecuteGotoImpl<true>(self, mh, code_item, shadow_frame, result_register);
+ if (transaction_active) {
+ return ExecuteGotoImpl<true, true>(self, mh, code_item, shadow_frame, result_register);
+ } else {
+ return ExecuteGotoImpl<true, false>(self, mh, code_item, shadow_frame, result_register);
+ }
}
}
}