Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "transaction.h" |
| 18 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "common_runtime_test.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "dex/dex_file.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 24 | #include "mirror/array-alloc-inl.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 25 | #include "mirror/class-alloc-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change-inl.h" |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 30 | class TransactionTest : public CommonRuntimeTest { |
| 31 | public: |
| 32 | // Tests failing class initialization due to native call with transaction rollback. |
| 33 | void testTransactionAbort(const char* tested_class_signature) { |
| 34 | ScopedObjectAccess soa(Thread::Current()); |
| 35 | jobject jclass_loader = LoadDex("Transaction"); |
| 36 | StackHandleScope<2> hs(soa.Self()); |
| 37 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 38 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 39 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 40 | |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 41 | // Load and initialize java.lang.ExceptionInInitializerError and the exception class used |
| 42 | // to abort transaction so they can be thrown during class initialization if the transaction |
| 43 | // aborts. |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 44 | MutableHandle<mirror::Class> h_klass( |
| 45 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), |
| 46 | "Ljava/lang/ExceptionInInitializerError;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 47 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 48 | class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 49 | ASSERT_TRUE(h_klass->IsInitialized()); |
| 50 | |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 51 | h_klass.Assign(class_linker_->FindSystemClass(soa.Self(), |
| 52 | Transaction::kAbortExceptionSignature)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 53 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 54 | class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 55 | ASSERT_TRUE(h_klass->IsInitialized()); |
| 56 | |
| 57 | // Load and verify utility class. |
| 58 | h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;", |
| 59 | class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 60 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 61 | class_linker_->VerifyClass(soa.Self(), h_klass); |
| 62 | ASSERT_TRUE(h_klass->IsVerified()); |
| 63 | |
| 64 | // Load and verify tested class. |
| 65 | h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 66 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 67 | class_linker_->VerifyClass(soa.Self(), h_klass); |
| 68 | ASSERT_TRUE(h_klass->IsVerified()); |
| 69 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 70 | ClassStatus old_status = h_klass->GetStatus(); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 71 | LockWord old_lock_word = h_klass->GetLockWord(false); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 72 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 73 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 74 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 75 | ASSERT_TRUE(Runtime::Current()->IsTransactionAborted()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 76 | ASSERT_FALSE(success); |
| 77 | ASSERT_TRUE(h_klass->IsErroneous()); |
| 78 | ASSERT_TRUE(soa.Self()->IsExceptionPending()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 79 | |
| 80 | // Check class's monitor get back to its original state without rolling back changes. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 81 | LockWord new_lock_word = h_klass->GetLockWord(false); |
| 82 | EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word)); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 83 | |
| 84 | // Check class status is rolled back properly. |
| 85 | soa.Self()->ClearException(); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 86 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 87 | ASSERT_EQ(old_status, h_klass->GetStatus()); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // Tests object's class is preserved after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 92 | TEST_F(TransactionTest, Object_class) { |
| 93 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 94 | StackHandleScope<2> hs(soa.Self()); |
| 95 | Handle<mirror::Class> h_klass( |
| 96 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 97 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 98 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 99 | Runtime::Current()->EnterTransactionMode(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 100 | Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 101 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 102 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 103 | // Rolling back transaction's changes must not clear the Object::class field. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 104 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 105 | EXPECT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 108 | // Tests object's monitor state is preserved after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 109 | TEST_F(TransactionTest, Object_monitor) { |
| 110 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 111 | StackHandleScope<2> hs(soa.Self()); |
| 112 | Handle<mirror::Class> h_klass( |
| 113 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 114 | ASSERT_TRUE(h_klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 115 | Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 116 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 117 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 118 | |
| 119 | // Lock object's monitor outside the transaction. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 120 | h_obj->MonitorEnter(soa.Self()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 121 | LockWord old_lock_word = h_obj->GetLockWord(false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 122 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 123 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 124 | // Unlock object's monitor inside the transaction. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 125 | h_obj->MonitorExit(soa.Self()); |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 126 | LockWord new_lock_word = h_obj->GetLockWord(false); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 127 | // Rolling back transaction's changes must not change monitor's state. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 128 | Runtime::Current()->RollbackAndExitTransactionMode(); |
| 129 | |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 130 | LockWord aborted_lock_word = h_obj->GetLockWord(false); |
| 131 | EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word)); |
| 132 | EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 135 | // Tests array's length is preserved after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 136 | TEST_F(TransactionTest, Array_length) { |
| 137 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 138 | StackHandleScope<2> hs(soa.Self()); |
| 139 | Handle<mirror::Class> h_klass( |
| 140 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 141 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | |
| 143 | constexpr int32_t kArraySize = 2; |
| 144 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 145 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 146 | |
| 147 | // Allocate an array during transaction. |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame^] | 148 | Handle<mirror::Array> h_obj = hs.NewHandle( |
| 149 | mirror::Array::Alloc(soa.Self(), |
| 150 | h_klass.Get(), |
| 151 | kArraySize, |
| 152 | h_klass->GetComponentSizeShift(), |
| 153 | Runtime::Current()->GetHeap()->GetCurrentAllocator())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 154 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 155 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 156 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 157 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 158 | // Rolling back transaction's changes must not reset array's length. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 159 | EXPECT_EQ(h_obj->GetLength(), kArraySize); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 162 | // Tests static fields are reset to their default value after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 163 | TEST_F(TransactionTest, StaticFieldsTest) { |
| 164 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 165 | StackHandleScope<4> hs(soa.Self()); |
| 166 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 167 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 168 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 169 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 170 | Handle<mirror::Class> h_klass( |
| 171 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 172 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 173 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 174 | ASSERT_TRUE(success); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 175 | ASSERT_TRUE(h_klass->IsInitialized()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 176 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 177 | |
| 178 | // Lookup fields. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 179 | ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 180 | ASSERT_TRUE(booleanField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 181 | ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 182 | ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 183 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 184 | ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 185 | ASSERT_TRUE(byteField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 186 | ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 187 | ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 188 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 189 | ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 190 | ASSERT_TRUE(charField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 191 | ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 192 | ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 193 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 194 | ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 195 | ASSERT_TRUE(shortField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 196 | ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 197 | ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 198 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 199 | ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 200 | ASSERT_TRUE(intField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 201 | ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 202 | ASSERT_EQ(intField->GetInt(h_klass.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 203 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 204 | ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 205 | ASSERT_TRUE(longField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 206 | ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 207 | ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 208 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 209 | ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 210 | ASSERT_TRUE(floatField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 211 | ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 212 | ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 213 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 214 | ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 215 | ASSERT_TRUE(doubleField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 216 | ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 217 | ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 218 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 219 | ArtField* objectField = h_klass->FindDeclaredStaticField("objectField", |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 220 | "Ljava/lang/Object;"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 221 | ASSERT_TRUE(objectField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 222 | ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 223 | ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 224 | |
| 225 | // Create a java.lang.Object instance to set objectField. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 226 | Handle<mirror::Class> object_klass( |
| 227 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 228 | ASSERT_TRUE(object_klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 229 | Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 230 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 231 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 232 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 233 | // Modify fields inside transaction then rollback changes. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 234 | Runtime::Current()->EnterTransactionMode(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 235 | booleanField->SetBoolean<true>(h_klass.Get(), true); |
| 236 | byteField->SetByte<true>(h_klass.Get(), 1); |
| 237 | charField->SetChar<true>(h_klass.Get(), 1u); |
| 238 | shortField->SetShort<true>(h_klass.Get(), 1); |
| 239 | intField->SetInt<true>(h_klass.Get(), 1); |
| 240 | longField->SetLong<true>(h_klass.Get(), 1); |
| 241 | floatField->SetFloat<true>(h_klass.Get(), 1.0); |
| 242 | doubleField->SetDouble<true>(h_klass.Get(), 1.0); |
| 243 | objectField->SetObject<true>(h_klass.Get(), h_obj.Get()); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 244 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 245 | |
| 246 | // Check values have properly been restored to their original (default) value. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 247 | EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false); |
| 248 | EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0); |
| 249 | EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u); |
| 250 | EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0); |
| 251 | EXPECT_EQ(intField->GetInt(h_klass.Get()), 0); |
| 252 | EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0)); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 253 | EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f)); |
| 254 | EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 255 | EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 258 | // Tests instance fields are reset to their default value after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 259 | TEST_F(TransactionTest, InstanceFieldsTest) { |
| 260 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 261 | StackHandleScope<5> hs(soa.Self()); |
| 262 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 263 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 264 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 265 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 266 | Handle<mirror::Class> h_klass( |
| 267 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 268 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 269 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 270 | ASSERT_TRUE(success); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 271 | ASSERT_TRUE(h_klass->IsInitialized()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 272 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 273 | |
| 274 | // Allocate an InstanceFieldTest object. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 275 | Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 276 | ASSERT_TRUE(h_instance != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 277 | |
| 278 | // Lookup fields. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 279 | ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 280 | ASSERT_TRUE(booleanField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 281 | ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 282 | ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 283 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 284 | ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 285 | ASSERT_TRUE(byteField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 286 | ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 287 | ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 288 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 289 | ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 290 | ASSERT_TRUE(charField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 291 | ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 292 | ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 293 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 294 | ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 295 | ASSERT_TRUE(shortField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 296 | ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 297 | ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 298 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 299 | ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 300 | ASSERT_TRUE(intField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 301 | ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 302 | ASSERT_EQ(intField->GetInt(h_instance.Get()), 0); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 303 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 304 | ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 305 | ASSERT_TRUE(longField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 306 | ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 307 | ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 308 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 309 | ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 310 | ASSERT_TRUE(floatField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 311 | ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 312 | ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 313 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 314 | ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 315 | ASSERT_TRUE(doubleField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 316 | ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 317 | ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 318 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 319 | ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField", |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 320 | "Ljava/lang/Object;"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 321 | ASSERT_TRUE(objectField != nullptr); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 322 | ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 323 | ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 324 | |
| 325 | // Create a java.lang.Object instance to set objectField. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 326 | Handle<mirror::Class> object_klass( |
| 327 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 328 | ASSERT_TRUE(object_klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 329 | Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 330 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 331 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 332 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 333 | // Modify fields inside transaction then rollback changes. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 334 | Runtime::Current()->EnterTransactionMode(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 335 | booleanField->SetBoolean<true>(h_instance.Get(), true); |
| 336 | byteField->SetByte<true>(h_instance.Get(), 1); |
| 337 | charField->SetChar<true>(h_instance.Get(), 1u); |
| 338 | shortField->SetShort<true>(h_instance.Get(), 1); |
| 339 | intField->SetInt<true>(h_instance.Get(), 1); |
| 340 | longField->SetLong<true>(h_instance.Get(), 1); |
| 341 | floatField->SetFloat<true>(h_instance.Get(), 1.0); |
| 342 | doubleField->SetDouble<true>(h_instance.Get(), 1.0); |
| 343 | objectField->SetObject<true>(h_instance.Get(), h_obj.Get()); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 344 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 345 | |
| 346 | // Check values have properly been restored to their original (default) value. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 347 | EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false); |
| 348 | EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0); |
| 349 | EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u); |
| 350 | EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0); |
| 351 | EXPECT_EQ(intField->GetInt(h_instance.Get()), 0); |
| 352 | EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0)); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 353 | EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f)); |
| 354 | EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 355 | EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 358 | // Tests static array fields are reset to their default value after transaction rollback. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 359 | TEST_F(TransactionTest, StaticArrayFieldsTest) { |
| 360 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 361 | StackHandleScope<13> hs(soa.Self()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 362 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 363 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 364 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 365 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 366 | Handle<mirror::Class> h_klass( |
| 367 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 368 | ASSERT_TRUE(h_klass != nullptr); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 369 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 370 | ASSERT_TRUE(success); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 371 | ASSERT_TRUE(h_klass->IsInitialized()); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 372 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 373 | |
| 374 | // Lookup fields. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 375 | ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 376 | ASSERT_TRUE(booleanArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 377 | Handle<mirror::BooleanArray> booleanArray = hs.NewHandle( |
| 378 | booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 379 | ASSERT_TRUE(booleanArray != nullptr); |
| 380 | ASSERT_EQ(booleanArray->GetLength(), 1); |
| 381 | ASSERT_EQ(booleanArray->GetWithoutChecks(0), false); |
| 382 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 383 | ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 384 | ASSERT_TRUE(byteArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 385 | Handle<mirror::ByteArray> byteArray = |
| 386 | hs.NewHandle(byteArrayField->GetObject(h_klass.Get())->AsByteArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 387 | ASSERT_TRUE(byteArray != nullptr); |
| 388 | ASSERT_EQ(byteArray->GetLength(), 1); |
| 389 | ASSERT_EQ(byteArray->GetWithoutChecks(0), 0); |
| 390 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 391 | ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 392 | ASSERT_TRUE(charArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 393 | Handle<mirror::CharArray> charArray = |
| 394 | hs.NewHandle(charArrayField->GetObject(h_klass.Get())->AsCharArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 395 | ASSERT_TRUE(charArray != nullptr); |
| 396 | ASSERT_EQ(charArray->GetLength(), 1); |
| 397 | ASSERT_EQ(charArray->GetWithoutChecks(0), 0u); |
| 398 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 399 | ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 400 | ASSERT_TRUE(shortArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 401 | Handle<mirror::ShortArray> shortArray = |
| 402 | hs.NewHandle(shortArrayField->GetObject(h_klass.Get())->AsShortArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 403 | ASSERT_TRUE(shortArray != nullptr); |
| 404 | ASSERT_EQ(shortArray->GetLength(), 1); |
| 405 | ASSERT_EQ(shortArray->GetWithoutChecks(0), 0); |
| 406 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 407 | ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 408 | ASSERT_TRUE(intArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 409 | Handle<mirror::IntArray> intArray = |
| 410 | hs.NewHandle(intArrayField->GetObject(h_klass.Get())->AsIntArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 411 | ASSERT_TRUE(intArray != nullptr); |
| 412 | ASSERT_EQ(intArray->GetLength(), 1); |
| 413 | ASSERT_EQ(intArray->GetWithoutChecks(0), 0); |
| 414 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 415 | ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 416 | ASSERT_TRUE(longArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 417 | Handle<mirror::LongArray> longArray = |
| 418 | hs.NewHandle(longArrayField->GetObject(h_klass.Get())->AsLongArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 419 | ASSERT_TRUE(longArray != nullptr); |
| 420 | ASSERT_EQ(longArray->GetLength(), 1); |
| 421 | ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0)); |
| 422 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 423 | ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 424 | ASSERT_TRUE(floatArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 425 | Handle<mirror::FloatArray> floatArray = |
| 426 | hs.NewHandle(floatArrayField->GetObject(h_klass.Get())->AsFloatArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 427 | ASSERT_TRUE(floatArray != nullptr); |
| 428 | ASSERT_EQ(floatArray->GetLength(), 1); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 429 | ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 430 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 431 | ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 432 | ASSERT_TRUE(doubleArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 433 | Handle<mirror::DoubleArray> doubleArray = |
| 434 | hs.NewHandle(doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 435 | ASSERT_TRUE(doubleArray != nullptr); |
| 436 | ASSERT_EQ(doubleArray->GetLength(), 1); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 437 | ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 438 | |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 439 | ArtField* objectArrayField = |
| 440 | h_klass->FindDeclaredStaticField("objectArrayField", "[Ljava/lang/Object;"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 441 | ASSERT_TRUE(objectArrayField != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 442 | Handle<mirror::ObjectArray<mirror::Object>> objectArray = |
| 443 | hs.NewHandle(objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 444 | ASSERT_TRUE(objectArray != nullptr); |
| 445 | ASSERT_EQ(objectArray->GetLength(), 1); |
| 446 | ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr); |
| 447 | |
| 448 | // Create a java.lang.Object instance to set objectField. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 449 | Handle<mirror::Class> object_klass( |
| 450 | hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 451 | ASSERT_TRUE(object_klass != nullptr); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 452 | Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 453 | ASSERT_TRUE(h_obj != nullptr); |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 454 | ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 455 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 456 | // Modify fields inside transaction then rollback changes. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 457 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 458 | booleanArray->SetWithoutChecks<true>(0, true); |
| 459 | byteArray->SetWithoutChecks<true>(0, 1); |
| 460 | charArray->SetWithoutChecks<true>(0, 1u); |
| 461 | shortArray->SetWithoutChecks<true>(0, 1); |
| 462 | intArray->SetWithoutChecks<true>(0, 1); |
| 463 | longArray->SetWithoutChecks<true>(0, 1); |
| 464 | floatArray->SetWithoutChecks<true>(0, 1.0); |
| 465 | doubleArray->SetWithoutChecks<true>(0, 1.0); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 466 | objectArray->SetWithoutChecks<true>(0, h_obj.Get()); |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 467 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 468 | |
| 469 | // Check values have properly been restored to their original (default) value. |
| 470 | EXPECT_EQ(booleanArray->GetWithoutChecks(0), false); |
| 471 | EXPECT_EQ(byteArray->GetWithoutChecks(0), 0); |
| 472 | EXPECT_EQ(charArray->GetWithoutChecks(0), 0u); |
| 473 | EXPECT_EQ(shortArray->GetWithoutChecks(0), 0); |
| 474 | EXPECT_EQ(intArray->GetWithoutChecks(0), 0); |
| 475 | EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0)); |
Ian Rogers | 647b1a8 | 2014-10-10 11:02:11 -0700 | [diff] [blame] | 476 | EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f)); |
| 477 | EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 478 | EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr); |
| 479 | } |
| 480 | |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 481 | // Tests rolling back interned strings and resolved strings. |
| 482 | TEST_F(TransactionTest, ResolveString) { |
| 483 | ScopedObjectAccess soa(Thread::Current()); |
| 484 | StackHandleScope<3> hs(soa.Self()); |
| 485 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 486 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 487 | ASSERT_TRUE(class_loader != nullptr); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 488 | |
| 489 | Handle<mirror::Class> h_klass( |
| 490 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;", |
| 491 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 492 | ASSERT_TRUE(h_klass != nullptr); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 493 | |
| 494 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 495 | ASSERT_TRUE(h_dex_cache != nullptr); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 496 | const DexFile* const dex_file = h_dex_cache->GetDexFile(); |
| 497 | ASSERT_TRUE(dex_file != nullptr); |
| 498 | |
| 499 | // Go search the dex file to find the string id of our string. |
| 500 | static const char* kResolvedString = "ResolvedString"; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 501 | const dex::StringId* string_id = dex_file->FindStringId(kResolvedString); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 502 | ASSERT_TRUE(string_id != nullptr); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 503 | dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id); |
| 504 | ASSERT_TRUE(string_idx.IsValid()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 505 | // String should only get resolved by the initializer. |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 506 | EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 507 | EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr); |
| 508 | // Do the transaction, then roll back. |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 509 | Runtime::Current()->EnterTransactionMode(); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 510 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
| 511 | ASSERT_TRUE(success); |
| 512 | ASSERT_TRUE(h_klass->IsInitialized()); |
| 513 | // Make sure the string got resolved by the transaction. |
| 514 | { |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 515 | ObjPtr<mirror::String> s = |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 516 | class_linker_->LookupString(string_idx, h_dex_cache.Get()); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 517 | ASSERT_TRUE(s != nullptr); |
| 518 | EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 519 | EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx)); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 520 | } |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 521 | Runtime::Current()->RollbackAndExitTransactionMode(); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 522 | // Check that the string did not stay resolved. |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 523 | EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr); |
Mathieu Chartier | bb816d6 | 2016-09-07 10:17:46 -0700 | [diff] [blame] | 524 | EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr); |
| 525 | ASSERT_FALSE(h_klass->IsInitialized()); |
| 526 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
| 527 | } |
| 528 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 529 | // Tests successful class initialization without class initializer. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 530 | TEST_F(TransactionTest, EmptyClass) { |
| 531 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 532 | StackHandleScope<2> hs(soa.Self()); |
| 533 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 534 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 535 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 536 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 537 | Handle<mirror::Class> h_klass( |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 538 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;", |
| 539 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 540 | ASSERT_TRUE(h_klass != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 541 | class_linker_->VerifyClass(soa.Self(), h_klass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 542 | ASSERT_TRUE(h_klass->IsVerified()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 543 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 544 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 545 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 546 | Runtime::Current()->ExitTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 547 | ASSERT_TRUE(success); |
| 548 | ASSERT_TRUE(h_klass->IsInitialized()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 549 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
| 550 | } |
| 551 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 552 | // Tests successful class initialization with class initializer. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 553 | TEST_F(TransactionTest, StaticFieldClass) { |
| 554 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 555 | StackHandleScope<2> hs(soa.Self()); |
| 556 | Handle<mirror::ClassLoader> class_loader( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 557 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction")))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 558 | ASSERT_TRUE(class_loader != nullptr); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 559 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 560 | Handle<mirror::Class> h_klass( |
| 561 | hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;", |
| 562 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 563 | ASSERT_TRUE(h_klass != nullptr); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 564 | class_linker_->VerifyClass(soa.Self(), h_klass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 565 | ASSERT_TRUE(h_klass->IsVerified()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 566 | |
Chang Xing | 16d1dd8 | 2017-07-20 17:56:26 -0700 | [diff] [blame] | 567 | Runtime::Current()->EnterTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 568 | bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 569 | Runtime::Current()->ExitTransactionMode(); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 570 | ASSERT_TRUE(success); |
| 571 | ASSERT_TRUE(h_klass->IsInitialized()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 572 | ASSERT_FALSE(soa.Self()->IsExceptionPending()); |
| 573 | } |
| 574 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 575 | // Tests failing class initialization due to native call. |
| 576 | TEST_F(TransactionTest, NativeCallAbortClass) { |
| 577 | testTransactionAbort("LTransaction$NativeCallAbortClass;"); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 578 | } |
| 579 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 580 | // Tests failing class initialization due to native call in a "synchronized" statement |
| 581 | // (which must catch any exception, do the monitor-exit then re-throw the caught exception). |
| 582 | TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) { |
| 583 | testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;"); |
| 584 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 585 | |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 586 | // Tests failing class initialization due to native call, even if an "all" catch handler |
| 587 | // catches the exception thrown when aborting the transaction. |
| 588 | TEST_F(TransactionTest, CatchNativeCallAbortClass) { |
| 589 | testTransactionAbort("LTransaction$CatchNativeCallAbortClass;"); |
| 590 | } |
| 591 | |
| 592 | // Tests failing class initialization with multiple transaction aborts. |
| 593 | TEST_F(TransactionTest, MultipleNativeCallAbortClass) { |
| 594 | testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;"); |
| 595 | } |
| 596 | |
| 597 | // Tests failing class initialization due to allocating instance of finalizable class. |
| 598 | TEST_F(TransactionTest, FinalizableAbortClass) { |
| 599 | testTransactionAbort("LTransaction$FinalizableAbortClass;"); |
| 600 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 601 | } // namespace art |