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