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