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