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