class TransactionTest : public CommonTransactionTest { protected:
TransactionTest() { this->use_boot_image_ = true; // We need the boot image for this test.
}
// Tests failing class initialization due to native call with transaction rollback. void testTransactionAbort(constchar* tested_class_signature) {
ScopedObjectAccess soa(Thread::Current());
jobject jclass_loader = LoadDex("Transaction");
StackHandleScope<2> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
ASSERT_TRUE(class_loader != nullptr);
// Load and initialize java.lang.ExceptionInInitializerError and the exception class used // to abort transaction so they can be thrown during class initialization if the transaction // aborts.
MutableHandle<mirror::Class> h_klass(
hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/ExceptionInInitializerError;")));
ASSERT_TRUE(h_klass != nullptr);
class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
ASSERT_TRUE(h_klass->IsInitialized());
// Check class's monitor get back to its original state without rolling back changes.
LockWord new_lock_word = h_klass->GetLockWord(false);
EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
// Check class status is rolled back properly.
soa.Self()->ClearException();
RollbackAndExitTransactionMode();
ASSERT_EQ(old_status, h_klass->GetStatus());
}
};
// Tests object's class is preserved after transaction rollback.
TEST_F(TransactionTest, Object_class) {
ScopedObjectAccess soa(Thread::Current());
StackHandleScope<2> hs(soa.Self());
Handle<mirror::Class> h_klass(
hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
ASSERT_TRUE(h_klass != nullptr);
EnterTransactionMode();
Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
ASSERT_TRUE(h_obj != nullptr);
ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); // Rolling back transaction's changes must not clear the Object::class field.
RollbackAndExitTransactionMode();
EXPECT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
}
EnterTransactionMode(); // Unlock object's monitor inside the transaction.
h_obj->MonitorExit(soa.Self());
LockWord new_lock_word = h_obj->GetLockWord(false); // Rolling back transaction's changes must not change monitor's state.
RollbackAndExitTransactionMode();
// Check values have properly been restored to their original (default) value.
EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
// Check values have properly been restored to their original (default) value.
EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
}
// Tests static array fields are reset to their default value after transaction rollback.
TEST_F(TransactionTest, StaticArrayFieldsTest) {
ScopedObjectAccess soa(Thread::Current());
StackHandleScope<13> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
ASSERT_TRUE(class_loader != nullptr);
// Go search the dex file to find the string id of our string. staticconstchar* kResolvedString = "ResolvedString"; const dex::StringId* string_id = dex_file->FindStringId(kResolvedString);
ASSERT_TRUE(string_id != nullptr);
dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
ASSERT_TRUE(string_idx.IsValid()); // String should only get resolved by the initializer.
EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr); // Do the transaction, then roll back.
EnterTransactionMode(); bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
ASSERT_TRUE(success);
ASSERT_TRUE(h_klass->IsInitialized()); // Make sure the string got resolved by the transaction.
{
ObjPtr<mirror::String> s =
class_linker_->LookupString(string_idx, h_dex_cache.Get());
ASSERT_TRUE(s != nullptr);
EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx));
}
RollbackAndExitTransactionMode(); // Check that the string did not stay resolved.
EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
ASSERT_FALSE(h_klass->IsInitialized());
ASSERT_FALSE(soa.Self()->IsExceptionPending());
}
class MethodTypeTransactionTest : public TransactionTest { protected:
MethodTypeTransactionTest() { // java.lang.invoke.MethodType factory methods and mirror::MethodType::Create // are backed by the same cache, which is in the primary boot image. As as a // result, MethodType creation can lead to writes to the map under a // transaction, which is forbidden. this->use_boot_image_ = false;
}
};
// Tests rolling back resolved method types in dex cache.
TEST_F(MethodTypeTransactionTest, ResolveMethodType) {
ScopedObjectAccess soa(Thread::Current());
StackHandleScope<3> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
ASSERT_TRUE(class_loader != nullptr);
// Do the transaction, then roll back.
EnterTransactionMode();
ObjPtr<mirror::MethodType> method_type =
class_linker_->ResolveMethodType(soa.Self(), proto_index, h_dex_cache, class_loader);
ASSERT_TRUE(method_type != nullptr); // Make sure the method type was recorded in the dex cache.
ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == method_type);
RollbackAndExitTransactionMode(); // Check that the method type was removed from the dex cache.
ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == nullptr);
}
// Tests successful class initialization without class initializer.
TEST_F(TransactionTest, EmptyClass) {
ScopedObjectAccess soa(Thread::Current());
StackHandleScope<2> hs(soa.Self());
Handle<mirror::ClassLoader> class_loader(
hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
ASSERT_TRUE(class_loader != nullptr);
// Tests failing class initialization due to native call.
TEST_F(TransactionTest, NativeCallAbortClass) {
testTransactionAbort("LTransaction$NativeCallAbortClass;");
}
// Tests failing class initialization due to native call in a "synchronized" statement // (which must catch any exception, do the monitor-exit then re-throw the caught exception).
TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
}
// Tests failing class initialization due to native call, even if an "all" catch handler // catches the exception thrown when aborting the transaction.
TEST_F(TransactionTest, CatchNativeCallAbortClass) {
testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
}
// Tests failing class initialization with multiple transaction aborts.
TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
}
// Tests failing class initialization due to Class.forName() not finding the class, // even if an "all" catch handler catches the exception thrown when aborting the transaction.
TEST_F(TransactionTest, CatchClassForNameAbortClass) {
testTransactionAbort("LTransaction$CatchClassForNameAbortClass;");
}
// Same as CatchClassForNameAbortClass but the class initializer tries to do the work twice. // This would trigger a DCHECK() if we continued executing bytecode with an aborted transaction.
TEST_F(TransactionTest, CatchClassForNameAbortClassTwice) {
testTransactionAbort("LTransaction$CatchClassForNameAbortClassTwice;");
}
// Tests failing class initialization due to allocating instance of finalizable class.
TEST_F(TransactionTest, FinalizableAbortClass) {
testTransactionAbort("LTransaction$FinalizableAbortClass;");
}
// The `long[].class` should be in the boot image but `long[][][].class` should not. // (We have seen `long[][].class` both present and missing from the boot image, // depending on the libcore code, so we do not use it for this test.)
Handle<mirror::Class> long_array_dim3_class = hs.NewHandle(FindClass("[[[J", class_loader));
ASSERT_TRUE(long_array_dim3_class != nullptr);
ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3_class.Get()));
ASSERT_TRUE(heap->ObjectIsInBootImageSpace(
long_array_dim3_class->GetComponentType()->GetComponentType()));
Handle<mirror::Array> long_array_dim3 = hs.NewHandle(mirror::Array::Alloc(
soa.Self(),
long_array_dim3_class.Get(), /*component_count=*/ 1,
long_array_dim3_class->GetComponentSizeShift(),
heap->GetCurrentAllocator()));
ASSERT_TRUE(long_array_dim3 != nullptr);
ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3.Get()));
Handle<mirror::Array> long_array = hs.NewHandle(mirror::Array::Alloc(
soa.Self(),
long_array_dim3_class->GetComponentType()->GetComponentType(), /*component_count=*/ 1,
long_array_dim3_class->GetComponentType()->GetComponentType()->GetComponentSizeShift(),
heap->GetCurrentAllocator()));
ASSERT_TRUE(long_array != nullptr);
ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array.Get()));
// Use the Array's IfTable as an array from the boot image.
Handle<mirror::ObjectArray<mirror::Object>> array_iftable =
hs.NewHandle(long_array_dim3_class->GetIfTable());
ASSERT_TRUE(array_iftable != nullptr);
ASSERT_TRUE(heap->ObjectIsInBootImageSpace(array_iftable.Get()));
// Test non-strict transaction.
ArenaPool* arena_pool = Runtime::Current()->GetArenaPool();
Transaction transaction( /*strict=*/ false, /*root=*/ nullptr, /*arena_stack=*/ nullptr, arena_pool); // Static field in boot image.
EXPECT_TRUE(transaction.WriteConstraint(boolean_class.Get()));
EXPECT_FALSE(transaction.ReadConstraint(boolean_class.Get())); // Instance field or array element in boot image. // Do not check ReadConstraint(), it expects only static fields (checks for class object).
EXPECT_TRUE(transaction.WriteConstraint(true_value.Get()));
EXPECT_TRUE(transaction.WriteConstraint(array_iftable.Get())); // Static field not in boot image.
EXPECT_FALSE(transaction.WriteConstraint(static_fields_test_class.Get()));
EXPECT_FALSE(transaction.ReadConstraint(static_fields_test_class.Get())); // Instance field or array element not in boot image. // Do not check ReadConstraint(), it expects only static fields (checks for class object).
EXPECT_FALSE(transaction.WriteConstraint(instance_fields_test_object.Get()));
EXPECT_FALSE(transaction.WriteConstraint(long_array_dim3.Get())); // Write value constraints.
EXPECT_FALSE(transaction.WriteValueConstraint(static_fields_test_class.Get()));
EXPECT_FALSE(transaction.WriteValueConstraint(instance_fields_test_object.Get()));
EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3->GetClass()));
EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3.Get()));
EXPECT_FALSE(transaction.WriteValueConstraint(long_array->GetClass()));
EXPECT_FALSE(transaction.WriteValueConstraint(long_array.Get()));
// Test strict transaction.
Transaction strict_transaction( /*strict=*/ true, /*root=*/ static_field_class.Get(), /*arena_stack=*/ nullptr, arena_pool); // Static field in boot image.
EXPECT_TRUE(strict_transaction.WriteConstraint(boolean_class.Get()));
EXPECT_TRUE(strict_transaction.ReadConstraint(boolean_class.Get())); // Instance field or array element in boot image. // Do not check ReadConstraint(), it expects only static fields (checks for class object).
EXPECT_TRUE(strict_transaction.WriteConstraint(true_value.Get()));
EXPECT_TRUE(strict_transaction.WriteConstraint(array_iftable.Get())); // Static field in another class not in boot image.
EXPECT_TRUE(strict_transaction.WriteConstraint(static_fields_test_class.Get()));
EXPECT_TRUE(strict_transaction.ReadConstraint(static_fields_test_class.Get())); // Instance field or array element not in boot image. // Do not check ReadConstraint(), it expects only static fields (checks for class object).
EXPECT_FALSE(strict_transaction.WriteConstraint(instance_fields_test_object.Get()));
EXPECT_FALSE(strict_transaction.WriteConstraint(long_array_dim3.Get())); // Static field in the same class.
EXPECT_FALSE(strict_transaction.WriteConstraint(static_field_class.Get()));
EXPECT_FALSE(strict_transaction.ReadConstraint(static_field_class.Get())); // Write value constraints.
EXPECT_FALSE(strict_transaction.WriteValueConstraint(static_fields_test_class.Get()));
EXPECT_FALSE(strict_transaction.WriteValueConstraint(instance_fields_test_object.Get())); // TODO: The following may be revised, see a TODO in Transaction::WriteValueConstraint().
EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3->GetClass()));
EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3.Get()));
EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array->GetClass()));
EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array.Get()));
}
} // namespace art
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.