blob: 5db51c8e93a287a9da43a1d40d882faf637c63e8 [file] [log] [blame]
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01001/*
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 Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "transaction.h"
18
19#include "common_runtime_test.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010020#include "mirror/array-inl.h"
21#include "mirror/art_field-inl.h"
22#include "mirror/art_method-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "scoped_thread_state_change.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010024
25namespace art {
26
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010027class 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 Hertzd2fe10a2014-01-15 10:20:56 +010037
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010038 // 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();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080066 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010067
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.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080078 LockWord new_lock_word = h_klass->GetLockWord(false);
79 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010080
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 Hertzd2fe10a2014-01-15 10:20:56 +010089TEST_F(TransactionTest, Object_class) {
90 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 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 Hertzd2fe10a2014-01-15 10:20:56 +010095
96 Transaction transaction;
97 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070098 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 Hertzd2fe10a2014-01-15 10:20:56 +0100101 Runtime::Current()->ExitTransactionMode();
102
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100103 // Rolling back transaction's changes must not clear the Object::class field.
104 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105 EXPECT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100106}
107
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100108// Tests object's monitor state is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109TEST_F(TransactionTest, Object_monitor) {
110 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 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 Hertzd2fe10a2014-01-15 10:20:56 +0100118
119 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 h_obj->MonitorEnter(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800121 LockWord old_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122
123 Transaction transaction;
124 Runtime::Current()->EnterTransactionMode(&transaction);
125 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 h_obj->MonitorExit(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800127 LockWord new_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128 Runtime::Current()->ExitTransactionMode();
129
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100130 // Rolling back transaction's changes must not change monitor's state.
131 transaction.Rollback();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800132 LockWord aborted_lock_word = h_obj->GetLockWord(false);
133 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
134 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135}
136
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100137// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138TEST_F(TransactionTest, Array_length) {
139 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 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 Hertzd2fe10a2014-01-15 10:20:56 +0100144
145 constexpr int32_t kArraySize = 2;
146
147 Transaction transaction;
148 Runtime::Current()->EnterTransactionMode(&transaction);
149
150 // Allocate an array during transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 Handle<mirror::Array> h_obj(
152 hs.NewHandle(
153 mirror::Array::Alloc<true>(soa.Self(), h_klass.Get(), kArraySize,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700154 h_klass->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
156 ASSERT_TRUE(h_obj.Get() != nullptr);
157 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100158 Runtime::Current()->ExitTransactionMode();
159
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100160 // Rolling back transaction's changes must not reset array's length.
161 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100163}
164
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100165// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100166TEST_F(TransactionTest, StaticFieldsTest) {
167 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 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 Hertzd2fe10a2014-01-15 10:20:56 +0100172
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700173 Handle<mirror::Class> h_klass(
174 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
175 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100176 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
177 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700178 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100179 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100180
181 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 mirror::ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100183 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700184 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100186
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 mirror::ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700189 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 mirror::ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100193 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700194 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700195 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100196
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 mirror::ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100198 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700199 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100201
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 mirror::ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100203 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700204 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100206
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 mirror::ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700209 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700210 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100211
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700212 mirror::ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700214 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700215 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100216
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700217 mirror::ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100218 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700219 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700220 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100221
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700222 mirror::ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100223 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100224 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700225 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100227
228 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700229 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 Hertzd2fe10a2014-01-15 10:20:56 +0100235
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100236 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237 Transaction transaction;
238 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700239 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 Hertzd2fe10a2014-01-15 10:20:56 +0100248 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100249 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250
251 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700252 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 Rogers647b1a82014-10-10 11:02:11 -0700258 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 Chartiereb8167a2014-05-07 15:43:14 -0700260 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100261}
262
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100263// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100264TEST_F(TransactionTest, InstanceFieldsTest) {
265 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 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 Hertzd2fe10a2014-01-15 10:20:56 +0100270
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700271 Handle<mirror::Class> h_klass(
272 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
273 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100274 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
275 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700276 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100277 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100278
279 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700280 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
281 ASSERT_TRUE(h_instance.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282
283 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700284 mirror::ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100285 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700286 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700287 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700289 mirror::ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700291 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700292 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100293
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700294 mirror::ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700296 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700297 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700299 mirror::ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700301 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700302 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100303
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700304 mirror::ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100305 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700306 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700307 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100308
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700309 mirror::ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700311 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700312 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100313
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700314 mirror::ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700316 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700317 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100318
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700319 mirror::ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100320 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700321 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700322 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100323
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700324 mirror::ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800325 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100326 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700327 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700328 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100329
330 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700331 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 Hertzd2fe10a2014-01-15 10:20:56 +0100337
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100338 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100339 Transaction transaction;
340 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700341 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 Hertzd2fe10a2014-01-15 10:20:56 +0100350 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100351 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352
353 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700354 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 Rogers647b1a82014-10-10 11:02:11 -0700360 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 Chartiereb8167a2014-05-07 15:43:14 -0700362 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100363}
364
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100365// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100366TEST_F(TransactionTest, StaticArrayFieldsTest) {
367 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700368 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 Hertzd2fe10a2014-01-15 10:20:56 +0100372
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700373 Handle<mirror::Class> h_klass(
374 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
375 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100376 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
377 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700378 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100379 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100380
381 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700382 mirror::ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100383 ASSERT_TRUE(booleanArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700384 mirror::BooleanArray* booleanArray = booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100385 ASSERT_TRUE(booleanArray != nullptr);
386 ASSERT_EQ(booleanArray->GetLength(), 1);
387 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
388
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700389 mirror::ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100390 ASSERT_TRUE(byteArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700391 mirror::ByteArray* byteArray = byteArrayField->GetObject(h_klass.Get())->AsByteArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100392 ASSERT_TRUE(byteArray != nullptr);
393 ASSERT_EQ(byteArray->GetLength(), 1);
394 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
395
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700396 mirror::ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100397 ASSERT_TRUE(charArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700398 mirror::CharArray* charArray = charArrayField->GetObject(h_klass.Get())->AsCharArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100399 ASSERT_TRUE(charArray != nullptr);
400 ASSERT_EQ(charArray->GetLength(), 1);
401 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
402
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700403 mirror::ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100404 ASSERT_TRUE(shortArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700405 mirror::ShortArray* shortArray = shortArrayField->GetObject(h_klass.Get())->AsShortArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100406 ASSERT_TRUE(shortArray != nullptr);
407 ASSERT_EQ(shortArray->GetLength(), 1);
408 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
409
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700410 mirror::ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100411 ASSERT_TRUE(intArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700412 mirror::IntArray* intArray = intArrayField->GetObject(h_klass.Get())->AsIntArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100413 ASSERT_TRUE(intArray != nullptr);
414 ASSERT_EQ(intArray->GetLength(), 1);
415 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
416
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700417 mirror::ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100418 ASSERT_TRUE(longArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 mirror::LongArray* longArray = longArrayField->GetObject(h_klass.Get())->AsLongArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100420 ASSERT_TRUE(longArray != nullptr);
421 ASSERT_EQ(longArray->GetLength(), 1);
422 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
423
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700424 mirror::ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100425 ASSERT_TRUE(floatArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700426 mirror::FloatArray* floatArray = floatArrayField->GetObject(h_klass.Get())->AsFloatArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100427 ASSERT_TRUE(floatArray != nullptr);
428 ASSERT_EQ(floatArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700429 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100430
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700431 mirror::ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100432 ASSERT_TRUE(doubleArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700433 mirror::DoubleArray* doubleArray = doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100434 ASSERT_TRUE(doubleArray != nullptr);
435 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700436 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100437
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700438 mirror::ArtField* objectArrayField = h_klass->FindDeclaredStaticField("objectArrayField",
Ian Rogers98379392014-02-24 16:53:16 -0800439 "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100440 ASSERT_TRUE(objectArrayField != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800441 mirror::ObjectArray<mirror::Object>* objectArray =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700442 objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100443 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 Chartiereb8167a2014-05-07 15:43:14 -0700448 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 Hertzd2fe10a2014-01-15 10:20:56 +0100454
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100455 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100456 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 Chartiereb8167a2014-05-07 15:43:14 -0700466 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100467 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100468 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100469
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 Rogers647b1a82014-10-10 11:02:11 -0700477 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
478 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100479 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
480}
481
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100482// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100483TEST_F(TransactionTest, EmptyClass) {
484 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700485 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 Hertzd2fe10a2014-01-15 10:20:56 +0100489
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700490 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100491 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
492 class_loader)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700493 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700494 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700495 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100496
497 Transaction transaction;
498 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100499 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100500 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100501 ASSERT_TRUE(success);
502 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100503 ASSERT_FALSE(soa.Self()->IsExceptionPending());
504}
505
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100506// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100507TEST_F(TransactionTest, StaticFieldClass) {
508 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700509 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 Hertzd2fe10a2014-01-15 10:20:56 +0100513
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700514 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 Rogers7b078e82014-09-10 14:44:24 -0700518 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700519 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100520
521 Transaction transaction;
522 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100523 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100524 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100525 ASSERT_TRUE(success);
526 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100527 ASSERT_FALSE(soa.Self()->IsExceptionPending());
528}
529
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100530// Tests failing class initialization due to native call.
531TEST_F(TransactionTest, NativeCallAbortClass) {
532 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100533}
534
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100535// 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).
537TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
538 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
539}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100540
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100541// Tests failing class initialization due to native call, even if an "all" catch handler
542// catches the exception thrown when aborting the transaction.
543TEST_F(TransactionTest, CatchNativeCallAbortClass) {
544 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
545}
546
547// Tests failing class initialization with multiple transaction aborts.
548TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
549 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
550}
551
552// Tests failing class initialization due to allocating instance of finalizable class.
553TEST_F(TransactionTest, FinalizableAbortClass) {
554 testTransactionAbort("LTransaction$FinalizableAbortClass;");
555}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100556} // namespace art