1 /*
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
17 #include "transaction.h"
18
19 #include "art_field-inl.h"
20 #include "art_method-inl.h"
21 #include "class_linker-inl.h"
22 #include "common_transaction_test.h"
23 #include "common_throws.h"
24 #include "dex/dex_file.h"
25 #include "mirror/array-alloc-inl.h"
26 #include "mirror/class-alloc-inl.h"
27 #include "scoped_thread_state_change-inl.h"
28
29 namespace art HIDDEN {
30
31 class TransactionTest : public CommonTransactionTest {
32 protected:
TransactionTest()33 TransactionTest() {
34 this->use_boot_image_ = true; // We need the boot image for this test.
35 }
36
37 // Tests failing class initialization due to native call with transaction rollback.
testTransactionAbort(const char * tested_class_signature)38 void testTransactionAbort(const char* tested_class_signature) {
39 ScopedObjectAccess soa(Thread::Current());
40 jobject jclass_loader = LoadDex("Transaction");
41 StackHandleScope<2> hs(soa.Self());
42 Handle<mirror::ClassLoader> class_loader(
43 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
44 ASSERT_TRUE(class_loader != nullptr);
45
46 // Load and initialize java.lang.ExceptionInInitializerError and the exception class used
47 // to abort transaction so they can be thrown during class initialization if the transaction
48 // aborts.
49 MutableHandle<mirror::Class> h_klass(
50 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
51 "Ljava/lang/ExceptionInInitializerError;")));
52 ASSERT_TRUE(h_klass != nullptr);
53 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
54 ASSERT_TRUE(h_klass->IsInitialized());
55
56 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(), kTransactionAbortErrorDescriptor));
57 ASSERT_TRUE(h_klass != nullptr);
58 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));
64 ASSERT_TRUE(h_klass != nullptr);
65 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
66 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));
70 ASSERT_TRUE(h_klass != nullptr);
71 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
72 ASSERT_TRUE(h_klass->IsVerified());
73
74 ClassStatus old_status = h_klass->GetStatus();
75 LockWord old_lock_word = h_klass->GetLockWord(false);
76
77 EnterTransactionMode();
78 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
79 ASSERT_TRUE(IsTransactionAborted());
80 ASSERT_FALSE(success);
81 ASSERT_TRUE(h_klass->IsErroneous());
82 ASSERT_TRUE(soa.Self()->IsExceptionPending());
83
84 // Check class's monitor get back to its original state without rolling back changes.
85 LockWord new_lock_word = h_klass->GetLockWord(false);
86 EXPECT_TRUE(LockWord::Equal<false>(old_lock_word, new_lock_word));
87
88 // Check class status is rolled back properly.
89 soa.Self()->ClearException();
90 RollbackAndExitTransactionMode();
91 ASSERT_EQ(old_status, h_klass->GetStatus());
92 }
93 };
94
95 // Tests object's class is preserved after transaction rollback.
TEST_F(TransactionTest,Object_class)96 TEST_F(TransactionTest, Object_class) {
97 ScopedObjectAccess soa(Thread::Current());
98 StackHandleScope<2> hs(soa.Self());
99 Handle<mirror::Class> h_klass(
100 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
101 ASSERT_TRUE(h_klass != nullptr);
102
103 EnterTransactionMode();
104 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
105 ASSERT_TRUE(h_obj != nullptr);
106 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
107 // Rolling back transaction's changes must not clear the Object::class field.
108 RollbackAndExitTransactionMode();
109 EXPECT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
110 }
111
112 // Tests object's monitor state is preserved after transaction rollback.
TEST_F(TransactionTest,Object_monitor)113 TEST_F(TransactionTest, Object_monitor) {
114 ScopedObjectAccess soa(Thread::Current());
115 StackHandleScope<2> hs(soa.Self());
116 Handle<mirror::Class> h_klass(
117 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
118 ASSERT_TRUE(h_klass != nullptr);
119 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
120 ASSERT_TRUE(h_obj != nullptr);
121 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
122
123 // Lock object's monitor outside the transaction.
124 h_obj->MonitorEnter(soa.Self());
125 LockWord old_lock_word = h_obj->GetLockWord(false);
126
127 EnterTransactionMode();
128 // Unlock object's monitor inside the transaction.
129 h_obj->MonitorExit(soa.Self());
130 LockWord new_lock_word = h_obj->GetLockWord(false);
131 // Rolling back transaction's changes must not change monitor's state.
132 RollbackAndExitTransactionMode();
133
134 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));
137 }
138
139 // Tests array's length is preserved after transaction rollback.
TEST_F(TransactionTest,Array_length)140 TEST_F(TransactionTest, Array_length) {
141 ScopedObjectAccess soa(Thread::Current());
142 StackHandleScope<2> hs(soa.Self());
143 Handle<mirror::Class> h_klass(
144 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
145 ASSERT_TRUE(h_klass != nullptr);
146
147 constexpr int32_t kArraySize = 2;
148
149 EnterTransactionMode();
150
151 // Allocate an array during transaction.
152 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()));
158 ASSERT_TRUE(h_obj != nullptr);
159 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
160 RollbackAndExitTransactionMode();
161
162 // Rolling back transaction's changes must not reset array's length.
163 EXPECT_EQ(h_obj->GetLength(), kArraySize);
164 }
165
166 // Tests static fields are reset to their default value after transaction rollback.
TEST_F(TransactionTest,StaticFieldsTest)167 TEST_F(TransactionTest, StaticFieldsTest) {
168 ScopedObjectAccess soa(Thread::Current());
169 StackHandleScope<4> hs(soa.Self());
170 Handle<mirror::ClassLoader> class_loader(
171 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
172 ASSERT_TRUE(class_loader != nullptr);
173
174 Handle<mirror::Class> h_klass(
175 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
176 ASSERT_TRUE(h_klass != nullptr);
177 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
178 ASSERT_TRUE(success);
179 ASSERT_TRUE(h_klass->IsInitialized());
180 ASSERT_FALSE(soa.Self()->IsExceptionPending());
181
182 // Lookup fields.
183 ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
184 ASSERT_TRUE(booleanField != nullptr);
185 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
186 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
187
188 ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
189 ASSERT_TRUE(byteField != nullptr);
190 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
191 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
192
193 ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
194 ASSERT_TRUE(charField != nullptr);
195 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
196 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
197
198 ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
199 ASSERT_TRUE(shortField != nullptr);
200 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
201 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
202
203 ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
204 ASSERT_TRUE(intField != nullptr);
205 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
206 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
207
208 ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
209 ASSERT_TRUE(longField != nullptr);
210 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
211 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
212
213 ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
214 ASSERT_TRUE(floatField != nullptr);
215 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
216 ASSERT_FLOAT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
217
218 ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
219 ASSERT_TRUE(doubleField != nullptr);
220 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
221 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
222
223 ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
224 "Ljava/lang/Object;");
225 ASSERT_TRUE(objectField != nullptr);
226 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
227 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
228
229 // Create a java.lang.Object instance to set objectField.
230 Handle<mirror::Class> object_klass(
231 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
232 ASSERT_TRUE(object_klass != nullptr);
233 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
234 ASSERT_TRUE(h_obj != nullptr);
235 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
236
237 // Modify fields inside transaction then rollback changes.
238 EnterTransactionMode();
239 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());
248 RollbackAndExitTransactionMode();
249
250 // Check values have properly been restored to their original (default) value.
251 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));
257 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));
259 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
260 }
261
262 // Tests instance fields are reset to their default value after transaction rollback.
TEST_F(TransactionTest,InstanceFieldsTest)263 TEST_F(TransactionTest, InstanceFieldsTest) {
264 ScopedObjectAccess soa(Thread::Current());
265 StackHandleScope<5> hs(soa.Self());
266 Handle<mirror::ClassLoader> class_loader(
267 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
268 ASSERT_TRUE(class_loader != nullptr);
269
270 Handle<mirror::Class> h_klass(
271 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
272 ASSERT_TRUE(h_klass != nullptr);
273 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
274 ASSERT_TRUE(success);
275 ASSERT_TRUE(h_klass->IsInitialized());
276 ASSERT_FALSE(soa.Self()->IsExceptionPending());
277
278 // Allocate an InstanceFieldTest object.
279 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
280 ASSERT_TRUE(h_instance != nullptr);
281
282 // Lookup fields.
283 ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
284 ASSERT_TRUE(booleanField != nullptr);
285 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
286 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
287
288 ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
289 ASSERT_TRUE(byteField != nullptr);
290 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
291 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
292
293 ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
294 ASSERT_TRUE(charField != nullptr);
295 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
296 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
297
298 ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
299 ASSERT_TRUE(shortField != nullptr);
300 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
301 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
302
303 ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
304 ASSERT_TRUE(intField != nullptr);
305 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
306 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
307
308 ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
309 ASSERT_TRUE(longField != nullptr);
310 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
311 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
312
313 ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
314 ASSERT_TRUE(floatField != nullptr);
315 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
316 ASSERT_FLOAT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
317
318 ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
319 ASSERT_TRUE(doubleField != nullptr);
320 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
321 ASSERT_DOUBLE_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
322
323 ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
324 "Ljava/lang/Object;");
325 ASSERT_TRUE(objectField != nullptr);
326 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
327 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
328
329 // Create a java.lang.Object instance to set objectField.
330 Handle<mirror::Class> object_klass(
331 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
332 ASSERT_TRUE(object_klass != nullptr);
333 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
334 ASSERT_TRUE(h_obj != nullptr);
335 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
336
337 // Modify fields inside transaction then rollback changes.
338 EnterTransactionMode();
339 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());
348 RollbackAndExitTransactionMode();
349
350 // Check values have properly been restored to their original (default) value.
351 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));
357 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));
359 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
360
361 // Fail to modify fields with strong CAS inside transaction, then rollback changes.
362 EnterTransactionMode();
363 bool cas_success = h_instance->CasField32</*kTransactionActive=*/ true>(
364 intField->GetOffset(),
365 /*old_value=*/ 1,
366 /*new_value=*/ 2,
367 CASMode::kStrong,
368 std::memory_order_seq_cst);
369 EXPECT_FALSE(cas_success);
370 cas_success = h_instance->CasFieldStrongSequentiallyConsistent64</*kTransactionActive=*/ true>(
371 longField->GetOffset(), /*old_value=*/ INT64_C(1), /*new_value=*/ INT64_C(2));
372 EXPECT_FALSE(cas_success);
373 cas_success = h_instance->CasFieldObject</*kTransactionActive=*/ true>(
374 objectField->GetOffset(),
375 /*old_value=*/ h_instance.Get(),
376 /*new_value=*/ nullptr,
377 CASMode::kStrong,
378 std::memory_order_seq_cst);
379 EXPECT_FALSE(cas_success);
380 RollbackAndExitTransactionMode();
381
382 // Check values have properly been restored to their original (default) value.
383 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
384 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
385 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
386
387 // Fail to modify fields with weak CAS inside transaction, then rollback changes.
388 EnterTransactionMode();
389 cas_success = h_instance->CasField32</*kTransactionActive=*/ true>(
390 intField->GetOffset(),
391 /*old_value=*/ 3,
392 /*new_value=*/ 4,
393 CASMode::kWeak,
394 std::memory_order_seq_cst);
395 EXPECT_FALSE(cas_success);
396 cas_success = h_instance->CasFieldWeakSequentiallyConsistent64</*kTransactionActive=*/ true>(
397 longField->GetOffset(), /*old_value=*/ INT64_C(3), /*new_value=*/ INT64_C(4));
398 EXPECT_FALSE(cas_success);
399 cas_success = h_instance->CasFieldObject</*kTransactionActive=*/ true>(
400 objectField->GetOffset(),
401 /*old_value=*/ h_klass.Get(),
402 /*new_value=*/ nullptr,
403 CASMode::kWeak,
404 std::memory_order_seq_cst);
405 EXPECT_FALSE(cas_success);
406 RollbackAndExitTransactionMode();
407
408 // Check values have properly been restored to their original (default) value.
409 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
410 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
411 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
412 }
413
414 // Tests static array fields are reset to their default value after transaction rollback.
TEST_F(TransactionTest,StaticArrayFieldsTest)415 TEST_F(TransactionTest, StaticArrayFieldsTest) {
416 ScopedObjectAccess soa(Thread::Current());
417 StackHandleScope<13> hs(soa.Self());
418 Handle<mirror::ClassLoader> class_loader(
419 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
420 ASSERT_TRUE(class_loader != nullptr);
421
422 Handle<mirror::Class> h_klass(
423 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
424 ASSERT_TRUE(h_klass != nullptr);
425 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
426 ASSERT_TRUE(success);
427 ASSERT_TRUE(h_klass->IsInitialized());
428 ASSERT_FALSE(soa.Self()->IsExceptionPending());
429
430 // Lookup fields.
431 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
432 ASSERT_TRUE(booleanArrayField != nullptr);
433 Handle<mirror::BooleanArray> booleanArray = hs.NewHandle(
434 booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray());
435 ASSERT_TRUE(booleanArray != nullptr);
436 ASSERT_EQ(booleanArray->GetLength(), 1);
437 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
438
439 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
440 ASSERT_TRUE(byteArrayField != nullptr);
441 Handle<mirror::ByteArray> byteArray =
442 hs.NewHandle(byteArrayField->GetObject(h_klass.Get())->AsByteArray());
443 ASSERT_TRUE(byteArray != nullptr);
444 ASSERT_EQ(byteArray->GetLength(), 1);
445 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
446
447 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
448 ASSERT_TRUE(charArrayField != nullptr);
449 Handle<mirror::CharArray> charArray =
450 hs.NewHandle(charArrayField->GetObject(h_klass.Get())->AsCharArray());
451 ASSERT_TRUE(charArray != nullptr);
452 ASSERT_EQ(charArray->GetLength(), 1);
453 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
454
455 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
456 ASSERT_TRUE(shortArrayField != nullptr);
457 Handle<mirror::ShortArray> shortArray =
458 hs.NewHandle(shortArrayField->GetObject(h_klass.Get())->AsShortArray());
459 ASSERT_TRUE(shortArray != nullptr);
460 ASSERT_EQ(shortArray->GetLength(), 1);
461 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
462
463 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
464 ASSERT_TRUE(intArrayField != nullptr);
465 Handle<mirror::IntArray> intArray =
466 hs.NewHandle(intArrayField->GetObject(h_klass.Get())->AsIntArray());
467 ASSERT_TRUE(intArray != nullptr);
468 ASSERT_EQ(intArray->GetLength(), 1);
469 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
470
471 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
472 ASSERT_TRUE(longArrayField != nullptr);
473 Handle<mirror::LongArray> longArray =
474 hs.NewHandle(longArrayField->GetObject(h_klass.Get())->AsLongArray());
475 ASSERT_TRUE(longArray != nullptr);
476 ASSERT_EQ(longArray->GetLength(), 1);
477 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
478
479 ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
480 ASSERT_TRUE(floatArrayField != nullptr);
481 Handle<mirror::FloatArray> floatArray =
482 hs.NewHandle(floatArrayField->GetObject(h_klass.Get())->AsFloatArray());
483 ASSERT_TRUE(floatArray != nullptr);
484 ASSERT_EQ(floatArray->GetLength(), 1);
485 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
486
487 ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
488 ASSERT_TRUE(doubleArrayField != nullptr);
489 Handle<mirror::DoubleArray> doubleArray =
490 hs.NewHandle(doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray());
491 ASSERT_TRUE(doubleArray != nullptr);
492 ASSERT_EQ(doubleArray->GetLength(), 1);
493 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
494
495 ArtField* objectArrayField =
496 h_klass->FindDeclaredStaticField("objectArrayField", "[Ljava/lang/Object;");
497 ASSERT_TRUE(objectArrayField != nullptr);
498 Handle<mirror::ObjectArray<mirror::Object>> objectArray =
499 hs.NewHandle(objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>());
500 ASSERT_TRUE(objectArray != nullptr);
501 ASSERT_EQ(objectArray->GetLength(), 1);
502 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
503
504 // Create a java.lang.Object instance to set objectField.
505 Handle<mirror::Class> object_klass(
506 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
507 ASSERT_TRUE(object_klass != nullptr);
508 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
509 ASSERT_TRUE(h_obj != nullptr);
510 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
511
512 // Modify fields inside transaction then rollback changes.
513 EnterTransactionMode();
514 booleanArray->SetWithoutChecks<true>(0, true);
515 byteArray->SetWithoutChecks<true>(0, 1);
516 charArray->SetWithoutChecks<true>(0, 1u);
517 shortArray->SetWithoutChecks<true>(0, 1);
518 intArray->SetWithoutChecks<true>(0, 1);
519 longArray->SetWithoutChecks<true>(0, 1);
520 floatArray->SetWithoutChecks<true>(0, 1.0);
521 doubleArray->SetWithoutChecks<true>(0, 1.0);
522 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
523 RollbackAndExitTransactionMode();
524
525 // Check values have properly been restored to their original (default) value.
526 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
527 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
528 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
529 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
530 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
531 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
532 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
533 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
534 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
535 }
536
537 // Tests rolling back interned strings and resolved strings.
TEST_F(TransactionTest,ResolveString)538 TEST_F(TransactionTest, ResolveString) {
539 ScopedObjectAccess soa(Thread::Current());
540 StackHandleScope<3> hs(soa.Self());
541 Handle<mirror::ClassLoader> class_loader(
542 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
543 ASSERT_TRUE(class_loader != nullptr);
544
545 Handle<mirror::Class> h_klass(
546 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;",
547 class_loader)));
548 ASSERT_TRUE(h_klass != nullptr);
549
550 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
551 ASSERT_TRUE(h_dex_cache != nullptr);
552 const DexFile* const dex_file = h_dex_cache->GetDexFile();
553 ASSERT_TRUE(dex_file != nullptr);
554
555 // Go search the dex file to find the string id of our string.
556 static const char* kResolvedString = "ResolvedString";
557 const dex::StringId* string_id = dex_file->FindStringId(kResolvedString);
558 ASSERT_TRUE(string_id != nullptr);
559 dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
560 ASSERT_TRUE(string_idx.IsValid());
561 // String should only get resolved by the initializer.
562 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
563 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
564 // Do the transaction, then roll back.
565 EnterTransactionMode();
566 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
567 ASSERT_TRUE(success);
568 ASSERT_TRUE(h_klass->IsInitialized());
569 // Make sure the string got resolved by the transaction.
570 {
571 ObjPtr<mirror::String> s =
572 class_linker_->LookupString(string_idx, h_dex_cache.Get());
573 ASSERT_TRUE(s != nullptr);
574 EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
575 EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx));
576 }
577 RollbackAndExitTransactionMode();
578 // Check that the string did not stay resolved.
579 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
580 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
581 ASSERT_FALSE(h_klass->IsInitialized());
582 ASSERT_FALSE(soa.Self()->IsExceptionPending());
583 }
584
585 class MethodTypeTransactionTest : public TransactionTest {
586 protected:
MethodTypeTransactionTest()587 MethodTypeTransactionTest() {
588 // java.lang.invoke.MethodType factory methods and mirror::MethodType::Create
589 // are backed by the same cache, which is in the primary boot image. As as a
590 // result, MethodType creation can lead to writes to the map under a
591 // transaction, which is forbidden.
592 this->use_boot_image_ = false;
593 }
594 };
595
596 // Tests rolling back resolved method types in dex cache.
TEST_F(MethodTypeTransactionTest,ResolveMethodType)597 TEST_F(MethodTypeTransactionTest, ResolveMethodType) {
598 ScopedObjectAccess soa(Thread::Current());
599 StackHandleScope<3> hs(soa.Self());
600 Handle<mirror::ClassLoader> class_loader(
601 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
602 ASSERT_TRUE(class_loader != nullptr);
603
604 MutableHandle<mirror::Class> h_klass(hs.NewHandle(
605 class_linker_->FindSystemClass(soa.Self(), "Ljava/util/concurrent/ConcurrentHashMap$Node;")));
606 ASSERT_TRUE(h_klass != nullptr);
607
608 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
609 ASSERT_TRUE(h_klass->IsInitialized());
610
611 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction;", class_loader));
612 ASSERT_TRUE(h_klass != nullptr);
613
614 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
615 ASSERT_TRUE(h_dex_cache != nullptr);
616 const DexFile* const dex_file = h_dex_cache->GetDexFile();
617 ASSERT_TRUE(dex_file != nullptr);
618
619 ASSERT_NE(dex_file->NumProtoIds(), 0u);
620 dex::ProtoIndex proto_index(0u);
621 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == nullptr);
622
623 // Do the transaction, then roll back.
624 EnterTransactionMode();
625 ObjPtr<mirror::MethodType> method_type =
626 class_linker_->ResolveMethodType(soa.Self(), proto_index, h_dex_cache, class_loader);
627 ASSERT_TRUE(method_type != nullptr);
628 // Make sure the method type was recorded in the dex cache.
629 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == method_type);
630 RollbackAndExitTransactionMode();
631 // Check that the method type was removed from the dex cache.
632 ASSERT_TRUE(h_dex_cache->GetResolvedMethodType(proto_index) == nullptr);
633 }
634
635 // Tests successful class initialization without class initializer.
TEST_F(TransactionTest,EmptyClass)636 TEST_F(TransactionTest, EmptyClass) {
637 ScopedObjectAccess soa(Thread::Current());
638 StackHandleScope<2> hs(soa.Self());
639 Handle<mirror::ClassLoader> class_loader(
640 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
641 ASSERT_TRUE(class_loader != nullptr);
642
643 Handle<mirror::Class> h_klass(
644 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
645 class_loader)));
646 ASSERT_TRUE(h_klass != nullptr);
647 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
648 ASSERT_TRUE(h_klass->IsVerified());
649
650 EnterTransactionMode();
651 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
652 ExitTransactionMode();
653 ASSERT_TRUE(success);
654 ASSERT_TRUE(h_klass->IsInitialized());
655 ASSERT_FALSE(soa.Self()->IsExceptionPending());
656 }
657
658 // Tests successful class initialization with class initializer.
TEST_F(TransactionTest,StaticFieldClass)659 TEST_F(TransactionTest, StaticFieldClass) {
660 ScopedObjectAccess soa(Thread::Current());
661 StackHandleScope<2> hs(soa.Self());
662 Handle<mirror::ClassLoader> class_loader(
663 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
664 ASSERT_TRUE(class_loader != nullptr);
665
666 Handle<mirror::Class> h_klass(
667 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
668 class_loader)));
669 ASSERT_TRUE(h_klass != nullptr);
670 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
671 ASSERT_TRUE(h_klass->IsVerified());
672
673 EnterTransactionMode();
674 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
675 ExitTransactionMode();
676 ASSERT_TRUE(success);
677 ASSERT_TRUE(h_klass->IsInitialized());
678 ASSERT_FALSE(soa.Self()->IsExceptionPending());
679 }
680
681 // Tests failing class initialization due to native call.
TEST_F(TransactionTest,NativeCallAbortClass)682 TEST_F(TransactionTest, NativeCallAbortClass) {
683 testTransactionAbort("LTransaction$NativeCallAbortClass;");
684 }
685
686 // Tests failing class initialization due to native call in a "synchronized" statement
687 // (which must catch any exception, do the monitor-exit then re-throw the caught exception).
TEST_F(TransactionTest,SynchronizedNativeCallAbortClass)688 TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
689 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
690 }
691
692 // Tests failing class initialization due to native call, even if an "all" catch handler
693 // catches the exception thrown when aborting the transaction.
TEST_F(TransactionTest,CatchNativeCallAbortClass)694 TEST_F(TransactionTest, CatchNativeCallAbortClass) {
695 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
696 }
697
698 // Tests failing class initialization with multiple transaction aborts.
TEST_F(TransactionTest,MultipleNativeCallAbortClass)699 TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
700 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
701 }
702
703 // Tests failing class initialization due to Class.forName() not finding the class,
704 // even if an "all" catch handler catches the exception thrown when aborting the transaction.
TEST_F(TransactionTest,CatchClassForNameAbortClass)705 TEST_F(TransactionTest, CatchClassForNameAbortClass) {
706 testTransactionAbort("LTransaction$CatchClassForNameAbortClass;");
707 }
708
709 // Same as CatchClassForNameAbortClass but the class initializer tries to do the work twice.
710 // This would trigger a DCHECK() if we continued executing bytecode with an aborted transaction.
TEST_F(TransactionTest,CatchClassForNameAbortClassTwice)711 TEST_F(TransactionTest, CatchClassForNameAbortClassTwice) {
712 testTransactionAbort("LTransaction$CatchClassForNameAbortClassTwice;");
713 }
714
715 // Tests failing class initialization due to allocating instance of finalizable class.
TEST_F(TransactionTest,FinalizableAbortClass)716 TEST_F(TransactionTest, FinalizableAbortClass) {
717 testTransactionAbort("LTransaction$FinalizableAbortClass;");
718 }
719
TEST_F(TransactionTest,Constraints)720 TEST_F(TransactionTest, Constraints) {
721 ScopedObjectAccess soa(Thread::Current());
722 StackHandleScope<11> hs(soa.Self());
723 Handle<mirror::ClassLoader> class_loader(
724 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
725
726 gc::Heap* heap = Runtime::Current()->GetHeap();
727 Handle<mirror::Class> boolean_class = hs.NewHandle(
728 class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader));
729 ASSERT_TRUE(boolean_class != nullptr);
730 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(boolean_class.Get()));
731 ArtField* true_field = boolean_class->FindDeclaredStaticField("TRUE", "Ljava/lang/Boolean;");
732 ASSERT_TRUE(true_field != nullptr);
733 ASSERT_TRUE(true_field->IsStatic());
734 Handle<mirror::Object> true_value = hs.NewHandle(true_field->GetObject(boolean_class.Get()));
735 ASSERT_TRUE(true_value != nullptr);
736 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(true_value.Get()));
737 ArtField* value_field = boolean_class->FindDeclaredInstanceField("value", "Z");
738 ASSERT_TRUE(value_field != nullptr);
739 ASSERT_FALSE(value_field->IsStatic());
740
741 Handle<mirror::Class> static_field_class(hs.NewHandle(
742 class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;", class_loader)));
743 ASSERT_TRUE(static_field_class != nullptr);
744 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(static_field_class.Get()));
745 ArtField* int_field = static_field_class->FindDeclaredStaticField("intField", "I");
746 ASSERT_TRUE(int_field != nullptr);
747
748 Handle<mirror::Class> static_fields_test_class(hs.NewHandle(
749 class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
750 ASSERT_TRUE(static_fields_test_class != nullptr);
751 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(static_fields_test_class.Get()));
752 ArtField* static_fields_test_int_field =
753 static_fields_test_class->FindDeclaredStaticField("intField", "I");
754 ASSERT_TRUE(static_fields_test_int_field != nullptr);
755
756 Handle<mirror::Class> instance_fields_test_class(hs.NewHandle(
757 class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
758 ASSERT_TRUE(instance_fields_test_class != nullptr);
759 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(instance_fields_test_class.Get()));
760 ArtField* instance_fields_test_int_field =
761 instance_fields_test_class->FindDeclaredInstanceField("intField", "I");
762 ASSERT_TRUE(instance_fields_test_int_field != nullptr);
763 Handle<mirror::Object> instance_fields_test_object = hs.NewHandle(
764 instance_fields_test_class->Alloc(soa.Self(), heap->GetCurrentAllocator()));
765 ASSERT_TRUE(instance_fields_test_object != nullptr);
766 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(instance_fields_test_object.Get()));
767
768 // The `long[].class` should be in the boot image but `long[][][].class` should not.
769 // (We have seen `long[][].class` both present and missing from the boot image,
770 // depending on the libcore code, so we do not use it for this test.)
771 Handle<mirror::Class> long_array_dim3_class = hs.NewHandle(
772 class_linker_->FindClass(soa.Self(), "[[[J", class_loader));
773 ASSERT_TRUE(long_array_dim3_class != nullptr);
774 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3_class.Get()));
775 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(
776 long_array_dim3_class->GetComponentType()->GetComponentType()));
777 Handle<mirror::Array> long_array_dim3 = hs.NewHandle(mirror::Array::Alloc(
778 soa.Self(),
779 long_array_dim3_class.Get(),
780 /*component_count=*/ 1,
781 long_array_dim3_class->GetComponentSizeShift(),
782 heap->GetCurrentAllocator()));
783 ASSERT_TRUE(long_array_dim3 != nullptr);
784 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3.Get()));
785 Handle<mirror::Array> long_array = hs.NewHandle(mirror::Array::Alloc(
786 soa.Self(),
787 long_array_dim3_class->GetComponentType()->GetComponentType(),
788 /*component_count=*/ 1,
789 long_array_dim3_class->GetComponentType()->GetComponentType()->GetComponentSizeShift(),
790 heap->GetCurrentAllocator()));
791 ASSERT_TRUE(long_array != nullptr);
792 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array.Get()));
793
794 // Use the Array's IfTable as an array from the boot image.
795 Handle<mirror::ObjectArray<mirror::Object>> array_iftable =
796 hs.NewHandle(long_array_dim3_class->GetIfTable());
797 ASSERT_TRUE(array_iftable != nullptr);
798 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(array_iftable.Get()));
799
800 // Test non-strict transaction.
801 ArenaPool* arena_pool = Runtime::Current()->GetArenaPool();
802 Transaction transaction(
803 /*strict=*/ false, /*root=*/ nullptr, /*arena_stack=*/ nullptr, arena_pool);
804 // Static field in boot image.
805 EXPECT_TRUE(transaction.WriteConstraint(boolean_class.Get()));
806 EXPECT_FALSE(transaction.ReadConstraint(boolean_class.Get()));
807 // Instance field or array element in boot image.
808 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
809 EXPECT_TRUE(transaction.WriteConstraint(true_value.Get()));
810 EXPECT_TRUE(transaction.WriteConstraint(array_iftable.Get()));
811 // Static field not in boot image.
812 EXPECT_FALSE(transaction.WriteConstraint(static_fields_test_class.Get()));
813 EXPECT_FALSE(transaction.ReadConstraint(static_fields_test_class.Get()));
814 // Instance field or array element not in boot image.
815 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
816 EXPECT_FALSE(transaction.WriteConstraint(instance_fields_test_object.Get()));
817 EXPECT_FALSE(transaction.WriteConstraint(long_array_dim3.Get()));
818 // Write value constraints.
819 EXPECT_FALSE(transaction.WriteValueConstraint(static_fields_test_class.Get()));
820 EXPECT_FALSE(transaction.WriteValueConstraint(instance_fields_test_object.Get()));
821 EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3->GetClass()));
822 EXPECT_TRUE(transaction.WriteValueConstraint(long_array_dim3.Get()));
823 EXPECT_FALSE(transaction.WriteValueConstraint(long_array->GetClass()));
824 EXPECT_FALSE(transaction.WriteValueConstraint(long_array.Get()));
825
826 // Test strict transaction.
827 Transaction strict_transaction(
828 /*strict=*/ true, /*root=*/ static_field_class.Get(), /*arena_stack=*/ nullptr, arena_pool);
829 // Static field in boot image.
830 EXPECT_TRUE(strict_transaction.WriteConstraint(boolean_class.Get()));
831 EXPECT_TRUE(strict_transaction.ReadConstraint(boolean_class.Get()));
832 // Instance field or array element in boot image.
833 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
834 EXPECT_TRUE(strict_transaction.WriteConstraint(true_value.Get()));
835 EXPECT_TRUE(strict_transaction.WriteConstraint(array_iftable.Get()));
836 // Static field in another class not in boot image.
837 EXPECT_TRUE(strict_transaction.WriteConstraint(static_fields_test_class.Get()));
838 EXPECT_TRUE(strict_transaction.ReadConstraint(static_fields_test_class.Get()));
839 // Instance field or array element not in boot image.
840 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
841 EXPECT_FALSE(strict_transaction.WriteConstraint(instance_fields_test_object.Get()));
842 EXPECT_FALSE(strict_transaction.WriteConstraint(long_array_dim3.Get()));
843 // Static field in the same class.
844 EXPECT_FALSE(strict_transaction.WriteConstraint(static_field_class.Get()));
845 EXPECT_FALSE(strict_transaction.ReadConstraint(static_field_class.Get()));
846 // Write value constraints.
847 EXPECT_FALSE(strict_transaction.WriteValueConstraint(static_fields_test_class.Get()));
848 EXPECT_FALSE(strict_transaction.WriteValueConstraint(instance_fields_test_object.Get()));
849 // TODO: The following may be revised, see a TODO in Transaction::WriteValueConstraint().
850 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3->GetClass()));
851 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array_dim3.Get()));
852 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array->GetClass()));
853 EXPECT_FALSE(strict_transaction.WriteValueConstraint(long_array.Get()));
854 }
855
856 } // namespace art
857