blob: 04aa7cdb524b539ea377af71ef23f73d600505c3 [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 {
Vladimir Marko672c0802019-07-26 13:03:13 +010031 protected:
Vladimir Marko483c41a2021-11-12 12:45:23 +000032 TransactionTest() {
33 this->use_boot_image_ = true; // We need the boot image for this test.
Vladimir Marko672c0802019-07-26 13:03:13 +010034 }
35
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010036 // Tests failing class initialization due to native call with transaction rollback.
37 void testTransactionAbort(const char* tested_class_signature) {
38 ScopedObjectAccess soa(Thread::Current());
39 jobject jclass_loader = LoadDex("Transaction");
40 StackHandleScope<2> hs(soa.Self());
41 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -070042 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080043 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010044
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020045 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
46 // to abort transaction so they can be thrown during class initialization if the transaction
47 // aborts.
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010048 MutableHandle<mirror::Class> h_klass(
49 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
50 "Ljava/lang/ExceptionInInitializerError;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -080051 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010052 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
53 ASSERT_TRUE(h_klass->IsInitialized());
54
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020055 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
Vladimir Marko0685b982021-03-25 11:59:22 +000056 Transaction::kAbortExceptionDescriptor));
Andreas Gampefa4333d2017-02-14 11:10:34 -080057 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010058 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
59 ASSERT_TRUE(h_klass->IsInitialized());
60
61 // Load and verify utility class.
62 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$AbortHelperClass;",
63 class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080064 ASSERT_TRUE(h_klass != nullptr);
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +000065 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010066 ASSERT_TRUE(h_klass->IsVerified());
67
68 // Load and verify tested class.
69 h_klass.Assign(class_linker_->FindClass(soa.Self(), tested_class_signature, class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -080070 ASSERT_TRUE(h_klass != nullptr);
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +000071 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010072 ASSERT_TRUE(h_klass->IsVerified());
73
Vladimir Marko2c64a832018-01-04 11:31:56 +000074 ClassStatus old_status = h_klass->GetStatus();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080075 LockWord old_lock_word = h_klass->GetLockWord(false);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010076
Vladimir Marko672c0802019-07-26 13:03:13 +010077 EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010078 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Vladimir Marko672c0802019-07-26 13:03:13 +010079 ASSERT_TRUE(IsTransactionAborted());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010080 ASSERT_FALSE(success);
81 ASSERT_TRUE(h_klass->IsErroneous());
82 ASSERT_TRUE(soa.Self()->IsExceptionPending());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010083
84 // Check class's monitor get back to its original state without rolling back changes.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -080085 LockWord new_lock_word = h_klass->GetLockWord(false);
86 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010087
88 // Check class status is rolled back properly.
89 soa.Self()->ClearException();
Vladimir Marko672c0802019-07-26 13:03:13 +010090 RollbackAndExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +010091 ASSERT_EQ(old_status, h_klass->GetStatus());
92 }
93};
94
95// Tests object's class is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010096TEST_F(TransactionTest, Object_class) {
97 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070098 StackHandleScope<2> hs(soa.Self());
99 Handle<mirror::Class> h_klass(
100 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800101 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102
Vladimir Marko672c0802019-07-26 13:03:13 +0100103 EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800105 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100106 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100107 // Rolling back transaction's changes must not clear the Object::class field.
Vladimir Marko672c0802019-07-26 13:03:13 +0100108 RollbackAndExitTransactionMode();
Vladimir Markodfc0de72019-04-01 10:57:55 +0100109 EXPECT_OBJ_PTR_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;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800118 ASSERT_TRUE(h_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800120 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100121 ASSERT_OBJ_PTR_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
Vladimir Marko672c0802019-07-26 13:03:13 +0100127 EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128 // 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 Hertz1c80bec2015-02-03 11:58:06 +0100131 // Rolling back transaction's changes must not change monitor's state.
Vladimir Marko672c0802019-07-26 13:03:13 +0100132 RollbackAndExitTransactionMode();
Chang Xing16d1dd82017-07-20 17:56:26 -0700133
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800134 LockWord aborted_lock_word = h_obj->GetLockWord(false);
135 EXPECT_FALSE(LockWord::Equal<false>(old_lock_word, new_lock_word));
136 EXPECT_TRUE(LockWord::Equal<false>(aborted_lock_word, new_lock_word));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137}
138
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100139// Tests array's length is preserved after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140TEST_F(TransactionTest, Array_length) {
141 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700142 StackHandleScope<2> hs(soa.Self());
143 Handle<mirror::Class> h_klass(
144 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800145 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146
147 constexpr int32_t kArraySize = 2;
148
Vladimir Marko672c0802019-07-26 13:03:13 +0100149 EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150
151 // Allocate an array during transaction.
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100152 Handle<mirror::Array> h_obj = hs.NewHandle(
153 mirror::Array::Alloc(soa.Self(),
154 h_klass.Get(),
155 kArraySize,
156 h_klass->GetComponentSizeShift(),
157 Runtime::Current()->GetHeap()->GetCurrentAllocator()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800158 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100159 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Vladimir Marko672c0802019-07-26 13:03:13 +0100160 RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100161
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100162 // Rolling back transaction's changes must not reset array's length.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700163 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100164}
165
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100166// Tests static fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167TEST_F(TransactionTest, StaticFieldsTest) {
168 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 StackHandleScope<4> hs(soa.Self());
170 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700171 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800172 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100173
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 Handle<mirror::Class> h_klass(
175 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800176 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100177 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
178 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700179 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100180 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100181
182 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700183 ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100184 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700185 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700186 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100187
Mathieu Chartierc7853442015-03-27 14:35:38 -0700188 ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100189 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700190 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700191 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100192
Mathieu Chartierc7853442015-03-27 14:35:38 -0700193 ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700195 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700196 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100197
Mathieu Chartierc7853442015-03-27 14:35:38 -0700198 ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700200 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700201 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100202
Mathieu Chartierc7853442015-03-27 14:35:38 -0700203 ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100204 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700205 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207
Mathieu Chartierc7853442015-03-27 14:35:38 -0700208 ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100209 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700210 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700211 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100212
Mathieu Chartierc7853442015-03-27 14:35:38 -0700213 ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700215 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700216 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217
Mathieu Chartierc7853442015-03-27 14:35:38 -0700218 ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700220 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700221 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222
Mathieu Chartierc7853442015-03-27 14:35:38 -0700223 ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100224 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100225 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700226 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700227 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100228
229 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700230 Handle<mirror::Class> object_klass(
231 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800232 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700233 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800234 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100235 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100236
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100237 // Modify fields inside transaction then rollback changes.
Vladimir Marko672c0802019-07-26 13:03:13 +0100238 EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700239 booleanField->SetBoolean<true>(h_klass.Get(), true);
240 byteField->SetByte<true>(h_klass.Get(), 1);
241 charField->SetChar<true>(h_klass.Get(), 1u);
242 shortField->SetShort<true>(h_klass.Get(), 1);
243 intField->SetInt<true>(h_klass.Get(), 1);
244 longField->SetLong<true>(h_klass.Get(), 1);
245 floatField->SetFloat<true>(h_klass.Get(), 1.0);
246 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
247 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Vladimir Marko672c0802019-07-26 13:03:13 +0100248 RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100249
250 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700251 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
252 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
253 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
254 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
255 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
256 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700257 EXPECT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
258 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700259 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100260}
261
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100262// Tests instance fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100263TEST_F(TransactionTest, InstanceFieldsTest) {
264 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700265 StackHandleScope<5> hs(soa.Self());
266 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700267 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800268 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100269
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700270 Handle<mirror::Class> h_klass(
271 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800272 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100273 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
274 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100276 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100277
278 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700279 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800280 ASSERT_TRUE(h_instance != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100281
282 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700283 ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100284 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700285 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700286 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287
Mathieu Chartierc7853442015-03-27 14:35:38 -0700288 ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100289 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700290 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700291 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292
Mathieu Chartierc7853442015-03-27 14:35:38 -0700293 ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700295 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700296 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100297
Mathieu Chartierc7853442015-03-27 14:35:38 -0700298 ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100299 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700300 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700301 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100302
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303 ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100304 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700305 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700306 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100307
Mathieu Chartierc7853442015-03-27 14:35:38 -0700308 ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100309 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700310 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700311 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100312
Mathieu Chartierc7853442015-03-27 14:35:38 -0700313 ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100314 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700315 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Ian Rogers647b1a82014-10-10 11:02:11 -0700316 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100317
Mathieu Chartierc7853442015-03-27 14:35:38 -0700318 ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100319 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700320 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Ian Rogers647b1a82014-10-10 11:02:11 -0700321 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100322
Mathieu Chartierc7853442015-03-27 14:35:38 -0700323 ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800324 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100325 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700326 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700327 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100328
329 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700330 Handle<mirror::Class> object_klass(
331 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800332 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700333 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800334 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100335 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100337 // Modify fields inside transaction then rollback changes.
Vladimir Marko672c0802019-07-26 13:03:13 +0100338 EnterTransactionMode();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700339 booleanField->SetBoolean<true>(h_instance.Get(), true);
340 byteField->SetByte<true>(h_instance.Get(), 1);
341 charField->SetChar<true>(h_instance.Get(), 1u);
342 shortField->SetShort<true>(h_instance.Get(), 1);
343 intField->SetInt<true>(h_instance.Get(), 1);
344 longField->SetLong<true>(h_instance.Get(), 1);
345 floatField->SetFloat<true>(h_instance.Get(), 1.0);
346 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
347 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Vladimir Marko672c0802019-07-26 13:03:13 +0100348 RollbackAndExitTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100349
350 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700351 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
352 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
353 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
354 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
355 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
356 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Ian Rogers647b1a82014-10-10 11:02:11 -0700357 EXPECT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
358 EXPECT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700359 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100360}
361
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100362// Tests static array fields are reset to their default value after transaction rollback.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100363TEST_F(TransactionTest, StaticArrayFieldsTest) {
364 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko4617d582019-03-28 13:48:31 +0000365 StackHandleScope<13> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700366 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700367 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800368 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100369
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700370 Handle<mirror::Class> h_klass(
371 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800372 ASSERT_TRUE(h_klass != nullptr);
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100373 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
374 ASSERT_TRUE(success);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700375 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100376 ASSERT_FALSE(soa.Self()->IsExceptionPending());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100377
378 // Lookup fields.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700379 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100380 ASSERT_TRUE(booleanArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000381 Handle<mirror::BooleanArray> booleanArray = hs.NewHandle(
382 booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100383 ASSERT_TRUE(booleanArray != nullptr);
384 ASSERT_EQ(booleanArray->GetLength(), 1);
385 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
386
Mathieu Chartierc7853442015-03-27 14:35:38 -0700387 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100388 ASSERT_TRUE(byteArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000389 Handle<mirror::ByteArray> byteArray =
390 hs.NewHandle(byteArrayField->GetObject(h_klass.Get())->AsByteArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100391 ASSERT_TRUE(byteArray != nullptr);
392 ASSERT_EQ(byteArray->GetLength(), 1);
393 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
394
Mathieu Chartierc7853442015-03-27 14:35:38 -0700395 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100396 ASSERT_TRUE(charArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000397 Handle<mirror::CharArray> charArray =
398 hs.NewHandle(charArrayField->GetObject(h_klass.Get())->AsCharArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100399 ASSERT_TRUE(charArray != nullptr);
400 ASSERT_EQ(charArray->GetLength(), 1);
401 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
402
Mathieu Chartierc7853442015-03-27 14:35:38 -0700403 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100404 ASSERT_TRUE(shortArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000405 Handle<mirror::ShortArray> shortArray =
406 hs.NewHandle(shortArrayField->GetObject(h_klass.Get())->AsShortArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100407 ASSERT_TRUE(shortArray != nullptr);
408 ASSERT_EQ(shortArray->GetLength(), 1);
409 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
410
Mathieu Chartierc7853442015-03-27 14:35:38 -0700411 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100412 ASSERT_TRUE(intArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000413 Handle<mirror::IntArray> intArray =
414 hs.NewHandle(intArrayField->GetObject(h_klass.Get())->AsIntArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100415 ASSERT_TRUE(intArray != nullptr);
416 ASSERT_EQ(intArray->GetLength(), 1);
417 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
418
Mathieu Chartierc7853442015-03-27 14:35:38 -0700419 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100420 ASSERT_TRUE(longArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000421 Handle<mirror::LongArray> longArray =
422 hs.NewHandle(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);
Vladimir Marko4617d582019-03-28 13:48:31 +0000429 Handle<mirror::FloatArray> floatArray =
430 hs.NewHandle(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);
Vladimir Marko4617d582019-03-28 13:48:31 +0000437 Handle<mirror::DoubleArray> doubleArray =
438 hs.NewHandle(doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100439 ASSERT_TRUE(doubleArray != nullptr);
440 ASSERT_EQ(doubleArray->GetLength(), 1);
Ian Rogers647b1a82014-10-10 11:02:11 -0700441 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100442
Vladimir Marko4617d582019-03-28 13:48:31 +0000443 ArtField* objectArrayField =
444 h_klass->FindDeclaredStaticField("objectArrayField", "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100445 ASSERT_TRUE(objectArrayField != nullptr);
Vladimir Marko4617d582019-03-28 13:48:31 +0000446 Handle<mirror::ObjectArray<mirror::Object>> objectArray =
447 hs.NewHandle(objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100448 ASSERT_TRUE(objectArray != nullptr);
449 ASSERT_EQ(objectArray->GetLength(), 1);
450 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
451
452 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700453 Handle<mirror::Class> object_klass(
454 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800455 ASSERT_TRUE(object_klass != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700456 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800457 ASSERT_TRUE(h_obj != nullptr);
Vladimir Markodfc0de72019-04-01 10:57:55 +0100458 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100459
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100460 // Modify fields inside transaction then rollback changes.
Vladimir Marko672c0802019-07-26 13:03:13 +0100461 EnterTransactionMode();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100462 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());
Vladimir Marko672c0802019-07-26 13:03:13 +0100471 RollbackAndExitTransactionMode();
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
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700485// Tests rolling back interned strings and resolved strings.
486TEST_F(TransactionTest, ResolveString) {
487 ScopedObjectAccess soa(Thread::Current());
488 StackHandleScope<3> hs(soa.Self());
489 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700490 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800491 ASSERT_TRUE(class_loader != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700492
493 Handle<mirror::Class> h_klass(
494 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;",
495 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800496 ASSERT_TRUE(h_klass != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700497
498 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800499 ASSERT_TRUE(h_dex_cache != nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700500 const DexFile* const dex_file = h_dex_cache->GetDexFile();
501 ASSERT_TRUE(dex_file != nullptr);
502
503 // Go search the dex file to find the string id of our string.
504 static const char* kResolvedString = "ResolvedString";
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800505 const dex::StringId* string_id = dex_file->FindStringId(kResolvedString);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700506 ASSERT_TRUE(string_id != nullptr);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800507 dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
508 ASSERT_TRUE(string_idx.IsValid());
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700509 // String should only get resolved by the initializer.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000510 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700511 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
512 // Do the transaction, then roll back.
Vladimir Marko672c0802019-07-26 13:03:13 +0100513 EnterTransactionMode();
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700514 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
515 ASSERT_TRUE(success);
516 ASSERT_TRUE(h_klass->IsInitialized());
517 // Make sure the string got resolved by the transaction.
518 {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000519 ObjPtr<mirror::String> s =
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000520 class_linker_->LookupString(string_idx, h_dex_cache.Get());
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700521 ASSERT_TRUE(s != nullptr);
522 EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
Vladimir Markobcf17522018-06-01 13:14:32 +0100523 EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx));
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700524 }
Vladimir Marko672c0802019-07-26 13:03:13 +0100525 RollbackAndExitTransactionMode();
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700526 // Check that the string did not stay resolved.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000527 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
Mathieu Chartierbb816d62016-09-07 10:17:46 -0700528 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
529 ASSERT_FALSE(h_klass->IsInitialized());
530 ASSERT_FALSE(soa.Self()->IsExceptionPending());
531}
532
Vladimir Markoe9a4a602021-06-22 16:33:34 +0100533// Tests rolling back resolved method types in dex cache.
534TEST_F(TransactionTest, ResolveMethodType) {
535 ScopedObjectAccess soa(Thread::Current());
536 StackHandleScope<3> hs(soa.Self());
537 Handle<mirror::ClassLoader> class_loader(
538 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
539 ASSERT_TRUE(class_loader != nullptr);
540
541 Handle<mirror::Class> h_klass(
542 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction;", class_loader)));
543 ASSERT_TRUE(h_klass != nullptr);
544
545 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
546 ASSERT_TRUE(h_dex_cache != nullptr);
547 const DexFile* const dex_file = h_dex_cache->GetDexFile();
548 ASSERT_TRUE(dex_file != nullptr);
549
550 ASSERT_NE(dex_file->NumProtoIds(), 0u);
551 dex::ProtoIndex proto_index(0u);
552 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == nullptr);
553
554 // Do the transaction, then roll back.
555 EnterTransactionMode();
556 ObjPtr<mirror::MethodType> method_type =
557 class_linker_->ResolveMethodType(soa.Self(), proto_index, h_dex_cache, class_loader);
558 ASSERT_TRUE(method_type != nullptr);
559 // Make sure the method type was recorded in the dex cache.
560 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == method_type);
561 RollbackAndExitTransactionMode();
562 // Check that the method type was removed from the dex cache.
563 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == nullptr);
564}
565
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100566// Tests successful class initialization without class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100567TEST_F(TransactionTest, EmptyClass) {
568 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700569 StackHandleScope<2> hs(soa.Self());
570 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700571 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800572 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100573
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700574 Handle<mirror::Class> h_klass(
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100575 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
576 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800577 ASSERT_TRUE(h_klass != nullptr);
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000578 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700579 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100580
Vladimir Marko672c0802019-07-26 13:03:13 +0100581 EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100582 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Vladimir Marko672c0802019-07-26 13:03:13 +0100583 ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100584 ASSERT_TRUE(success);
585 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100586 ASSERT_FALSE(soa.Self()->IsExceptionPending());
587}
588
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100589// Tests successful class initialization with class initializer.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100590TEST_F(TransactionTest, StaticFieldClass) {
591 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700592 StackHandleScope<2> hs(soa.Self());
593 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700594 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800595 ASSERT_TRUE(class_loader != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100596
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700597 Handle<mirror::Class> h_klass(
598 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
599 class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800600 ASSERT_TRUE(h_klass != nullptr);
Nicolas Geoffray5b0b2e12021-03-19 14:48:40 +0000601 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700602 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100603
Vladimir Marko672c0802019-07-26 13:03:13 +0100604 EnterTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100605 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Vladimir Marko672c0802019-07-26 13:03:13 +0100606 ExitTransactionMode();
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100607 ASSERT_TRUE(success);
608 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100609 ASSERT_FALSE(soa.Self()->IsExceptionPending());
610}
611
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100612// Tests failing class initialization due to native call.
613TEST_F(TransactionTest, NativeCallAbortClass) {
614 testTransactionAbort("LTransaction$NativeCallAbortClass;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100615}
616
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100617// Tests failing class initialization due to native call in a "synchronized" statement
618// (which must catch any exception, do the monitor-exit then re-throw the caught exception).
619TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
620 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
621}
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100622
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100623// Tests failing class initialization due to native call, even if an "all" catch handler
624// catches the exception thrown when aborting the transaction.
625TEST_F(TransactionTest, CatchNativeCallAbortClass) {
626 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
627}
628
629// Tests failing class initialization with multiple transaction aborts.
630TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
631 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
632}
633
Vladimir Marko0685b982021-03-25 11:59:22 +0000634// Tests failing class initialization due to Class.forName() not finding the class,
635// even if an "all" catch handler catches the exception thrown when aborting the transaction.
636TEST_F(TransactionTest, CatchClassForNameAbortClass) {
637 testTransactionAbort("LTransaction$CatchClassForNameAbortClass;");
638}
639
640// Same as CatchClassForNameAbortClass but the class initializer tries to do the work twice.
641// This would trigger a DCHECK() if we continued executing bytecode with an aborted transaction.
642TEST_F(TransactionTest, CatchClassForNameAbortClassTwice) {
643 testTransactionAbort("LTransaction$CatchClassForNameAbortClassTwice;");
644}
645
Sebastien Hertz1c80bec2015-02-03 11:58:06 +0100646// Tests failing class initialization due to allocating instance of finalizable class.
647TEST_F(TransactionTest, FinalizableAbortClass) {
648 testTransactionAbort("LTransaction$FinalizableAbortClass;");
649}
Vladimir Marko672c0802019-07-26 13:03:13 +0100650
651TEST_F(TransactionTest, Constraints) {
652 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000653 StackHandleScope<11> hs(soa.Self());
Vladimir Marko672c0802019-07-26 13:03:13 +0100654 Handle<mirror::ClassLoader> class_loader(
655 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
656
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000657 gc::Heap* heap = Runtime::Current()->GetHeap();
Vladimir Marko672c0802019-07-26 13:03:13 +0100658 Handle<mirror::Class> boolean_class = hs.NewHandle(
659 class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000660 ASSERT_TRUE(boolean_class != nullptr);
661 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(boolean_class.Get()));
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100662 ArtField* true_field = boolean_class->FindDeclaredStaticField("TRUE", "Ljava/lang/Boolean;");
Vladimir Marko672c0802019-07-26 13:03:13 +0100663 ASSERT_TRUE(true_field != nullptr);
664 ASSERT_TRUE(true_field->IsStatic());
665 Handle<mirror::Object> true_value = hs.NewHandle(true_field->GetObject(boolean_class.Get()));
666 ASSERT_TRUE(true_value != nullptr);
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000667 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(true_value.Get()));
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100668 ArtField* value_field = boolean_class->FindDeclaredInstanceField("value", "Z");
Vladimir Marko672c0802019-07-26 13:03:13 +0100669 ASSERT_TRUE(value_field != nullptr);
670 ASSERT_FALSE(value_field->IsStatic());
671
672 Handle<mirror::Class> static_field_class(hs.NewHandle(
673 class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;", class_loader)));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000674 ASSERT_TRUE(static_field_class != nullptr);
675 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(static_field_class.Get()));
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100676 ArtField* int_field = static_field_class->FindDeclaredStaticField("intField", "I");
Vladimir Marko672c0802019-07-26 13:03:13 +0100677 ASSERT_TRUE(int_field != nullptr);
678
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000679 Handle<mirror::Class> static_fields_test_class(hs.NewHandle(
680 class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
681 ASSERT_TRUE(static_fields_test_class != nullptr);
682 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(static_fields_test_class.Get()));
683 ArtField* static_fields_test_int_field =
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100684 static_fields_test_class->FindDeclaredStaticField("intField", "I");
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000685 ASSERT_TRUE(static_fields_test_int_field != nullptr);
686
687 Handle<mirror::Class> instance_fields_test_class(hs.NewHandle(
688 class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
689 ASSERT_TRUE(instance_fields_test_class != nullptr);
690 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(instance_fields_test_class.Get()));
691 ArtField* instance_fields_test_int_field =
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100692 instance_fields_test_class->FindDeclaredInstanceField("intField", "I");
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000693 ASSERT_TRUE(instance_fields_test_int_field != nullptr);
694 Handle<mirror::Object> instance_fields_test_object = hs.NewHandle(
695 instance_fields_test_class->Alloc(soa.Self(), heap->GetCurrentAllocator()));
696 ASSERT_TRUE(instance_fields_test_object != nullptr);
697 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(instance_fields_test_object.Get()));
698
Vladimir Markoa982cd62020-08-17 14:13:57 +0000699 // The `long[].class` should be in the boot image but `long[][][].class` should not.
700 // (We have seen `long[][].class` both present and missing from the boot image,
701 // depending on the libcore code, so we do not use it for this test.)
702 Handle<mirror::Class> long_array_dim3_class = hs.NewHandle(
703 class_linker_->FindClass(soa.Self(), "[[[J", class_loader));
704 ASSERT_TRUE(long_array_dim3_class != nullptr);
705 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3_class.Get()));
706 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(
707 long_array_dim3_class->GetComponentType()->GetComponentType()));
708 Handle<mirror::Array> long_array_dim3 = hs.NewHandle(mirror::Array::Alloc(
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000709 soa.Self(),
Vladimir Markoa982cd62020-08-17 14:13:57 +0000710 long_array_dim3_class.Get(),
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000711 /*component_count=*/ 1,
Vladimir Markoa982cd62020-08-17 14:13:57 +0000712 long_array_dim3_class->GetComponentSizeShift(),
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000713 heap->GetCurrentAllocator()));
Vladimir Markoa982cd62020-08-17 14:13:57 +0000714 ASSERT_TRUE(long_array_dim3 != nullptr);
715 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000716 Handle<mirror::Array> long_array = hs.NewHandle(mirror::Array::Alloc(
717 soa.Self(),
Vladimir Markoa982cd62020-08-17 14:13:57 +0000718 long_array_dim3_class->GetComponentType()->GetComponentType(),
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000719 /*component_count=*/ 1,
Vladimir Markoa982cd62020-08-17 14:13:57 +0000720 long_array_dim3_class->GetComponentType()->GetComponentType()->GetComponentSizeShift(),
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000721 heap->GetCurrentAllocator()));
722 ASSERT_TRUE(long_array != nullptr);
723 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array.Get()));
724
725 // Use the Array's IfTable as an array from the boot image.
726 Handle<mirror::ObjectArray<mirror::Object>> array_iftable =
Vladimir Markoa982cd62020-08-17 14:13:57 +0000727 hs.NewHandle(long_array_dim3_class->GetIfTable());
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000728 ASSERT_TRUE(array_iftable != nullptr);
729 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(array_iftable.Get()));
Vladimir Marko149cdda2019-11-12 15:02:51 +0000730
Vladimir Marko672c0802019-07-26 13:03:13 +0100731 // Test non-strict transaction.
Vladimir Markob6e67922021-07-06 15:53:07 +0100732 ArenaPool* arena_pool = Runtime::Current()->GetArenaPool();
733 Transaction transaction(
734 /*strict=*/ false, /*root=*/ nullptr, /*arena_stack=*/ nullptr, arena_pool);
Vladimir Marko672c0802019-07-26 13:03:13 +0100735 // Static field in boot image.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100736 EXPECT_TRUE(transaction.WriteConstraint(boolean_class.Get()));
737 EXPECT_FALSE(transaction.ReadConstraint(boolean_class.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000738 // Instance field or array element in boot image.
739 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
Vladimir Marko24c080f2021-07-06 14:04:30 +0100740 EXPECT_TRUE(transaction.WriteConstraint(true_value.Get()));
741 EXPECT_TRUE(transaction.WriteConstraint(array_iftable.Get()));
Vladimir Marko672c0802019-07-26 13:03:13 +0100742 // Static field not in boot image.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100743 EXPECT_FALSE(transaction.WriteConstraint(static_fields_test_class.Get()));
744 EXPECT_FALSE(transaction.ReadConstraint(static_fields_test_class.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000745 // Instance field or array element not in boot image.
746 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
Vladimir Marko24c080f2021-07-06 14:04:30 +0100747 EXPECT_FALSE(transaction.WriteConstraint(instance_fields_test_object.Get()));
748 EXPECT_FALSE(transaction.WriteConstraint(long_array_dim3.Get()));
Vladimir Marko149cdda2019-11-12 15:02:51 +0000749 // Write value constraints.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100750 EXPECT_FALSE(transaction.WriteValueConstraint(static_fields_test_class.Get()));
751 EXPECT_FALSE(transaction.WriteValueConstraint(instance_fields_test_object.Get()));
752 EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3->GetClass()));
753 EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3.Get()));
754 EXPECT_FALSE(transaction.WriteValueConstraint(long_array->GetClass()));
755 EXPECT_FALSE(transaction.WriteValueConstraint(long_array.Get()));
Vladimir Marko672c0802019-07-26 13:03:13 +0100756
757 // Test strict transaction.
Vladimir Markob6e67922021-07-06 15:53:07 +0100758 Transaction strict_transaction(
759 /*strict=*/ true, /*root=*/ static_field_class.Get(), /*arena_stack=*/ nullptr, arena_pool);
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000760 // Static field in boot image.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100761 EXPECT_TRUE(strict_transaction.WriteConstraint(boolean_class.Get()));
762 EXPECT_TRUE(strict_transaction.ReadConstraint(boolean_class.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000763 // Instance field or array element in boot image.
764 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
Vladimir Marko24c080f2021-07-06 14:04:30 +0100765 EXPECT_TRUE(strict_transaction.WriteConstraint(true_value.Get()));
766 EXPECT_TRUE(strict_transaction.WriteConstraint(array_iftable.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000767 // Static field in another class not in boot image.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100768 EXPECT_TRUE(strict_transaction.WriteConstraint(static_fields_test_class.Get()));
769 EXPECT_TRUE(strict_transaction.ReadConstraint(static_fields_test_class.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000770 // Instance field or array element not in boot image.
771 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
Vladimir Marko24c080f2021-07-06 14:04:30 +0100772 EXPECT_FALSE(strict_transaction.WriteConstraint(instance_fields_test_object.Get()));
773 EXPECT_FALSE(strict_transaction.WriteConstraint(long_array_dim3.Get()));
Vladimir Marko672c0802019-07-26 13:03:13 +0100774 // Static field in the same class.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100775 EXPECT_FALSE(strict_transaction.WriteConstraint(static_field_class.Get()));
776 EXPECT_FALSE(strict_transaction.ReadConstraint(static_field_class.Get()));
Vladimir Marko149cdda2019-11-12 15:02:51 +0000777 // Write value constraints.
Vladimir Marko24c080f2021-07-06 14:04:30 +0100778 EXPECT_FALSE(strict_transaction.WriteValueConstraint(static_fields_test_class.Get()));
779 EXPECT_FALSE(strict_transaction.WriteValueConstraint(instance_fields_test_object.Get()));
Vladimir Marko4d7b6892020-01-16 17:06:35 +0000780 // TODO: The following may be revised, see a TODO in Transaction::WriteValueConstraint().
Vladimir Marko24c080f2021-07-06 14:04:30 +0100781 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3->GetClass()));
782 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3.Get()));
783 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array->GetClass()));
784 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array.Get()));
Vladimir Marko672c0802019-07-26 13:03:13 +0100785}
786
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100787} // namespace art