blob: 24ecf6b09642d97fba2a7aaf6f6d988b8d9cdd8e [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 Hertz2fd7e692015-04-02 11:11:19 +020038 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
39 // to abort transaction so they can be thrown during class initialization if the transaction
40 // aborts.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010041 MutableHandle<mirror::Class> h_klass(
42 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
43 "Ljava/lang/ExceptionInInitializerError;")));
44 ASSERT_TRUE(h_klass.Get() != nullptr);
45 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
46 ASSERT_TRUE(h_klass->IsInitialized());
47
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020048 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
49 Transaction::kAbortExceptionSignature));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010050 ASSERT_TRUE(h_klass.Get() != nullptr);
51 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
52 ASSERT_TRUE(h_klass->IsInitialized());
53
54 // Load and verify utility class.
55 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;",
56 class_loader));
57 ASSERT_TRUE(h_klass.Get() != nullptr);
58 class_linker_->VerifyClass(soa.Self(), h_klass);
59 ASSERT_TRUE(h_klass->IsVerified());
60
61 // Load and verify tested class.
62 h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader));
63 ASSERT_TRUE(h_klass.Get() != nullptr);
64 class_linker_->VerifyClass(soa.Self(), h_klass);
65 ASSERT_TRUE(h_klass->IsVerified());
66
67 mirror::Class::Status old_status = h_klass->GetStatus();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080068 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010069
70 Transaction transaction;
71 Runtime::Current()->EnterTransactionMode(&transaction);
72 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
73 Runtime::Current()->ExitTransactionMode();
74 ASSERT_FALSE(success);
75 ASSERT_TRUE(h_klass->IsErroneous());
76 ASSERT_TRUE(soa.Self()->IsExceptionPending());
77 ASSERT_TRUE(transaction.IsAborted());
78
79 // Check class's monitor get back to its original state without rolling back changes.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080080 LockWord new_lock_word = h_klass->GetLockWord(false);
81 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010082
83 // Check class status is rolled back properly.
84 soa.Self()->ClearException();
85 transaction.Rollback();
86 ASSERT_EQ(old_status, h_klass->GetStatus());
87 }
88};
89
90// Tests object's class is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010091TEST_F(TransactionTest, Object_class) {
92 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070093 StackHandleScope<2> hs(soa.Self());
94 Handle<mirror::Class> h_klass(
95 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
96 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010097
98 Transaction transaction;
99 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
101 ASSERT_TRUE(h_obj.Get() != nullptr);
102 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 Runtime::Current()->ExitTransactionMode();
104
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100105 // Rolling back transaction's changes must not clear the Object::class field.
106 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 EXPECT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100108}
109
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100110// Tests object's monitor state is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111TEST_F(TransactionTest, Object_monitor) {
112 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 StackHandleScope<2> hs(soa.Self());
114 Handle<mirror::Class> h_klass(
115 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
116 ASSERT_TRUE(h_klass.Get() != nullptr);
117 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
118 ASSERT_TRUE(h_obj.Get() != nullptr);
119 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100120
121 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 h_obj->MonitorEnter(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800123 LockWord old_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100124
125 Transaction transaction;
126 Runtime::Current()->EnterTransactionMode(&transaction);
127 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700128 h_obj->MonitorExit(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800129 LockWord new_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100130 Runtime::Current()->ExitTransactionMode();
131
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100132 // Rolling back transaction's changes must not change monitor's state.
133 transaction.Rollback();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800134 LockWord aborted_lock_word = h_obj->GetLockWord(false);
135 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
136 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137}
138
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100139// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140TEST_F(TransactionTest, Array_length) {
141 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700142 StackHandleScope<2> hs(soa.Self());
143 Handle<mirror::Class> h_klass(
144 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
145 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146
147 constexpr int32_t kArraySize = 2;
148
149 Transaction transaction;
150 Runtime::Current()->EnterTransactionMode(&transaction);
151
152 // Allocate an array during transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 Handle<mirror::Array> h_obj(
154 hs.NewHandle(
155 mirror::Array::Alloc<true>(soa.Self(), h_klass.Get(), kArraySize,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700156 h_klass->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
158 ASSERT_TRUE(h_obj.Get() != nullptr);
159 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160 Runtime::Current()->ExitTransactionMode();
161
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100162 // Rolling back transaction's changes must not reset array's length.
163 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165}
166
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100167// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100168TEST_F(TransactionTest, StaticFieldsTest) {
169 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 StackHandleScope<4> hs(soa.Self());
171 Handle<mirror::ClassLoader> class_loader(
172 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
173 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100174
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 Handle<mirror::Class> h_klass(
176 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
177 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100178 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
179 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100181 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100182
183 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700184 mirror::ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100185 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700186 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 mirror::ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100190 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700191 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100193
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 mirror::ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100195 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700196 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100198
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 mirror::ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100200 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700201 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100203
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700204 mirror::ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700206 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 mirror::ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700211 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700212 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 mirror::ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100215 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700216 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700217 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100218
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 mirror::ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700221 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700222 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100223
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 mirror::ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100225 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100226 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700227 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700228 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100229
230 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700231 Handle<mirror::Class> object_klass(
232 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
233 ASSERT_TRUE(object_klass.Get() != nullptr);
234 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
235 ASSERT_TRUE(h_obj.Get() != nullptr);
236 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100238 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100239 Transaction transaction;
240 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700241 booleanField->SetBoolean<true>(h_klass.Get(), true);
242 byteField->SetByte<true>(h_klass.Get(), 1);
243 charField->SetChar<true>(h_klass.Get(), 1u);
244 shortField->SetShort<true>(h_klass.Get(), 1);
245 intField->SetInt<true>(h_klass.Get(), 1);
246 longField->SetLong<true>(h_klass.Get(), 1);
247 floatField->SetFloat<true>(h_klass.Get(), 1.0);
248 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
249 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100251 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100252
253 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700254 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
255 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
256 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
257 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
258 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
259 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700260 EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
261 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700262 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100263}
264
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100265// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100266TEST_F(TransactionTest, InstanceFieldsTest) {
267 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700268 StackHandleScope<5> hs(soa.Self());
269 Handle<mirror::ClassLoader> class_loader(
270 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
271 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100272
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700273 Handle<mirror::Class> h_klass(
274 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
275 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100276 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
277 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100279 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280
281 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
283 ASSERT_TRUE(h_instance.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100284
285 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700286 mirror::ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700288 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700289 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700291 mirror::ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700293 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700294 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700296 mirror::ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700298 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700299 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 mirror::ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100302 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700303 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700304 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100305
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700306 mirror::ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100307 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700308 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700309 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700311 mirror::ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100312 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700313 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700314 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 mirror::ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100317 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700318 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700319 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100320
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700321 mirror::ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100322 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700323 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700324 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100325
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700326 mirror::ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800327 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100328 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700329 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700330 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100331
332 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700333 Handle<mirror::Class> object_klass(
334 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
335 ASSERT_TRUE(object_klass.Get() != nullptr);
336 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
337 ASSERT_TRUE(h_obj.Get() != nullptr);
338 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100339
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100340 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 Transaction transaction;
342 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700343 booleanField->SetBoolean<true>(h_instance.Get(), true);
344 byteField->SetByte<true>(h_instance.Get(), 1);
345 charField->SetChar<true>(h_instance.Get(), 1u);
346 shortField->SetShort<true>(h_instance.Get(), 1);
347 intField->SetInt<true>(h_instance.Get(), 1);
348 longField->SetLong<true>(h_instance.Get(), 1);
349 floatField->SetFloat<true>(h_instance.Get(), 1.0);
350 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
351 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100353 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100354
355 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700356 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
357 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
358 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
359 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
360 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
361 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700362 EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
363 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700364 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365}
366
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100367// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100368TEST_F(TransactionTest, StaticArrayFieldsTest) {
369 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700370 StackHandleScope<4> hs(soa.Self());
371 Handle<mirror::ClassLoader> class_loader(
372 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
373 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100374
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700375 Handle<mirror::Class> h_klass(
376 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
377 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100378 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
379 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700380 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100381 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100382
383 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700384 mirror::ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100385 ASSERT_TRUE(booleanArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700386 mirror::BooleanArray* booleanArray = booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100387 ASSERT_TRUE(booleanArray != nullptr);
388 ASSERT_EQ(booleanArray->GetLength(), 1);
389 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
390
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700391 mirror::ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100392 ASSERT_TRUE(byteArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700393 mirror::ByteArray* byteArray = byteArrayField->GetObject(h_klass.Get())->AsByteArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100394 ASSERT_TRUE(byteArray != nullptr);
395 ASSERT_EQ(byteArray->GetLength(), 1);
396 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
397
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700398 mirror::ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100399 ASSERT_TRUE(charArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700400 mirror::CharArray* charArray = charArrayField->GetObject(h_klass.Get())->AsCharArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100401 ASSERT_TRUE(charArray != nullptr);
402 ASSERT_EQ(charArray->GetLength(), 1);
403 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
404
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700405 mirror::ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100406 ASSERT_TRUE(shortArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700407 mirror::ShortArray* shortArray = shortArrayField->GetObject(h_klass.Get())->AsShortArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100408 ASSERT_TRUE(shortArray != nullptr);
409 ASSERT_EQ(shortArray->GetLength(), 1);
410 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
411
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700412 mirror::ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100413 ASSERT_TRUE(intArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 mirror::IntArray* intArray = intArrayField->GetObject(h_klass.Get())->AsIntArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100415 ASSERT_TRUE(intArray != nullptr);
416 ASSERT_EQ(intArray->GetLength(), 1);
417 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
418
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 mirror::ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100420 ASSERT_TRUE(longArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700421 mirror::LongArray* longArray = longArrayField->GetObject(h_klass.Get())->AsLongArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100422 ASSERT_TRUE(longArray != nullptr);
423 ASSERT_EQ(longArray->GetLength(), 1);
424 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
425
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700426 mirror::ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100427 ASSERT_TRUE(floatArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700428 mirror::FloatArray* floatArray = floatArrayField->GetObject(h_klass.Get())->AsFloatArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100429 ASSERT_TRUE(floatArray != nullptr);
430 ASSERT_EQ(floatArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700431 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100432
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700433 mirror::ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100434 ASSERT_TRUE(doubleArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700435 mirror::DoubleArray* doubleArray = doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100436 ASSERT_TRUE(doubleArray != nullptr);
437 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700438 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100439
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700440 mirror::ArtField* objectArrayField = h_klass->FindDeclaredStaticField("objectArrayField",
Ian Rogers98379392014-02-24 16:53:16 -0800441 "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100442 ASSERT_TRUE(objectArrayField != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800443 mirror::ObjectArray<mirror::Object>* objectArray =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700444 objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100445 ASSERT_TRUE(objectArray != nullptr);
446 ASSERT_EQ(objectArray->GetLength(), 1);
447 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
448
449 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700450 Handle<mirror::Class> object_klass(
451 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
452 ASSERT_TRUE(object_klass.Get() != nullptr);
453 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
454 ASSERT_TRUE(h_obj.Get() != nullptr);
455 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100456
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100457 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100458 Transaction transaction;
459 Runtime::Current()->EnterTransactionMode(&transaction);
460 booleanArray->SetWithoutChecks<true>(0, true);
461 byteArray->SetWithoutChecks<true>(0, 1);
462 charArray->SetWithoutChecks<true>(0, 1u);
463 shortArray->SetWithoutChecks<true>(0, 1);
464 intArray->SetWithoutChecks<true>(0, 1);
465 longArray->SetWithoutChecks<true>(0, 1);
466 floatArray->SetWithoutChecks<true>(0, 1.0);
467 doubleArray->SetWithoutChecks<true>(0, 1.0);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700468 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100469 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100470 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100471
472 // Check values have properly been restored to their original (default) value.
473 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
474 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
475 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
476 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
477 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
478 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700479 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
480 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100481 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
482}
483
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100484// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100485TEST_F(TransactionTest, EmptyClass) {
486 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700487 StackHandleScope<2> hs(soa.Self());
488 Handle<mirror::ClassLoader> class_loader(
489 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
490 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100491
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100493 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
494 class_loader)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700495 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700496 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700497 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100498
499 Transaction transaction;
500 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100501 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100502 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100503 ASSERT_TRUE(success);
504 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100505 ASSERT_FALSE(soa.Self()->IsExceptionPending());
506}
507
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100508// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100509TEST_F(TransactionTest, StaticFieldClass) {
510 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700511 StackHandleScope<2> hs(soa.Self());
512 Handle<mirror::ClassLoader> class_loader(
513 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
514 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100515
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700516 Handle<mirror::Class> h_klass(
517 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
518 class_loader)));
519 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700520 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700521 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100522
523 Transaction transaction;
524 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100525 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100526 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100527 ASSERT_TRUE(success);
528 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100529 ASSERT_FALSE(soa.Self()->IsExceptionPending());
530}
531
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100532// Tests failing class initialization due to native call.
533TEST_F(TransactionTest, NativeCallAbortClass) {
534 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100535}
536
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100537// Tests failing class initialization due to native call in a "synchronized" statement
538// (which must catch any exception, do the monitor-exit then re-throw the caught exception).
539TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
540 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
541}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100542
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100543// Tests failing class initialization due to native call, even if an "all" catch handler
544// catches the exception thrown when aborting the transaction.
545TEST_F(TransactionTest, CatchNativeCallAbortClass) {
546 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
547}
548
549// Tests failing class initialization with multiple transaction aborts.
550TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
551 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
552}
553
554// Tests failing class initialization due to allocating instance of finalizable class.
555TEST_F(TransactionTest, FinalizableAbortClass) {
556 testTransactionAbort("LTransaction$FinalizableAbortClass;");
557}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100558} // namespace art