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_runtime_test.h"
23 #include "dex/dex_file.h"
24 #include "mirror/array-alloc-inl.h"
25 #include "mirror/class-alloc-inl.h"
26 #include "scoped_thread_state_change-inl.h"
27
28 namespace art {
29
30 class TransactionTest : public CommonRuntimeTest {
31 protected:
TransactionTest()32 TransactionTest() {
33 this->use_boot_image_ = true; // We need the boot image for this test.
34 }
35
36 // Tests failing class initialization due to native call with transaction rollback.
testTransactionAbort(const char * tested_class_signature)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(
42 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
43 ASSERT_TRUE(class_loader != nullptr);
44
45 // 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.
48 MutableHandle<mirror::Class> h_klass(
49 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
50 "Ljava/lang/ExceptionInInitializerError;")));
51 ASSERT_TRUE(h_klass != nullptr);
52 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
53 ASSERT_TRUE(h_klass->IsInitialized());
54
55 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(),
56 Transaction::kAbortExceptionDescriptor));
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
362 // Tests static array fields are reset to their default value after transaction rollback.
TEST_F(TransactionTest,StaticArrayFieldsTest)363 TEST_F(TransactionTest, StaticArrayFieldsTest) {
364 ScopedObjectAccess soa(Thread::Current());
365 StackHandleScope<13> hs(soa.Self());
366 Handle<mirror::ClassLoader> class_loader(
367 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
368 ASSERT_TRUE(class_loader != nullptr);
369
370 Handle<mirror::Class> h_klass(
371 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
372 ASSERT_TRUE(h_klass != nullptr);
373 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
374 ASSERT_TRUE(success);
375 ASSERT_TRUE(h_klass->IsInitialized());
376 ASSERT_FALSE(soa.Self()->IsExceptionPending());
377
378 // Lookup fields.
379 ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
380 ASSERT_TRUE(booleanArrayField != nullptr);
381 Handle<mirror::BooleanArray> booleanArray = hs.NewHandle(
382 booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray());
383 ASSERT_TRUE(booleanArray != nullptr);
384 ASSERT_EQ(booleanArray->GetLength(), 1);
385 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
386
387 ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
388 ASSERT_TRUE(byteArrayField != nullptr);
389 Handle<mirror::ByteArray> byteArray =
390 hs.NewHandle(byteArrayField->GetObject(h_klass.Get())->AsByteArray());
391 ASSERT_TRUE(byteArray != nullptr);
392 ASSERT_EQ(byteArray->GetLength(), 1);
393 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
394
395 ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
396 ASSERT_TRUE(charArrayField != nullptr);
397 Handle<mirror::CharArray> charArray =
398 hs.NewHandle(charArrayField->GetObject(h_klass.Get())->AsCharArray());
399 ASSERT_TRUE(charArray != nullptr);
400 ASSERT_EQ(charArray->GetLength(), 1);
401 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
402
403 ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
404 ASSERT_TRUE(shortArrayField != nullptr);
405 Handle<mirror::ShortArray> shortArray =
406 hs.NewHandle(shortArrayField->GetObject(h_klass.Get())->AsShortArray());
407 ASSERT_TRUE(shortArray != nullptr);
408 ASSERT_EQ(shortArray->GetLength(), 1);
409 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
410
411 ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
412 ASSERT_TRUE(intArrayField != nullptr);
413 Handle<mirror::IntArray> intArray =
414 hs.NewHandle(intArrayField->GetObject(h_klass.Get())->AsIntArray());
415 ASSERT_TRUE(intArray != nullptr);
416 ASSERT_EQ(intArray->GetLength(), 1);
417 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
418
419 ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
420 ASSERT_TRUE(longArrayField != nullptr);
421 Handle<mirror::LongArray> longArray =
422 hs.NewHandle(longArrayField->GetObject(h_klass.Get())->AsLongArray());
423 ASSERT_TRUE(longArray != nullptr);
424 ASSERT_EQ(longArray->GetLength(), 1);
425 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
426
427 ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
428 ASSERT_TRUE(floatArrayField != nullptr);
429 Handle<mirror::FloatArray> floatArray =
430 hs.NewHandle(floatArrayField->GetObject(h_klass.Get())->AsFloatArray());
431 ASSERT_TRUE(floatArray != nullptr);
432 ASSERT_EQ(floatArray->GetLength(), 1);
433 ASSERT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
434
435 ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
436 ASSERT_TRUE(doubleArrayField != nullptr);
437 Handle<mirror::DoubleArray> doubleArray =
438 hs.NewHandle(doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray());
439 ASSERT_TRUE(doubleArray != nullptr);
440 ASSERT_EQ(doubleArray->GetLength(), 1);
441 ASSERT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
442
443 ArtField* objectArrayField =
444 h_klass->FindDeclaredStaticField("objectArrayField", "[Ljava/lang/Object;");
445 ASSERT_TRUE(objectArrayField != nullptr);
446 Handle<mirror::ObjectArray<mirror::Object>> objectArray =
447 hs.NewHandle(objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>());
448 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.
453 Handle<mirror::Class> object_klass(
454 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
455 ASSERT_TRUE(object_klass != nullptr);
456 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
457 ASSERT_TRUE(h_obj != nullptr);
458 ASSERT_OBJ_PTR_EQ(h_obj->GetClass(), h_klass.Get());
459
460 // Modify fields inside transaction then rollback changes.
461 EnterTransactionMode();
462 booleanArray->SetWithoutChecks<true>(0, true);
463 byteArray->SetWithoutChecks<true>(0, 1);
464 charArray->SetWithoutChecks<true>(0, 1u);
465 shortArray->SetWithoutChecks<true>(0, 1);
466 intArray->SetWithoutChecks<true>(0, 1);
467 longArray->SetWithoutChecks<true>(0, 1);
468 floatArray->SetWithoutChecks<true>(0, 1.0);
469 doubleArray->SetWithoutChecks<true>(0, 1.0);
470 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
471 RollbackAndExitTransactionMode();
472
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));
480 EXPECT_FLOAT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
481 EXPECT_DOUBLE_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
482 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
483 }
484
485 // Tests rolling back interned strings and resolved strings.
TEST_F(TransactionTest,ResolveString)486 TEST_F(TransactionTest, ResolveString) {
487 ScopedObjectAccess soa(Thread::Current());
488 StackHandleScope<3> hs(soa.Self());
489 Handle<mirror::ClassLoader> class_loader(
490 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
491 ASSERT_TRUE(class_loader != nullptr);
492
493 Handle<mirror::Class> h_klass(
494 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$ResolveString;",
495 class_loader)));
496 ASSERT_TRUE(h_klass != nullptr);
497
498 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(h_klass->GetDexCache()));
499 ASSERT_TRUE(h_dex_cache != nullptr);
500 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";
505 const dex::StringId* string_id = dex_file->FindStringId(kResolvedString);
506 ASSERT_TRUE(string_id != nullptr);
507 dex::StringIndex string_idx = dex_file->GetIndexForStringId(*string_id);
508 ASSERT_TRUE(string_idx.IsValid());
509 // String should only get resolved by the initializer.
510 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
511 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
512 // Do the transaction, then roll back.
513 EnterTransactionMode();
514 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 {
519 ObjPtr<mirror::String> s =
520 class_linker_->LookupString(string_idx, h_dex_cache.Get());
521 ASSERT_TRUE(s != nullptr);
522 EXPECT_STREQ(s->ToModifiedUtf8().c_str(), kResolvedString);
523 EXPECT_OBJ_PTR_EQ(s, h_dex_cache->GetResolvedString(string_idx));
524 }
525 RollbackAndExitTransactionMode();
526 // Check that the string did not stay resolved.
527 EXPECT_TRUE(class_linker_->LookupString(string_idx, h_dex_cache.Get()) == nullptr);
528 EXPECT_TRUE(h_dex_cache->GetResolvedString(string_idx) == nullptr);
529 ASSERT_FALSE(h_klass->IsInitialized());
530 ASSERT_FALSE(soa.Self()->IsExceptionPending());
531 }
532
533 // Tests rolling back resolved method types in dex cache.
TEST_F(TransactionTest,ResolveMethodType)534 TEST_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
566 // Tests successful class initialization without class initializer.
TEST_F(TransactionTest,EmptyClass)567 TEST_F(TransactionTest, EmptyClass) {
568 ScopedObjectAccess soa(Thread::Current());
569 StackHandleScope<2> hs(soa.Self());
570 Handle<mirror::ClassLoader> class_loader(
571 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
572 ASSERT_TRUE(class_loader != nullptr);
573
574 Handle<mirror::Class> h_klass(
575 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;",
576 class_loader)));
577 ASSERT_TRUE(h_klass != nullptr);
578 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
579 ASSERT_TRUE(h_klass->IsVerified());
580
581 EnterTransactionMode();
582 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
583 ExitTransactionMode();
584 ASSERT_TRUE(success);
585 ASSERT_TRUE(h_klass->IsInitialized());
586 ASSERT_FALSE(soa.Self()->IsExceptionPending());
587 }
588
589 // Tests successful class initialization with class initializer.
TEST_F(TransactionTest,StaticFieldClass)590 TEST_F(TransactionTest, StaticFieldClass) {
591 ScopedObjectAccess soa(Thread::Current());
592 StackHandleScope<2> hs(soa.Self());
593 Handle<mirror::ClassLoader> class_loader(
594 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
595 ASSERT_TRUE(class_loader != nullptr);
596
597 Handle<mirror::Class> h_klass(
598 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
599 class_loader)));
600 ASSERT_TRUE(h_klass != nullptr);
601 class_linker_->VerifyClass(soa.Self(), /* verifier_deps= */ nullptr, h_klass);
602 ASSERT_TRUE(h_klass->IsVerified());
603
604 EnterTransactionMode();
605 bool success = class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
606 ExitTransactionMode();
607 ASSERT_TRUE(success);
608 ASSERT_TRUE(h_klass->IsInitialized());
609 ASSERT_FALSE(soa.Self()->IsExceptionPending());
610 }
611
612 // Tests failing class initialization due to native call.
TEST_F(TransactionTest,NativeCallAbortClass)613 TEST_F(TransactionTest, NativeCallAbortClass) {
614 testTransactionAbort("LTransaction$NativeCallAbortClass;");
615 }
616
617 // 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).
TEST_F(TransactionTest,SynchronizedNativeCallAbortClass)619 TEST_F(TransactionTest, SynchronizedNativeCallAbortClass) {
620 testTransactionAbort("LTransaction$SynchronizedNativeCallAbortClass;");
621 }
622
623 // Tests failing class initialization due to native call, even if an "all" catch handler
624 // catches the exception thrown when aborting the transaction.
TEST_F(TransactionTest,CatchNativeCallAbortClass)625 TEST_F(TransactionTest, CatchNativeCallAbortClass) {
626 testTransactionAbort("LTransaction$CatchNativeCallAbortClass;");
627 }
628
629 // Tests failing class initialization with multiple transaction aborts.
TEST_F(TransactionTest,MultipleNativeCallAbortClass)630 TEST_F(TransactionTest, MultipleNativeCallAbortClass) {
631 testTransactionAbort("LTransaction$MultipleNativeCallAbortClass;");
632 }
633
634 // 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.
TEST_F(TransactionTest,CatchClassForNameAbortClass)636 TEST_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.
TEST_F(TransactionTest,CatchClassForNameAbortClassTwice)642 TEST_F(TransactionTest, CatchClassForNameAbortClassTwice) {
643 testTransactionAbort("LTransaction$CatchClassForNameAbortClassTwice;");
644 }
645
646 // Tests failing class initialization due to allocating instance of finalizable class.
TEST_F(TransactionTest,FinalizableAbortClass)647 TEST_F(TransactionTest, FinalizableAbortClass) {
648 testTransactionAbort("LTransaction$FinalizableAbortClass;");
649 }
650
TEST_F(TransactionTest,Constraints)651 TEST_F(TransactionTest, Constraints) {
652 ScopedObjectAccess soa(Thread::Current());
653 StackHandleScope<11> hs(soa.Self());
654 Handle<mirror::ClassLoader> class_loader(
655 hs.NewHandle(soa.Decode<mirror::ClassLoader>(LoadDex("Transaction"))));
656
657 gc::Heap* heap = Runtime::Current()->GetHeap();
658 Handle<mirror::Class> boolean_class = hs.NewHandle(
659 class_linker_->FindClass(soa.Self(), "Ljava/lang/Boolean;", class_loader));
660 ASSERT_TRUE(boolean_class != nullptr);
661 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(boolean_class.Get()));
662 ArtField* true_field = boolean_class->FindDeclaredStaticField("TRUE", "Ljava/lang/Boolean;");
663 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);
667 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(true_value.Get()));
668 ArtField* value_field = boolean_class->FindDeclaredInstanceField("value", "Z");
669 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)));
674 ASSERT_TRUE(static_field_class != nullptr);
675 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(static_field_class.Get()));
676 ArtField* int_field = static_field_class->FindDeclaredStaticField("intField", "I");
677 ASSERT_TRUE(int_field != nullptr);
678
679 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 =
684 static_fields_test_class->FindDeclaredStaticField("intField", "I");
685 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 =
692 instance_fields_test_class->FindDeclaredInstanceField("intField", "I");
693 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
699 // 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(
709 soa.Self(),
710 long_array_dim3_class.Get(),
711 /*component_count=*/ 1,
712 long_array_dim3_class->GetComponentSizeShift(),
713 heap->GetCurrentAllocator()));
714 ASSERT_TRUE(long_array_dim3 != nullptr);
715 ASSERT_FALSE(heap->ObjectIsInBootImageSpace(long_array_dim3.Get()));
716 Handle<mirror::Array> long_array = hs.NewHandle(mirror::Array::Alloc(
717 soa.Self(),
718 long_array_dim3_class->GetComponentType()->GetComponentType(),
719 /*component_count=*/ 1,
720 long_array_dim3_class->GetComponentType()->GetComponentType()->GetComponentSizeShift(),
721 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 =
727 hs.NewHandle(long_array_dim3_class->GetIfTable());
728 ASSERT_TRUE(array_iftable != nullptr);
729 ASSERT_TRUE(heap->ObjectIsInBootImageSpace(array_iftable.Get()));
730
731 // Test non-strict transaction.
732 ArenaPool* arena_pool = Runtime::Current()->GetArenaPool();
733 Transaction transaction(
734 /*strict=*/ false, /*root=*/ nullptr, /*arena_stack=*/ nullptr, arena_pool);
735 // Static field in boot image.
736 EXPECT_TRUE(transaction.WriteConstraint(boolean_class.Get()));
737 EXPECT_FALSE(transaction.ReadConstraint(boolean_class.Get()));
738 // Instance field or array element in boot image.
739 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
740 EXPECT_TRUE(transaction.WriteConstraint(true_value.Get()));
741 EXPECT_TRUE(transaction.WriteConstraint(array_iftable.Get()));
742 // Static field not in boot image.
743 EXPECT_FALSE(transaction.WriteConstraint(static_fields_test_class.Get()));
744 EXPECT_FALSE(transaction.ReadConstraint(static_fields_test_class.Get()));
745 // Instance field or array element not in boot image.
746 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
747 EXPECT_FALSE(transaction.WriteConstraint(instance_fields_test_object.Get()));
748 EXPECT_FALSE(transaction.WriteConstraint(long_array_dim3.Get()));
749 // Write value constraints.
750 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()));
756
757 // Test strict transaction.
758 Transaction strict_transaction(
759 /*strict=*/ true, /*root=*/ static_field_class.Get(), /*arena_stack=*/ nullptr, arena_pool);
760 // Static field in boot image.
761 EXPECT_TRUE(strict_transaction.WriteConstraint(boolean_class.Get()));
762 EXPECT_TRUE(strict_transaction.ReadConstraint(boolean_class.Get()));
763 // Instance field or array element in boot image.
764 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
765 EXPECT_TRUE(strict_transaction.WriteConstraint(true_value.Get()));
766 EXPECT_TRUE(strict_transaction.WriteConstraint(array_iftable.Get()));
767 // Static field in another class not in boot image.
768 EXPECT_TRUE(strict_transaction.WriteConstraint(static_fields_test_class.Get()));
769 EXPECT_TRUE(strict_transaction.ReadConstraint(static_fields_test_class.Get()));
770 // Instance field or array element not in boot image.
771 // Do not check ReadConstraint(), it expects only static fields (checks for class object).
772 EXPECT_FALSE(strict_transaction.WriteConstraint(instance_fields_test_object.Get()));
773 EXPECT_FALSE(strict_transaction.WriteConstraint(long_array_dim3.Get()));
774 // Static field in the same class.
775 EXPECT_FALSE(strict_transaction.WriteConstraint(static_field_class.Get()));
776 EXPECT_FALSE(strict_transaction.ReadConstraint(static_field_class.Get()));
777 // Write value constraints.
778 EXPECT_FALSE(strict_transaction.WriteValueConstraint(static_fields_test_class.Get()));
779 EXPECT_FALSE(strict_transaction.WriteValueConstraint(instance_fields_test_object.Get()));
780 // TODO: The following may be revised, see a TODO in Transaction::WriteValueConstraint().
781 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()));
785 }
786
787 } // namespace art
788