blob: a43c9670925f6999cd6c57202a2934c6ef1f0a1e [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"
Mathieu Chartierbb816d62016-09-07 10:17:46 -070023#include "dex_file.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010024#include "mirror/array-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010026
27namespace art {
28
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010029class TransactionTest : public CommonRuntimeTest {
30 public:
31 // Tests failing class initialization due to native call with transaction rollback.
32 void testTransactionAbort(const char* tested_class_signature) {
33 ScopedObjectAccess soa(Thread::Current());
34 jobject jclass_loader = LoadDex("Transaction");
35 StackHandleScope<2> hs(soa.Self());
36 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070037 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010038 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010039
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020040 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
41 // to abort transaction so they can be thrown during class initialization if the transaction
42 // aborts.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010043 MutableHandle<mirror::Class> h_klass(
44 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
45 "Ljava/lang/ExceptionInInitializerError;")));
46 ASSERT_TRUE(h_klass.Get() != nullptr);
47 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
48 ASSERT_TRUE(h_klass->IsInitialized());
49
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020050 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
51 Transaction::kAbortExceptionSignature));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010052 ASSERT_TRUE(h_klass.Get() != nullptr);
53 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
54 ASSERT_TRUE(h_klass->IsInitialized());
55
56 // Load and verify utility class.
57 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;",
58 class_loader));
59 ASSERT_TRUE(h_klass.Get() != nullptr);
60 class_linker_->VerifyClass(soa.Self(), h_klass);
61 ASSERT_TRUE(h_klass->IsVerified());
62
63 // Load and verify tested class.
64 h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader));
65 ASSERT_TRUE(h_klass.Get() != nullptr);
66 class_linker_->VerifyClass(soa.Self(), h_klass);
67 ASSERT_TRUE(h_klass->IsVerified());
68
69 mirror::Class::Status old_status = h_klass->GetStatus();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080070 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010071
72 Transaction transaction;
73 Runtime::Current()->EnterTransactionMode(&transaction);
74 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
75 Runtime::Current()->ExitTransactionMode();
76 ASSERT_FALSE(success);
77 ASSERT_TRUE(h_klass->IsErroneous());
78 ASSERT_TRUE(soa.Self()->IsExceptionPending());
79 ASSERT_TRUE(transaction.IsAborted());
80
81 // Check class's monitor get back to its original state without rolling back changes.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080082 LockWord new_lock_word = h_klass->GetLockWord(false);
83 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010084
85 // Check class status is rolled back properly.
86 soa.Self()->ClearException();
87 transaction.Rollback();
88 ASSERT_EQ(old_status, h_klass->GetStatus());
89 }
90};
91
92// Tests object's class is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010093TEST_F(TransactionTest, Object_class) {
94 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070095 StackHandleScope<2> hs(soa.Self());
96 Handle<mirror::Class> h_klass(
97 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
98 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010099
100 Transaction transaction;
101 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700102 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
103 ASSERT_TRUE(h_obj.Get() != nullptr);
104 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100105 Runtime::Current()->ExitTransactionMode();
106
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100107 // Rolling back transaction's changes must not clear the Object::class field.
108 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 EXPECT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100110}
111
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100112// Tests object's monitor state is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113TEST_F(TransactionTest, Object_monitor) {
114 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 StackHandleScope<2> hs(soa.Self());
116 Handle<mirror::Class> h_klass(
117 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
118 ASSERT_TRUE(h_klass.Get() != nullptr);
119 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
120 ASSERT_TRUE(h_obj.Get() != nullptr);
121 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122
123 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 h_obj->MonitorEnter(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800125 LockWord old_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100126
127 Transaction transaction;
128 Runtime::Current()->EnterTransactionMode(&transaction);
129 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 h_obj->MonitorExit(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800131 LockWord new_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100132 Runtime::Current()->ExitTransactionMode();
133
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100134 // Rolling back transaction's changes must not change monitor's state.
135 transaction.Rollback();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800136 LockWord aborted_lock_word = h_obj->GetLockWord(false);
137 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
138 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100139}
140
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100141// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142TEST_F(TransactionTest, Array_length) {
143 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 StackHandleScope<2> hs(soa.Self());
145 Handle<mirror::Class> h_klass(
146 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
147 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148
149 constexpr int32_t kArraySize = 2;
150
151 Transaction transaction;
152 Runtime::Current()->EnterTransactionMode(&transaction);
153
154 // Allocate an array during transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 Handle<mirror::Array> h_obj(
156 hs.NewHandle(
157 mirror::Array::Alloc<true>(soa.Self(), h_klass.Get(), kArraySize,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700158 h_klass->GetComponentSizeShift(),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
160 ASSERT_TRUE(h_obj.Get() != nullptr);
161 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162 Runtime::Current()->ExitTransactionMode();
163
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100164 // Rolling back transaction's changes must not reset array's length.
165 transaction.Rollback();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167}
168
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100169// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100170TEST_F(TransactionTest, StaticFieldsTest) {
171 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700172 StackHandleScope<4> hs(soa.Self());
173 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700174 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100176
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 Handle<mirror::Class> h_klass(
178 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
179 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100180 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
181 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100183 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100184
185 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700186 ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100187 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700188 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100190
Mathieu Chartierc7853442015-03-27 14:35:38 -0700191 ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100192 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700193 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100195
Mathieu Chartierc7853442015-03-27 14:35:38 -0700196 ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100197 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700198 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100200
Mathieu Chartierc7853442015-03-27 14:35:38 -0700201 ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100202 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700203 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700204 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205
Mathieu Chartierc7853442015-03-27 14:35:38 -0700206 ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700208 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210
Mathieu Chartierc7853442015-03-27 14:35:38 -0700211 ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100212 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700213 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100215
Mathieu Chartierc7853442015-03-27 14:35:38 -0700216 ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700218 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700219 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220
Mathieu Chartierc7853442015-03-27 14:35:38 -0700221 ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700223 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700224 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100225
Mathieu Chartierc7853442015-03-27 14:35:38 -0700226 ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100227 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100228 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700229 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700230 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100231
232 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700233 Handle<mirror::Class> object_klass(
234 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
235 ASSERT_TRUE(object_klass.Get() != nullptr);
236 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
237 ASSERT_TRUE(h_obj.Get() != nullptr);
238 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100239
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100240 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100241 Transaction transaction;
242 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700243 booleanField->SetBoolean<true>(h_klass.Get(), true);
244 byteField->SetByte<true>(h_klass.Get(), 1);
245 charField->SetChar<true>(h_klass.Get(), 1u);
246 shortField->SetShort<true>(h_klass.Get(), 1);
247 intField->SetInt<true>(h_klass.Get(), 1);
248 longField->SetLong<true>(h_klass.Get(), 1);
249 floatField->SetFloat<true>(h_klass.Get(), 1.0);
250 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
251 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100252 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100253 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100254
255 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700256 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
257 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
258 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
259 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
260 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
261 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700262 EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
263 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700264 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100265}
266
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100267// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100268TEST_F(TransactionTest, InstanceFieldsTest) {
269 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700270 StackHandleScope<5> hs(soa.Self());
271 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700272 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700273 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100274
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 Handle<mirror::Class> h_klass(
276 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
277 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100278 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
279 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700280 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100281 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282
283 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700284 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
285 ASSERT_TRUE(h_instance.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100286
287 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700288 ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100289 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700290 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700291 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292
Mathieu Chartierc7853442015-03-27 14:35:38 -0700293 ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700295 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700296 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297
Mathieu Chartierc7853442015-03-27 14:35:38 -0700298 ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100299 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700300 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100302
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303 ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100304 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700305 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700306 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100307
Mathieu Chartierc7853442015-03-27 14:35:38 -0700308 ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100309 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700310 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700311 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100312
Mathieu Chartierc7853442015-03-27 14:35:38 -0700313 ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100314 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700315 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100317
Mathieu Chartierc7853442015-03-27 14:35:38 -0700318 ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100319 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700320 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700321 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100322
Mathieu Chartierc7853442015-03-27 14:35:38 -0700323 ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100324 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700325 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700326 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100327
Mathieu Chartierc7853442015-03-27 14:35:38 -0700328 ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800329 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100330 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700331 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700332 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100333
334 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700335 Handle<mirror::Class> object_klass(
336 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
337 ASSERT_TRUE(object_klass.Get() != nullptr);
338 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
339 ASSERT_TRUE(h_obj.Get() != nullptr);
340 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100342 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 Transaction transaction;
344 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700345 booleanField->SetBoolean<true>(h_instance.Get(), true);
346 byteField->SetByte<true>(h_instance.Get(), 1);
347 charField->SetChar<true>(h_instance.Get(), 1u);
348 shortField->SetShort<true>(h_instance.Get(), 1);
349 intField->SetInt<true>(h_instance.Get(), 1);
350 longField->SetLong<true>(h_instance.Get(), 1);
351 floatField->SetFloat<true>(h_instance.Get(), 1.0);
352 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
353 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100354 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100355 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100356
357 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700358 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
359 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
360 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
361 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
362 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
363 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700364 EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
365 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700366 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100367}
368
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100369// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100370TEST_F(TransactionTest, StaticArrayFieldsTest) {
371 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700372 StackHandleScope<4> hs(soa.Self());
373 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700374 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700375 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100376
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700377 Handle<mirror::Class> h_klass(
378 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
379 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100380 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
381 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700382 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100383 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100384
385 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700386 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100387 ASSERT_TRUE(booleanArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700388 mirror::BooleanArray* booleanArray = booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100389 ASSERT_TRUE(booleanArray != nullptr);
390 ASSERT_EQ(booleanArray->GetLength(), 1);
391 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
392
Mathieu Chartierc7853442015-03-27 14:35:38 -0700393 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100394 ASSERT_TRUE(byteArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700395 mirror::ByteArray* byteArray = byteArrayField->GetObject(h_klass.Get())->AsByteArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100396 ASSERT_TRUE(byteArray != nullptr);
397 ASSERT_EQ(byteArray->GetLength(), 1);
398 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
399
Mathieu Chartierc7853442015-03-27 14:35:38 -0700400 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100401 ASSERT_TRUE(charArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700402 mirror::CharArray* charArray = charArrayField->GetObject(h_klass.Get())->AsCharArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100403 ASSERT_TRUE(charArray != nullptr);
404 ASSERT_EQ(charArray->GetLength(), 1);
405 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
406
Mathieu Chartierc7853442015-03-27 14:35:38 -0700407 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100408 ASSERT_TRUE(shortArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700409 mirror::ShortArray* shortArray = shortArrayField->GetObject(h_klass.Get())->AsShortArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100410 ASSERT_TRUE(shortArray != nullptr);
411 ASSERT_EQ(shortArray->GetLength(), 1);
412 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
413
Mathieu Chartierc7853442015-03-27 14:35:38 -0700414 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100415 ASSERT_TRUE(intArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700416 mirror::IntArray* intArray = intArrayField->GetObject(h_klass.Get())->AsIntArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100417 ASSERT_TRUE(intArray != nullptr);
418 ASSERT_EQ(intArray->GetLength(), 1);
419 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
420
Mathieu Chartierc7853442015-03-27 14:35:38 -0700421 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100422 ASSERT_TRUE(longArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700423 mirror::LongArray* longArray = longArrayField->GetObject(h_klass.Get())->AsLongArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100424 ASSERT_TRUE(longArray != nullptr);
425 ASSERT_EQ(longArray->GetLength(), 1);
426 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
427
Mathieu Chartierc7853442015-03-27 14:35:38 -0700428 ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100429 ASSERT_TRUE(floatArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700430 mirror::FloatArray* floatArray = floatArrayField->GetObject(h_klass.Get())->AsFloatArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100431 ASSERT_TRUE(floatArray != nullptr);
432 ASSERT_EQ(floatArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700433 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100434
Mathieu Chartierc7853442015-03-27 14:35:38 -0700435 ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100436 ASSERT_TRUE(doubleArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700437 mirror::DoubleArray* doubleArray = doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100438 ASSERT_TRUE(doubleArray != nullptr);
439 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700440 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100441
Mathieu Chartierc7853442015-03-27 14:35:38 -0700442 ArtField* objectArrayField = h_klass->FindDeclaredStaticField("objectArrayField",
Ian Rogers98379392014-02-24 16:53:16 -0800443 "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100444 ASSERT_TRUE(objectArrayField != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800445 mirror::ObjectArray<mirror::Object>* objectArray =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700446 objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100447 ASSERT_TRUE(objectArray != nullptr);
448 ASSERT_EQ(objectArray->GetLength(), 1);
449 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
450
451 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700452 Handle<mirror::Class> object_klass(
453 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
454 ASSERT_TRUE(object_klass.Get() != nullptr);
455 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
456 ASSERT_TRUE(h_obj.Get() != nullptr);
457 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100458
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100459 // Modify fields inside transaction then rollback changes.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100460 Transaction transaction;
461 Runtime::Current()->EnterTransactionMode(&transaction);
462 booleanArray->SetWithoutChecks<true>(0, true);
463 byteArray->SetWithoutChecks<true>(0, 1);
464 charArray->SetWithoutChecks<true>(0, 1u);
465 shortArray->SetWithoutChecks<true>(0, 1);
466 intArray->SetWithoutChecks<true>(0, 1);
467 longArray->SetWithoutChecks<true>(0, 1);
468 floatArray->SetWithoutChecks<true>(0, 1.0);
469 doubleArray->SetWithoutChecks<true>(0, 1.0);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700470 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100471 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100472 transaction.Rollback();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100473
474 // Check values have properly been restored to their original (default) value.
475 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
476 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
477 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
478 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
479 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
480 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700481 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
482 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100483 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
484}
485
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700486// Tests rolling back interned strings and resolved strings.
487TEST_F(TransactionTest, ResolveString) {
488 ScopedObjectAccess soa(Thread::Current());
489 StackHandleScope<3> hs(soa.Self());
490 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700491 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700492 ASSERT_TRUE(class_loader.Get() != nullptr);
493
494 Handle<mirror::Class> h_klass(
495 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;",
496 class_loader)));
497 ASSERT_TRUE(h_klass.Get() != nullptr);
498
499 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
500 ASSERT_TRUE(h_dex_cache.Get() != nullptr);
501 const DexFile* const dex_file = h_dex_cache->GetDexFile();
502 ASSERT_TRUE(dex_file != nullptr);
503
504 // Go search the dex file to find the string id of our string.
505 static const char* kResolvedString = "ResolvedString";
506 const DexFile::StringId* string_id = dex_file->FindStringId(kResolvedString);
507 ASSERT_TRUE(string_id != nullptr);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800508 dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
509 ASSERT_TRUE(string_idx.IsValid());
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700510 // String should only get resolved by the initializer.
511 EXPECT_TRUE(class_linker_->LookupString(*dex_file, string_idx, h_dex_cache) == nullptr);
512 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
513 // Do the transaction, then roll back.
514 Transaction transaction;
515 Runtime::Current()->EnterTransactionMode(&transaction);
516 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
517 ASSERT_TRUE(success);
518 ASSERT_TRUE(h_klass->IsInitialized());
519 // Make sure the string got resolved by the transaction.
520 {
521 mirror::String* s = class_linker_->LookupString(*dex_file, string_idx, h_dex_cache);
522 ASSERT_TRUE(s != nullptr);
523 EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
524 EXPECT_EQ(s, h_dex_cache->GetResolvedString(string_idx));
525 }
526 Runtime::Current()->ExitTransactionMode();
527 transaction.Rollback();
528 // Check that the string did not stay resolved.
529 EXPECT_TRUE(class_linker_->LookupString(*dex_file, string_idx, h_dex_cache) == nullptr);
530 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
531 ASSERT_FALSE(h_klass->IsInitialized());
532 ASSERT_FALSE(soa.Self()->IsExceptionPending());
533}
534
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100535// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100536TEST_F(TransactionTest, EmptyClass) {
537 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700538 StackHandleScope<2> hs(soa.Self());
539 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700540 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700541 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100542
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700543 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100544 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
545 class_loader)));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700546 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700547 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700548 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100549
550 Transaction transaction;
551 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100552 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100553 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100554 ASSERT_TRUE(success);
555 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100556 ASSERT_FALSE(soa.Self()->IsExceptionPending());
557}
558
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100559// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100560TEST_F(TransactionTest, StaticFieldClass) {
561 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700562 StackHandleScope<2> hs(soa.Self());
563 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700564 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700565 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100566
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700567 Handle<mirror::Class> h_klass(
568 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
569 class_loader)));
570 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700571 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700572 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100573
574 Transaction transaction;
575 Runtime::Current()->EnterTransactionMode(&transaction);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100576 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100577 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100578 ASSERT_TRUE(success);
579 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100580 ASSERT_FALSE(soa.Self()->IsExceptionPending());
581}
582
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100583// Tests failing class initialization due to native call.
584TEST_F(TransactionTest, NativeCallAbortClass) {
585 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100586}
587
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100588// Tests failing class initialization due to native call in a "synchronized" statement
589// (which must catch any exception, do the monitor-exit then re-throw the caught exception).
590TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
591 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
592}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100593
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100594// Tests failing class initialization due to native call, even if an "all" catch handler
595// catches the exception thrown when aborting the transaction.
596TEST_F(TransactionTest, CatchNativeCallAbortClass) {
597 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
598}
599
600// Tests failing class initialization with multiple transaction aborts.
601TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
602 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
603}
604
605// Tests failing class initialization due to allocating instance of finalizable class.
606TEST_F(TransactionTest, FinalizableAbortClass) {
607 testTransactionAbort("LTransaction$FinalizableAbortClass;");
608}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100609} // namespace art