blob: 8279a26f52ebaee62cfe693c167221da87f0de22 [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
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_runtime_test.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010023#include "mirror/array-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "scoped_thread_state_change.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010025
26namespace art {
27
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010028class TransactionTest : public CommonRuntimeTest {
29 public:
30 // Tests failing class initialization due to native call with transaction rollback.
31 void testTransactionAbort(const char* tested_class_signature) {
32 ScopedObjectAccess soa(Thread::Current());
33 jobject jclass_loader = LoadDex("Transaction");
34 StackHandleScope<2> hs(soa.Self());
35 Handle<mirror::ClassLoader> class_loader(
36 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
37 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010038
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020039 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
40 // to abort transaction so they can be thrown during class initialization if the transaction
41 // aborts.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010042 MutableHandle<mirror::Class> h_klass(
43 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
44 "Ljava/lang/ExceptionInInitializerError;")));
45 ASSERT_TRUE(h_klass.Get() != nullptr);
46 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
47 ASSERT_TRUE(h_klass->IsInitialized());
48
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020049 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
50 Transaction::kAbortExceptionSignature));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010051 ASSERT_TRUE(h_klass.Get() != nullptr);
52 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
53 ASSERT_TRUE(h_klass->IsInitialized());
54
55 // Load and verify utility class.
56 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;",
57 class_loader));
58 ASSERT_TRUE(h_klass.Get() != nullptr);
59 class_linker_->VerifyClass(soa.Self(), h_klass);
60 ASSERT_TRUE(h_klass->IsVerified());
61
62 // Load and verify tested class.
63 h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader));
64 ASSERT_TRUE(h_klass.Get() != nullptr);
65 class_linker_->VerifyClass(soa.Self(), h_klass);
66 ASSERT_TRUE(h_klass->IsVerified());
67
68 mirror::Class::Status old_status = h_klass->GetStatus();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080069 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010070
71 Transaction transaction;
72 Runtime::Current()->EnterTransactionMode(&transaction);
73 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
74 Runtime::Current()->ExitTransactionMode();
75 ASSERT_FALSE(success);
76 ASSERT_TRUE(h_klass->IsErroneous());
77 ASSERT_TRUE(soa.Self()->IsExceptionPending());
78 ASSERT_TRUE(transaction.IsAborted());
79
80 // Check class's monitor get back to its original state without rolling back changes.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080081 LockWord new_lock_word = h_klass->GetLockWord(false);
82 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010083
84 // Check class status is rolled back properly.
85 soa.Self()->ClearException();
86 transaction.Rollback();
87 ASSERT_EQ(old_status, h_klass->GetStatus());
88 }
89};
90
91// Tests object's class is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010092TEST_F(TransactionTest, Object_class) {
93 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070094 StackHandleScope<2> hs(soa.Self());
95 Handle<mirror::Class> h_klass(
96 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
97 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098
99 Transaction transaction;
100 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700101 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
102 ASSERT_TRUE(h_obj.Get() != nullptr);
103 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 Runtime::Current()->ExitTransactionMode();
105
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100106 // Rolling back transaction's changes must not clear the Object::class field.
107 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 EXPECT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109}
110
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100111// Tests object's monitor state is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112TEST_F(TransactionTest, Object_monitor) {
113 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 StackHandleScope<2> hs(soa.Self());
115 Handle<mirror::Class> h_klass(
116 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
117 ASSERT_TRUE(h_klass.Get() != nullptr);
118 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
119 ASSERT_TRUE(h_obj.Get() != nullptr);
120 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121
122 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 h_obj->MonitorEnter(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800124 LockWord old_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125
126 Transaction transaction;
127 Runtime::Current()->EnterTransactionMode(&transaction);
128 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 h_obj->MonitorExit(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800130 LockWord new_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131 Runtime::Current()->ExitTransactionMode();
132
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100133 // Rolling back transaction's changes must not change monitor's state.
134 transaction.Rollback();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800135 LockWord aborted_lock_word = h_obj->GetLockWord(false);
136 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
137 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138}
139
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100140// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141TEST_F(TransactionTest, Array_length) {
142 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700143 StackHandleScope<2> hs(soa.Self());
144 Handle<mirror::Class> h_klass(
145 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
146 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100147
148 constexpr int32_t kArraySize = 2;
149
150 Transaction transaction;
151 Runtime::Current()->EnterTransactionMode(&transaction);
152
153 // Allocate an array during transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 Handle<mirror::Array> h_obj(
155 hs.NewHandle(
156 mirror::Array::Alloc<true>(soa.Self(), h_klass.Get(), kArraySize,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700157 h_klass->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
159 ASSERT_TRUE(h_obj.Get() != nullptr);
160 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100161 Runtime::Current()->ExitTransactionMode();
162
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100163 // Rolling back transaction's changes must not reset array's length.
164 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100166}
167
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100168// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169TEST_F(TransactionTest, StaticFieldsTest) {
170 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700171 StackHandleScope<4> hs(soa.Self());
172 Handle<mirror::ClassLoader> class_loader(
173 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
174 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100175
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700176 Handle<mirror::Class> h_klass(
177 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
178 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100179 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
180 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100182 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100183
184 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700185 ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100186 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700187 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100189
Mathieu Chartierc7853442015-03-27 14:35:38 -0700190 ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700192 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194
Mathieu Chartierc7853442015-03-27 14:35:38 -0700195 ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100196 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700197 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199
Mathieu Chartierc7853442015-03-27 14:35:38 -0700200 ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100201 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700202 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100204
Mathieu Chartierc7853442015-03-27 14:35:38 -0700205 ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100206 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700207 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100209
Mathieu Chartierc7853442015-03-27 14:35:38 -0700210 ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100211 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700212 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214
Mathieu Chartierc7853442015-03-27 14:35:38 -0700215 ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100216 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700217 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700218 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219
Mathieu Chartierc7853442015-03-27 14:35:38 -0700220 ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100221 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700222 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700223 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100224
Mathieu Chartierc7853442015-03-27 14:35:38 -0700225 ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100226 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100227 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700228 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700229 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100230
231 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700232 Handle<mirror::Class> object_klass(
233 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
234 ASSERT_TRUE(object_klass.Get() != nullptr);
235 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
236 ASSERT_TRUE(h_obj.Get() != nullptr);
237 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100238
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100239 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100240 Transaction transaction;
241 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700242 booleanField->SetBoolean<true>(h_klass.Get(), true);
243 byteField->SetByte<true>(h_klass.Get(), 1);
244 charField->SetChar<true>(h_klass.Get(), 1u);
245 shortField->SetShort<true>(h_klass.Get(), 1);
246 intField->SetInt<true>(h_klass.Get(), 1);
247 longField->SetLong<true>(h_klass.Get(), 1);
248 floatField->SetFloat<true>(h_klass.Get(), 1.0);
249 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
250 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100251 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100252 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100253
254 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
256 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
257 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
258 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
259 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
260 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700261 EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
262 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700263 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100264}
265
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100266// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100267TEST_F(TransactionTest, InstanceFieldsTest) {
268 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700269 StackHandleScope<5> hs(soa.Self());
270 Handle<mirror::ClassLoader> class_loader(
271 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
272 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700274 Handle<mirror::Class> h_klass(
275 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
276 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100277 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
278 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700279 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100280 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100281
282 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700283 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
284 ASSERT_TRUE(h_instance.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100285
286 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700287 ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700289 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700290 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291
Mathieu Chartierc7853442015-03-27 14:35:38 -0700292 ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100293 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700294 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700295 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296
Mathieu Chartierc7853442015-03-27 14:35:38 -0700297 ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700299 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700300 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301
Mathieu Chartierc7853442015-03-27 14:35:38 -0700302 ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100303 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700304 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700305 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100306
Mathieu Chartierc7853442015-03-27 14:35:38 -0700307 ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100308 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700309 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700310 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100311
Mathieu Chartierc7853442015-03-27 14:35:38 -0700312 ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100313 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700314 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700315 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100316
Mathieu Chartierc7853442015-03-27 14:35:38 -0700317 ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100318 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700319 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700320 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100321
Mathieu Chartierc7853442015-03-27 14:35:38 -0700322 ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100323 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700324 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700325 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100326
Mathieu Chartierc7853442015-03-27 14:35:38 -0700327 ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800328 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100329 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700330 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700331 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100332
333 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700334 Handle<mirror::Class> object_klass(
335 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
336 ASSERT_TRUE(object_klass.Get() != nullptr);
337 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
338 ASSERT_TRUE(h_obj.Get() != nullptr);
339 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100340
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100341 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100342 Transaction transaction;
343 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700344 booleanField->SetBoolean<true>(h_instance.Get(), true);
345 byteField->SetByte<true>(h_instance.Get(), 1);
346 charField->SetChar<true>(h_instance.Get(), 1u);
347 shortField->SetShort<true>(h_instance.Get(), 1);
348 intField->SetInt<true>(h_instance.Get(), 1);
349 longField->SetLong<true>(h_instance.Get(), 1);
350 floatField->SetFloat<true>(h_instance.Get(), 1.0);
351 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
352 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100354 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100355
356 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700357 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
358 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
359 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
360 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
361 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
362 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700363 EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
364 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700365 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100366}
367
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100368// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100369TEST_F(TransactionTest, StaticArrayFieldsTest) {
370 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700371 StackHandleScope<4> hs(soa.Self());
372 Handle<mirror::ClassLoader> class_loader(
373 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
374 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100375
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700376 Handle<mirror::Class> h_klass(
377 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
378 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100379 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
380 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700381 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100382 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100383
384 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700385 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100386 ASSERT_TRUE(booleanArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700387 mirror::BooleanArray* booleanArray = booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100388 ASSERT_TRUE(booleanArray != nullptr);
389 ASSERT_EQ(booleanArray->GetLength(), 1);
390 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
391
Mathieu Chartierc7853442015-03-27 14:35:38 -0700392 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 ASSERT_TRUE(byteArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700394 mirror::ByteArray* byteArray = byteArrayField->GetObject(h_klass.Get())->AsByteArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100395 ASSERT_TRUE(byteArray != nullptr);
396 ASSERT_EQ(byteArray->GetLength(), 1);
397 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
398
Mathieu Chartierc7853442015-03-27 14:35:38 -0700399 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100400 ASSERT_TRUE(charArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700401 mirror::CharArray* charArray = charArrayField->GetObject(h_klass.Get())->AsCharArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100402 ASSERT_TRUE(charArray != nullptr);
403 ASSERT_EQ(charArray->GetLength(), 1);
404 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
405
Mathieu Chartierc7853442015-03-27 14:35:38 -0700406 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100407 ASSERT_TRUE(shortArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700408 mirror::ShortArray* shortArray = shortArrayField->GetObject(h_klass.Get())->AsShortArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100409 ASSERT_TRUE(shortArray != nullptr);
410 ASSERT_EQ(shortArray->GetLength(), 1);
411 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
412
Mathieu Chartierc7853442015-03-27 14:35:38 -0700413 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100414 ASSERT_TRUE(intArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700415 mirror::IntArray* intArray = intArrayField->GetObject(h_klass.Get())->AsIntArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100416 ASSERT_TRUE(intArray != nullptr);
417 ASSERT_EQ(intArray->GetLength(), 1);
418 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
419
Mathieu Chartierc7853442015-03-27 14:35:38 -0700420 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100421 ASSERT_TRUE(longArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700422 mirror::LongArray* longArray = longArrayField->GetObject(h_klass.Get())->AsLongArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100423 ASSERT_TRUE(longArray != nullptr);
424 ASSERT_EQ(longArray->GetLength(), 1);
425 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
426
Mathieu Chartierc7853442015-03-27 14:35:38 -0700427 ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100428 ASSERT_TRUE(floatArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700429 mirror::FloatArray* floatArray = floatArrayField->GetObject(h_klass.Get())->AsFloatArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100430 ASSERT_TRUE(floatArray != nullptr);
431 ASSERT_EQ(floatArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700432 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100433
Mathieu Chartierc7853442015-03-27 14:35:38 -0700434 ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100435 ASSERT_TRUE(doubleArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700436 mirror::DoubleArray* doubleArray = doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100437 ASSERT_TRUE(doubleArray != nullptr);
438 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700439 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100440
Mathieu Chartierc7853442015-03-27 14:35:38 -0700441 ArtField* objectArrayField = h_klass->FindDeclaredStaticField("objectArrayField",
Ian Rogers98379392014-02-24 16:53:16 -0800442 "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100443 ASSERT_TRUE(objectArrayField != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800444 mirror::ObjectArray<mirror::Object>* objectArray =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700445 objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100446 ASSERT_TRUE(objectArray != nullptr);
447 ASSERT_EQ(objectArray->GetLength(), 1);
448 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
449
450 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700451 Handle<mirror::Class> object_klass(
452 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
453 ASSERT_TRUE(object_klass.Get() != nullptr);
454 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
455 ASSERT_TRUE(h_obj.Get() != nullptr);
456 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100457
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100458 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100459 Transaction transaction;
460 Runtime::Current()->EnterTransactionMode(&transaction);
461 booleanArray->SetWithoutChecks<true>(0, true);
462 byteArray->SetWithoutChecks<true>(0, 1);
463 charArray->SetWithoutChecks<true>(0, 1u);
464 shortArray->SetWithoutChecks<true>(0, 1);
465 intArray->SetWithoutChecks<true>(0, 1);
466 longArray->SetWithoutChecks<true>(0, 1);
467 floatArray->SetWithoutChecks<true>(0, 1.0);
468 doubleArray->SetWithoutChecks<true>(0, 1.0);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700469 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100470 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100471 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100472
473 // Check values have properly been restored to their original (default) value.
474 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
475 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
476 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
477 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
478 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
479 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700480 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
481 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100482 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
483}
484
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100485// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100486TEST_F(TransactionTest, EmptyClass) {
487 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700488 StackHandleScope<2> hs(soa.Self());
489 Handle<mirror::ClassLoader> class_loader(
490 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
491 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100492
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700493 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100494 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
495 class_loader)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700496 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700497 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700498 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100499
500 Transaction transaction;
501 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100502 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100503 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100504 ASSERT_TRUE(success);
505 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100506 ASSERT_FALSE(soa.Self()->IsExceptionPending());
507}
508
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100509// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100510TEST_F(TransactionTest, StaticFieldClass) {
511 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700512 StackHandleScope<2> hs(soa.Self());
513 Handle<mirror::ClassLoader> class_loader(
514 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
515 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100516
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700517 Handle<mirror::Class> h_klass(
518 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
519 class_loader)));
520 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700522 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100523
524 Transaction transaction;
525 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100526 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100527 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100528 ASSERT_TRUE(success);
529 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100530 ASSERT_FALSE(soa.Self()->IsExceptionPending());
531}
532
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100533// Tests failing class initialization due to native call.
534TEST_F(TransactionTest, NativeCallAbortClass) {
535 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100536}
537
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100538// Tests failing class initialization due to native call in a "synchronized" statement
539// (which must catch any exception, do the monitor-exit then re-throw the caught exception).
540TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
541 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
542}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100543
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100544// Tests failing class initialization due to native call, even if an "all" catch handler
545// catches the exception thrown when aborting the transaction.
546TEST_F(TransactionTest, CatchNativeCallAbortClass) {
547 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
548}
549
550// Tests failing class initialization with multiple transaction aborts.
551TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
552 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
553}
554
555// Tests failing class initialization due to allocating instance of finalizable class.
556TEST_F(TransactionTest, FinalizableAbortClass) {
557 testTransactionAbort("LTransaction$FinalizableAbortClass;");
558}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100559} // namespace art