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