blob: 7ad741a7a01bec631da9f488e83660976cec46fc [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"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070024#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070025#include "mirror/class-alloc-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010027
28namespace art {
29
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010030class TransactionTest : public CommonRuntimeTest {
31 public:
32 // Tests failing class initialization due to native call with transaction rollback.
33 void testTransactionAbort(const char* tested_class_signature) {
34 ScopedObjectAccess soa(Thread::Current());
35 jobject jclass_loader = LoadDex("Transaction");
36 StackHandleScope<2> hs(soa.Self());
37 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070038 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080039 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010040
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020041 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
42 // to abort transaction so they can be thrown during class initialization if the transaction
43 // aborts.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010044 MutableHandle<mirror::Class> h_klass(
45 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
46 "Ljava/lang/ExceptionInInitializerError;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -080047 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010048 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
49 ASSERT_TRUE(h_klass->IsInitialized());
50
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020051 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
52 Transaction::kAbortExceptionSignature));
Andreas Gampefa4333d2017-02-14 11:10:34 -080053 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010054 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
55 ASSERT_TRUE(h_klass->IsInitialized());
56
57 // Load and verify utility class.
58 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;",
59 class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080060 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010061 class_linker_->VerifyClass(soa.Self(), h_klass);
62 ASSERT_TRUE(h_klass->IsVerified());
63
64 // Load and verify tested class.
65 h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080066 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010067 class_linker_->VerifyClass(soa.Self(), h_klass);
68 ASSERT_TRUE(h_klass->IsVerified());
69
Vladimir Marko2c64a832018-01-04 11:31:56 +000070 ClassStatus old_status = h_klass->GetStatus();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080071 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010072
Chang Xing16d1dd82017-07-20 17:56:26 -070073 Runtime::Current()->EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010074 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Chang Xing16d1dd82017-07-20 17:56:26 -070075 ASSERT_TRUE(Runtime::Current()->IsTransactionAborted());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010076 ASSERT_FALSE(success);
77 ASSERT_TRUE(h_klass->IsErroneous());
78 ASSERT_TRUE(soa.Self()->IsExceptionPending());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010079
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();
Chang Xing16d1dd82017-07-20 17:56:26 -070086 Runtime::Current()->RollbackAndExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010087 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;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -080097 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098
Chang Xing16d1dd82017-07-20 17:56:26 -070099 Runtime::Current()->EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800101 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100102 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100103 // Rolling back transaction's changes must not clear the Object::class field.
Chang Xing16d1dd82017-07-20 17:56:26 -0700104 Runtime::Current()->RollbackAndExitTransactionMode();
Vladimir Markodfc0de72019-04-01 10:57:55 +0100105 EXPECT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100106}
107
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100108// Tests object's monitor state is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109TEST_F(TransactionTest, Object_monitor) {
110 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 StackHandleScope<2> hs(soa.Self());
112 Handle<mirror::Class> h_klass(
113 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800114 ASSERT_TRUE(h_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800116 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100117 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118
119 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 h_obj->MonitorEnter(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800121 LockWord old_lock_word = h_obj->GetLockWord(false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122
Chang Xing16d1dd82017-07-20 17:56:26 -0700123 Runtime::Current()->EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100124 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 h_obj->MonitorExit(soa.Self());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800126 LockWord new_lock_word = h_obj->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100127 // Rolling back transaction's changes must not change monitor's state.
Chang Xing16d1dd82017-07-20 17:56:26 -0700128 Runtime::Current()->RollbackAndExitTransactionMode();
129
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800130 LockWord aborted_lock_word = h_obj->GetLockWord(false);
131 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
132 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100133}
134
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100135// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136TEST_F(TransactionTest, Array_length) {
137 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138 StackHandleScope<2> hs(soa.Self());
139 Handle<mirror::Class> h_klass(
140 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800141 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142
143 constexpr int32_t kArraySize = 2;
144
Chang Xing16d1dd82017-07-20 17:56:26 -0700145 Runtime::Current()->EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146
147 // Allocate an array during transaction.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100148 Handle<mirror::Array> h_obj = hs.NewHandle(
149 mirror::Array::Alloc(soa.Self(),
150 h_klass.Get(),
151 kArraySize,
152 h_klass->GetComponentSizeShift(),
153 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800154 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100155 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Chang Xing16d1dd82017-07-20 17:56:26 -0700156 Runtime::Current()->RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100157
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100158 // Rolling back transaction's changes must not reset array's length.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160}
161
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100162// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100163TEST_F(TransactionTest, StaticFieldsTest) {
164 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 StackHandleScope<4> hs(soa.Self());
166 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700167 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800168 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 Handle<mirror::Class> h_klass(
171 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800172 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100173 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
174 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700175 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100176 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100177
178 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700179 ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100180 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700181 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700182 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100183
Mathieu Chartierc7853442015-03-27 14:35:38 -0700184 ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100185 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700186 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100188
Mathieu Chartierc7853442015-03-27 14:35:38 -0700189 ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100190 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700191 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100193
Mathieu Chartierc7853442015-03-27 14:35:38 -0700194 ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100195 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700196 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100198
Mathieu Chartierc7853442015-03-27 14:35:38 -0700199 ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100200 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700201 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100203
Mathieu Chartierc7853442015-03-27 14:35:38 -0700204 ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700206 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100208
Mathieu Chartierc7853442015-03-27 14:35:38 -0700209 ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700211 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700212 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100213
Mathieu Chartierc7853442015-03-27 14:35:38 -0700214 ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100215 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700216 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700217 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100218
Mathieu Chartierc7853442015-03-27 14:35:38 -0700219 ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100220 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100221 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700222 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700223 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100224
225 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 Handle<mirror::Class> object_klass(
227 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800228 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700229 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800230 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100231 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100232
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100233 // Modify fields inside transaction then rollback changes.
Chang Xing16d1dd82017-07-20 17:56:26 -0700234 Runtime::Current()->EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700235 booleanField->SetBoolean<true>(h_klass.Get(), true);
236 byteField->SetByte<true>(h_klass.Get(), 1);
237 charField->SetChar<true>(h_klass.Get(), 1u);
238 shortField->SetShort<true>(h_klass.Get(), 1);
239 intField->SetInt<true>(h_klass.Get(), 1);
240 longField->SetLong<true>(h_klass.Get(), 1);
241 floatField->SetFloat<true>(h_klass.Get(), 1.0);
242 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
243 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Chang Xing16d1dd82017-07-20 17:56:26 -0700244 Runtime::Current()->RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100245
246 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700247 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
248 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
249 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
250 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
251 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
252 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700253 EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
254 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100256}
257
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100258// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100259TEST_F(TransactionTest, InstanceFieldsTest) {
260 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700261 StackHandleScope<5> hs(soa.Self());
262 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700263 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800264 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100265
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 Handle<mirror::Class> h_klass(
267 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800268 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100269 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
270 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700271 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100272 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273
274 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800276 ASSERT_TRUE(h_instance != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100277
278 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700279 ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700281 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100283
Mathieu Chartierc7853442015-03-27 14:35:38 -0700284 ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100285 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700286 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700287 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288
Mathieu Chartierc7853442015-03-27 14:35:38 -0700289 ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700291 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700292 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100293
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294 ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700296 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700297 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298
Mathieu Chartierc7853442015-03-27 14:35:38 -0700299 ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700301 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700302 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100303
Mathieu Chartierc7853442015-03-27 14:35:38 -0700304 ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100305 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700306 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700307 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100308
Mathieu Chartierc7853442015-03-27 14:35:38 -0700309 ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700311 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700312 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100313
Mathieu Chartierc7853442015-03-27 14:35:38 -0700314 ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700316 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700317 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100318
Mathieu Chartierc7853442015-03-27 14:35:38 -0700319 ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800320 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100321 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700322 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700323 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100324
325 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700326 Handle<mirror::Class> object_klass(
327 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800328 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700329 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800330 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100331 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100332
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100333 // Modify fields inside transaction then rollback changes.
Chang Xing16d1dd82017-07-20 17:56:26 -0700334 Runtime::Current()->EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700335 booleanField->SetBoolean<true>(h_instance.Get(), true);
336 byteField->SetByte<true>(h_instance.Get(), 1);
337 charField->SetChar<true>(h_instance.Get(), 1u);
338 shortField->SetShort<true>(h_instance.Get(), 1);
339 intField->SetInt<true>(h_instance.Get(), 1);
340 longField->SetLong<true>(h_instance.Get(), 1);
341 floatField->SetFloat<true>(h_instance.Get(), 1.0);
342 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
343 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Chang Xing16d1dd82017-07-20 17:56:26 -0700344 Runtime::Current()->RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100345
346 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700347 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
348 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
349 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
350 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
351 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
352 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700353 EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
354 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700355 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100356}
357
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100358// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100359TEST_F(TransactionTest, StaticArrayFieldsTest) {
360 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko4617d582019-03-28 13:48:31 +0000361 StackHandleScope<13> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700362 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700363 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800364 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700366 Handle<mirror::Class> h_klass(
367 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800368 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100369 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
370 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700371 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100372 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100373
374 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700375 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100376 ASSERT_TRUE(booleanArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000377 Handle<mirror::BooleanArray> booleanArray = hs.NewHandle(
378 booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100379 ASSERT_TRUE(booleanArray != nullptr);
380 ASSERT_EQ(booleanArray->GetLength(), 1);
381 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
382
Mathieu Chartierc7853442015-03-27 14:35:38 -0700383 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100384 ASSERT_TRUE(byteArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000385 Handle<mirror::ByteArray> byteArray =
386 hs.NewHandle(byteArrayField->GetObject(h_klass.Get())->AsByteArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100387 ASSERT_TRUE(byteArray != nullptr);
388 ASSERT_EQ(byteArray->GetLength(), 1);
389 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
390
Mathieu Chartierc7853442015-03-27 14:35:38 -0700391 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100392 ASSERT_TRUE(charArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000393 Handle<mirror::CharArray> charArray =
394 hs.NewHandle(charArrayField->GetObject(h_klass.Get())->AsCharArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100395 ASSERT_TRUE(charArray != nullptr);
396 ASSERT_EQ(charArray->GetLength(), 1);
397 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
398
Mathieu Chartierc7853442015-03-27 14:35:38 -0700399 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100400 ASSERT_TRUE(shortArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000401 Handle<mirror::ShortArray> shortArray =
402 hs.NewHandle(shortArrayField->GetObject(h_klass.Get())->AsShortArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100403 ASSERT_TRUE(shortArray != nullptr);
404 ASSERT_EQ(shortArray->GetLength(), 1);
405 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
406
Mathieu Chartierc7853442015-03-27 14:35:38 -0700407 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100408 ASSERT_TRUE(intArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000409 Handle<mirror::IntArray> intArray =
410 hs.NewHandle(intArrayField->GetObject(h_klass.Get())->AsIntArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100411 ASSERT_TRUE(intArray != nullptr);
412 ASSERT_EQ(intArray->GetLength(), 1);
413 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
414
Mathieu Chartierc7853442015-03-27 14:35:38 -0700415 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100416 ASSERT_TRUE(longArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000417 Handle<mirror::LongArray> longArray =
418 hs.NewHandle(longArrayField->GetObject(h_klass.Get())->AsLongArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100419 ASSERT_TRUE(longArray != nullptr);
420 ASSERT_EQ(longArray->GetLength(), 1);
421 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
422
Mathieu Chartierc7853442015-03-27 14:35:38 -0700423 ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100424 ASSERT_TRUE(floatArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000425 Handle<mirror::FloatArray> floatArray =
426 hs.NewHandle(floatArrayField->GetObject(h_klass.Get())->AsFloatArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100427 ASSERT_TRUE(floatArray != nullptr);
428 ASSERT_EQ(floatArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700429 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100430
Mathieu Chartierc7853442015-03-27 14:35:38 -0700431 ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100432 ASSERT_TRUE(doubleArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000433 Handle<mirror::DoubleArray> doubleArray =
434 hs.NewHandle(doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100435 ASSERT_TRUE(doubleArray != nullptr);
436 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700437 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100438
Vladimir Marko4617d582019-03-28 13:48:31 +0000439 ArtField* objectArrayField =
440 h_klass->FindDeclaredStaticField("objectArrayField", "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100441 ASSERT_TRUE(objectArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000442 Handle<mirror::ObjectArray<mirror::Object>> objectArray =
443 hs.NewHandle(objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100444 ASSERT_TRUE(objectArray != nullptr);
445 ASSERT_EQ(objectArray->GetLength(), 1);
446 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
447
448 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700449 Handle<mirror::Class> object_klass(
450 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800451 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700452 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800453 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100454 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100455
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100456 // Modify fields inside transaction then rollback changes.
Chang Xing16d1dd82017-07-20 17:56:26 -0700457 Runtime::Current()->EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100458 booleanArray->SetWithoutChecks<true>(0, true);
459 byteArray->SetWithoutChecks<true>(0, 1);
460 charArray->SetWithoutChecks<true>(0, 1u);
461 shortArray->SetWithoutChecks<true>(0, 1);
462 intArray->SetWithoutChecks<true>(0, 1);
463 longArray->SetWithoutChecks<true>(0, 1);
464 floatArray->SetWithoutChecks<true>(0, 1.0);
465 doubleArray->SetWithoutChecks<true>(0, 1.0);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700466 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Chang Xing16d1dd82017-07-20 17:56:26 -0700467 Runtime::Current()->RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100468
469 // Check values have properly been restored to their original (default) value.
470 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
471 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
472 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
473 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
474 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
475 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700476 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
477 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100478 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
479}
480
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700481// Tests rolling back interned strings and resolved strings.
482TEST_F(TransactionTest, ResolveString) {
483 ScopedObjectAccess soa(Thread::Current());
484 StackHandleScope<3> hs(soa.Self());
485 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700486 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800487 ASSERT_TRUE(class_loader != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700488
489 Handle<mirror::Class> h_klass(
490 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;",
491 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800492 ASSERT_TRUE(h_klass != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700493
494 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800495 ASSERT_TRUE(h_dex_cache != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700496 const DexFile* const dex_file = h_dex_cache->GetDexFile();
497 ASSERT_TRUE(dex_file != nullptr);
498
499 // Go search the dex file to find the string id of our string.
500 static const char* kResolvedString = "ResolvedString";
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800501 const dex::StringId* string_id = dex_file->FindStringId(kResolvedString);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700502 ASSERT_TRUE(string_id != nullptr);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800503 dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
504 ASSERT_TRUE(string_idx.IsValid());
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700505 // String should only get resolved by the initializer.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000506 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700507 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
508 // Do the transaction, then roll back.
Chang Xing16d1dd82017-07-20 17:56:26 -0700509 Runtime::Current()->EnterTransactionMode();
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700510 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
511 ASSERT_TRUE(success);
512 ASSERT_TRUE(h_klass->IsInitialized());
513 // Make sure the string got resolved by the transaction.
514 {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000515 ObjPtr<mirror::String> s =
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000516 class_linker_->LookupString(string_idx, h_dex_cache.Get());
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700517 ASSERT_TRUE(s != nullptr);
518 EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
Vladimir Markobcf17522018-06-01 13:14:32 +0100519 EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700520 }
Chang Xing16d1dd82017-07-20 17:56:26 -0700521 Runtime::Current()->RollbackAndExitTransactionMode();
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700522 // Check that the string did not stay resolved.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000523 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700524 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
525 ASSERT_FALSE(h_klass->IsInitialized());
526 ASSERT_FALSE(soa.Self()->IsExceptionPending());
527}
528
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100529// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100530TEST_F(TransactionTest, EmptyClass) {
531 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700532 StackHandleScope<2> hs(soa.Self());
533 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700534 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800535 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100536
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700537 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100538 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
539 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800540 ASSERT_TRUE(h_klass != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700541 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700542 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100543
Chang Xing16d1dd82017-07-20 17:56:26 -0700544 Runtime::Current()->EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100545 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100546 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100547 ASSERT_TRUE(success);
548 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100549 ASSERT_FALSE(soa.Self()->IsExceptionPending());
550}
551
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100552// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100553TEST_F(TransactionTest, StaticFieldClass) {
554 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700555 StackHandleScope<2> hs(soa.Self());
556 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700557 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800558 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100559
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700560 Handle<mirror::Class> h_klass(
561 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
562 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800563 ASSERT_TRUE(h_klass != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700564 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700565 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100566
Chang Xing16d1dd82017-07-20 17:56:26 -0700567 Runtime::Current()->EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100568 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100569 Runtime::Current()->ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100570 ASSERT_TRUE(success);
571 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100572 ASSERT_FALSE(soa.Self()->IsExceptionPending());
573}
574
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100575// Tests failing class initialization due to native call.
576TEST_F(TransactionTest, NativeCallAbortClass) {
577 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100578}
579
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100580// Tests failing class initialization due to native call in a "synchronized" statement
581// (which must catch any exception, do the monitor-exit then re-throw the caught exception).
582TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
583 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
584}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100585
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100586// Tests failing class initialization due to native call, even if an "all" catch handler
587// catches the exception thrown when aborting the transaction.
588TEST_F(TransactionTest, CatchNativeCallAbortClass) {
589 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
590}
591
592// Tests failing class initialization with multiple transaction aborts.
593TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
594 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
595}
596
597// Tests failing class initialization due to allocating instance of finalizable class.
598TEST_F(TransactionTest, FinalizableAbortClass) {
599 testTransactionAbort("LTransaction$FinalizableAbortClass;");
600}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100601} // namespace art