blob: 8c37489e17a26b3e9d1f3b6a8654239211f10c9b [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
19#include "common_runtime_test.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010020#include "mirror/array-inl.h"
21#include "mirror/art_field-inl.h"
22#include "mirror/art_method-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "scoped_thread_state_change.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010024
25namespace art {
26
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027class TransactionTest : public CommonRuntimeTest {};
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010028
29TEST_F(TransactionTest, Object_class) {
30 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031 StackHandleScope<2> hs(soa.Self());
32 Handle<mirror::Class> h_klass(
33 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
34 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010035
36 Transaction transaction;
37 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070038 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
39 ASSERT_TRUE(h_obj.Get() != nullptr);
40 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010041 Runtime::Current()->ExitTransactionMode();
42
43 // Aborting transaction must not clear the Object::class field.
44 transaction.Abort();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070045 EXPECT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010046}
47
48TEST_F(TransactionTest, Object_monitor) {
49 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070050 StackHandleScope<2> hs(soa.Self());
51 Handle<mirror::Class> h_klass(
52 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
53 ASSERT_TRUE(h_klass.Get() != nullptr);
54 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
55 ASSERT_TRUE(h_obj.Get() != nullptr);
56 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010057
58 // Lock object's monitor outside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070059 h_obj->MonitorEnter(soa.Self());
60 uint32_t old_lock_word = h_obj->GetLockWord(false).GetValue();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010061
62 Transaction transaction;
63 Runtime::Current()->EnterTransactionMode(&transaction);
64 // Unlock object's monitor inside the transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070065 h_obj->MonitorExit(soa.Self());
66 uint32_t new_lock_word = h_obj->GetLockWord(false).GetValue();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010067 Runtime::Current()->ExitTransactionMode();
68
69 // Aborting transaction must not clear the Object::class field.
70 transaction.Abort();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070071 uint32_t aborted_lock_word = h_obj->GetLockWord(false).GetValue();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010072 EXPECT_NE(old_lock_word, new_lock_word);
73 EXPECT_EQ(aborted_lock_word, new_lock_word);
74}
75
76TEST_F(TransactionTest, Array_length) {
77 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070078 StackHandleScope<2> hs(soa.Self());
79 Handle<mirror::Class> h_klass(
80 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
81 ASSERT_TRUE(h_klass.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010082
83 constexpr int32_t kArraySize = 2;
84
85 Transaction transaction;
86 Runtime::Current()->EnterTransactionMode(&transaction);
87
88 // Allocate an array during transaction.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070089 Handle<mirror::Array> h_obj(
90 hs.NewHandle(
91 mirror::Array::Alloc<true>(soa.Self(), h_klass.Get(), kArraySize,
92 h_klass->GetComponentSize(),
93 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
94 ASSERT_TRUE(h_obj.Get() != nullptr);
95 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010096 Runtime::Current()->ExitTransactionMode();
97
98 // Aborting transaction must not clear the Object::class field.
99 transaction.Abort();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 EXPECT_EQ(h_obj->GetLength(), kArraySize);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100101}
102
103TEST_F(TransactionTest, StaticFieldsTest) {
104 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105 StackHandleScope<4> hs(soa.Self());
106 Handle<mirror::ClassLoader> class_loader(
107 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
108 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700110 Handle<mirror::Class> h_klass(
111 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticFieldsTest;", class_loader)));
112 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700113 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115
116 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 mirror::ArtField* booleanField = h_klass->FindDeclaredStaticField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700119 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 ASSERT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 mirror::ArtField* byteField = h_klass->FindDeclaredStaticField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700124 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700125 ASSERT_EQ(byteField->GetByte(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100126
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 mirror::ArtField* charField = h_klass->FindDeclaredStaticField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100128 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700129 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 ASSERT_EQ(charField->GetChar(h_klass.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 mirror::ArtField* shortField = h_klass->FindDeclaredStaticField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100133 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700134 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 ASSERT_EQ(shortField->GetShort(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100136
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 mirror::ArtField* intField = h_klass->FindDeclaredStaticField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700139 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 ASSERT_EQ(intField->GetInt(h_klass.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100141
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700142 mirror::ArtField* longField = h_klass->FindDeclaredStaticField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100143 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700144 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 ASSERT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 mirror::ArtField* floatField = h_klass->FindDeclaredStaticField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100148 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700149 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 ASSERT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 mirror::ArtField* doubleField = h_klass->FindDeclaredStaticField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700154 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 ASSERT_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100156
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 mirror::ArtField* objectField = h_klass->FindDeclaredStaticField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800158 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700160 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161 ASSERT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100162
163 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 Handle<mirror::Class> object_klass(
165 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
166 ASSERT_TRUE(object_klass.Get() != nullptr);
167 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
168 ASSERT_TRUE(h_obj.Get() != nullptr);
169 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100170
171 // Modify fields inside transaction and abort it.
172 Transaction transaction;
173 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 booleanField->SetBoolean<true>(h_klass.Get(), true);
175 byteField->SetByte<true>(h_klass.Get(), 1);
176 charField->SetChar<true>(h_klass.Get(), 1u);
177 shortField->SetShort<true>(h_klass.Get(), 1);
178 intField->SetInt<true>(h_klass.Get(), 1);
179 longField->SetLong<true>(h_klass.Get(), 1);
180 floatField->SetFloat<true>(h_klass.Get(), 1.0);
181 doubleField->SetDouble<true>(h_klass.Get(), 1.0);
182 objectField->SetObject<true>(h_klass.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100183 Runtime::Current()->ExitTransactionMode();
184 transaction.Abort();
185
186 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 EXPECT_EQ(booleanField->GetBoolean(h_klass.Get()), false);
188 EXPECT_EQ(byteField->GetByte(h_klass.Get()), 0);
189 EXPECT_EQ(charField->GetChar(h_klass.Get()), 0u);
190 EXPECT_EQ(shortField->GetShort(h_klass.Get()), 0);
191 EXPECT_EQ(intField->GetInt(h_klass.Get()), 0);
192 EXPECT_EQ(longField->GetLong(h_klass.Get()), static_cast<int64_t>(0));
193 EXPECT_EQ(floatField->GetFloat(h_klass.Get()), static_cast<float>(0.0f));
194 EXPECT_EQ(doubleField->GetDouble(h_klass.Get()), static_cast<double>(0.0));
195 EXPECT_EQ(objectField->GetObject(h_klass.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100196}
197
198TEST_F(TransactionTest, InstanceFieldsTest) {
199 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 StackHandleScope<5> hs(soa.Self());
201 Handle<mirror::ClassLoader> class_loader(
202 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
203 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100204
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 Handle<mirror::Class> h_klass(
206 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LInstanceFieldsTest;", class_loader)));
207 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700208 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210
211 // Allocate an InstanceFieldTest object.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700212 Handle<mirror::Object> h_instance(hs.NewHandle(h_klass->AllocObject(soa.Self())));
213 ASSERT_TRUE(h_instance.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100214
215 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700216 mirror::ArtField* booleanField = h_klass->FindDeclaredInstanceField("booleanField", "Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100217 ASSERT_TRUE(booleanField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700218 ASSERT_EQ(booleanField->GetTypeAsPrimitiveType(), Primitive::kPrimBoolean);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 ASSERT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700221 mirror::ArtField* byteField = h_klass->FindDeclaredInstanceField("byteField", "B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 ASSERT_TRUE(byteField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700223 ASSERT_EQ(byteField->GetTypeAsPrimitiveType(), Primitive::kPrimByte);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700224 ASSERT_EQ(byteField->GetByte(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100225
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700226 mirror::ArtField* charField = h_klass->FindDeclaredInstanceField("charField", "C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100227 ASSERT_TRUE(charField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700228 ASSERT_EQ(charField->GetTypeAsPrimitiveType(), Primitive::kPrimChar);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700229 ASSERT_EQ(charField->GetChar(h_instance.Get()), 0u);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100230
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700231 mirror::ArtField* shortField = h_klass->FindDeclaredInstanceField("shortField", "S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100232 ASSERT_TRUE(shortField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700233 ASSERT_EQ(shortField->GetTypeAsPrimitiveType(), Primitive::kPrimShort);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700234 ASSERT_EQ(shortField->GetShort(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100235
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700236 mirror::ArtField* intField = h_klass->FindDeclaredInstanceField("intField", "I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100237 ASSERT_TRUE(intField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700238 ASSERT_EQ(intField->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700239 ASSERT_EQ(intField->GetInt(h_instance.Get()), 0);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100240
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700241 mirror::ArtField* longField = h_klass->FindDeclaredInstanceField("longField", "J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100242 ASSERT_TRUE(longField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700243 ASSERT_EQ(longField->GetTypeAsPrimitiveType(), Primitive::kPrimLong);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700244 ASSERT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100245
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700246 mirror::ArtField* floatField = h_klass->FindDeclaredInstanceField("floatField", "F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100247 ASSERT_TRUE(floatField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700248 ASSERT_EQ(floatField->GetTypeAsPrimitiveType(), Primitive::kPrimFloat);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249 ASSERT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100250
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700251 mirror::ArtField* doubleField = h_klass->FindDeclaredInstanceField("doubleField", "D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100252 ASSERT_TRUE(doubleField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700253 ASSERT_EQ(doubleField->GetTypeAsPrimitiveType(), Primitive::kPrimDouble);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700254 ASSERT_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100255
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700256 mirror::ArtField* objectField = h_klass->FindDeclaredInstanceField("objectField",
Ian Rogers98379392014-02-24 16:53:16 -0800257 "Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100258 ASSERT_TRUE(objectField != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700259 ASSERT_EQ(objectField->GetTypeAsPrimitiveType(), Primitive::kPrimNot);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700260 ASSERT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100261
262 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700263 Handle<mirror::Class> object_klass(
264 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
265 ASSERT_TRUE(object_klass.Get() != nullptr);
266 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
267 ASSERT_TRUE(h_obj.Get() != nullptr);
268 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100269
270 // Modify fields inside transaction and abort it.
271 Transaction transaction;
272 Runtime::Current()->EnterTransactionMode(&transaction);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700273 booleanField->SetBoolean<true>(h_instance.Get(), true);
274 byteField->SetByte<true>(h_instance.Get(), 1);
275 charField->SetChar<true>(h_instance.Get(), 1u);
276 shortField->SetShort<true>(h_instance.Get(), 1);
277 intField->SetInt<true>(h_instance.Get(), 1);
278 longField->SetLong<true>(h_instance.Get(), 1);
279 floatField->SetFloat<true>(h_instance.Get(), 1.0);
280 doubleField->SetDouble<true>(h_instance.Get(), 1.0);
281 objectField->SetObject<true>(h_instance.Get(), h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282 Runtime::Current()->ExitTransactionMode();
283 transaction.Abort();
284
285 // Check values have properly been restored to their original (default) value.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700286 EXPECT_EQ(booleanField->GetBoolean(h_instance.Get()), false);
287 EXPECT_EQ(byteField->GetByte(h_instance.Get()), 0);
288 EXPECT_EQ(charField->GetChar(h_instance.Get()), 0u);
289 EXPECT_EQ(shortField->GetShort(h_instance.Get()), 0);
290 EXPECT_EQ(intField->GetInt(h_instance.Get()), 0);
291 EXPECT_EQ(longField->GetLong(h_instance.Get()), static_cast<int64_t>(0));
292 EXPECT_EQ(floatField->GetFloat(h_instance.Get()), static_cast<float>(0.0f));
293 EXPECT_EQ(doubleField->GetDouble(h_instance.Get()), static_cast<double>(0.0));
294 EXPECT_EQ(objectField->GetObject(h_instance.Get()), nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100295}
296
297
298TEST_F(TransactionTest, StaticArrayFieldsTest) {
299 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700300 StackHandleScope<4> hs(soa.Self());
301 Handle<mirror::ClassLoader> class_loader(
302 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
303 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100304
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700305 Handle<mirror::Class> h_klass(
306 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticArrayFieldsTest;", class_loader)));
307 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700308 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700309 ASSERT_TRUE(h_klass->IsInitialized());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100310
311 // Lookup fields.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700312 mirror::ArtField* booleanArrayField = h_klass->FindDeclaredStaticField("booleanArrayField", "[Z");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100313 ASSERT_TRUE(booleanArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700314 mirror::BooleanArray* booleanArray = booleanArrayField->GetObject(h_klass.Get())->AsBooleanArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100315 ASSERT_TRUE(booleanArray != nullptr);
316 ASSERT_EQ(booleanArray->GetLength(), 1);
317 ASSERT_EQ(booleanArray->GetWithoutChecks(0), false);
318
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700319 mirror::ArtField* byteArrayField = h_klass->FindDeclaredStaticField("byteArrayField", "[B");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100320 ASSERT_TRUE(byteArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700321 mirror::ByteArray* byteArray = byteArrayField->GetObject(h_klass.Get())->AsByteArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100322 ASSERT_TRUE(byteArray != nullptr);
323 ASSERT_EQ(byteArray->GetLength(), 1);
324 ASSERT_EQ(byteArray->GetWithoutChecks(0), 0);
325
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700326 mirror::ArtField* charArrayField = h_klass->FindDeclaredStaticField("charArrayField", "[C");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100327 ASSERT_TRUE(charArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700328 mirror::CharArray* charArray = charArrayField->GetObject(h_klass.Get())->AsCharArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100329 ASSERT_TRUE(charArray != nullptr);
330 ASSERT_EQ(charArray->GetLength(), 1);
331 ASSERT_EQ(charArray->GetWithoutChecks(0), 0u);
332
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700333 mirror::ArtField* shortArrayField = h_klass->FindDeclaredStaticField("shortArrayField", "[S");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100334 ASSERT_TRUE(shortArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700335 mirror::ShortArray* shortArray = shortArrayField->GetObject(h_klass.Get())->AsShortArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336 ASSERT_TRUE(shortArray != nullptr);
337 ASSERT_EQ(shortArray->GetLength(), 1);
338 ASSERT_EQ(shortArray->GetWithoutChecks(0), 0);
339
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700340 mirror::ArtField* intArrayField = h_klass->FindDeclaredStaticField("intArrayField", "[I");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 ASSERT_TRUE(intArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 mirror::IntArray* intArray = intArrayField->GetObject(h_klass.Get())->AsIntArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 ASSERT_TRUE(intArray != nullptr);
344 ASSERT_EQ(intArray->GetLength(), 1);
345 ASSERT_EQ(intArray->GetWithoutChecks(0), 0);
346
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700347 mirror::ArtField* longArrayField = h_klass->FindDeclaredStaticField("longArrayField", "[J");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 ASSERT_TRUE(longArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700349 mirror::LongArray* longArray = longArrayField->GetObject(h_klass.Get())->AsLongArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100350 ASSERT_TRUE(longArray != nullptr);
351 ASSERT_EQ(longArray->GetLength(), 1);
352 ASSERT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
353
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700354 mirror::ArtField* floatArrayField = h_klass->FindDeclaredStaticField("floatArrayField", "[F");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100355 ASSERT_TRUE(floatArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700356 mirror::FloatArray* floatArray = floatArrayField->GetObject(h_klass.Get())->AsFloatArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100357 ASSERT_TRUE(floatArray != nullptr);
358 ASSERT_EQ(floatArray->GetLength(), 1);
359 ASSERT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
360
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700361 mirror::ArtField* doubleArrayField = h_klass->FindDeclaredStaticField("doubleArrayField", "[D");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100362 ASSERT_TRUE(doubleArrayField != nullptr);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700363 mirror::DoubleArray* doubleArray = doubleArrayField->GetObject(h_klass.Get())->AsDoubleArray();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100364 ASSERT_TRUE(doubleArray != nullptr);
365 ASSERT_EQ(doubleArray->GetLength(), 1);
366 ASSERT_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
367
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700368 mirror::ArtField* objectArrayField = h_klass->FindDeclaredStaticField("objectArrayField",
Ian Rogers98379392014-02-24 16:53:16 -0800369 "[Ljava/lang/Object;");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100370 ASSERT_TRUE(objectArrayField != nullptr);
Ian Rogers98379392014-02-24 16:53:16 -0800371 mirror::ObjectArray<mirror::Object>* objectArray =
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700372 objectArrayField->GetObject(h_klass.Get())->AsObjectArray<mirror::Object>();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100373 ASSERT_TRUE(objectArray != nullptr);
374 ASSERT_EQ(objectArray->GetLength(), 1);
375 ASSERT_EQ(objectArray->GetWithoutChecks(0), nullptr);
376
377 // Create a java.lang.Object instance to set objectField.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700378 Handle<mirror::Class> object_klass(
379 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;")));
380 ASSERT_TRUE(object_klass.Get() != nullptr);
381 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(soa.Self())));
382 ASSERT_TRUE(h_obj.Get() != nullptr);
383 ASSERT_EQ(h_obj->GetClass(), h_klass.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100384
385 // Modify fields inside transaction and abort it.
386 Transaction transaction;
387 Runtime::Current()->EnterTransactionMode(&transaction);
388 booleanArray->SetWithoutChecks<true>(0, true);
389 byteArray->SetWithoutChecks<true>(0, 1);
390 charArray->SetWithoutChecks<true>(0, 1u);
391 shortArray->SetWithoutChecks<true>(0, 1);
392 intArray->SetWithoutChecks<true>(0, 1);
393 longArray->SetWithoutChecks<true>(0, 1);
394 floatArray->SetWithoutChecks<true>(0, 1.0);
395 doubleArray->SetWithoutChecks<true>(0, 1.0);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700396 objectArray->SetWithoutChecks<true>(0, h_obj.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100397 Runtime::Current()->ExitTransactionMode();
398 transaction.Abort();
399
400 // Check values have properly been restored to their original (default) value.
401 EXPECT_EQ(booleanArray->GetWithoutChecks(0), false);
402 EXPECT_EQ(byteArray->GetWithoutChecks(0), 0);
403 EXPECT_EQ(charArray->GetWithoutChecks(0), 0u);
404 EXPECT_EQ(shortArray->GetWithoutChecks(0), 0);
405 EXPECT_EQ(intArray->GetWithoutChecks(0), 0);
406 EXPECT_EQ(longArray->GetWithoutChecks(0), static_cast<int64_t>(0));
407 EXPECT_EQ(floatArray->GetWithoutChecks(0), static_cast<float>(0.0f));
408 EXPECT_EQ(doubleArray->GetWithoutChecks(0), static_cast<double>(0.0f));
409 EXPECT_EQ(objectArray->GetWithoutChecks(0), nullptr);
410}
411
412TEST_F(TransactionTest, EmptyClass) {
413 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 StackHandleScope<2> hs(soa.Self());
415 Handle<mirror::ClassLoader> class_loader(
416 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
417 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100418
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 Handle<mirror::Class> h_klass(
420 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$EmptyStatic;", class_loader)));
421 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700422 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700423 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100424
425 Transaction transaction;
426 Runtime::Current()->EnterTransactionMode(&transaction);
Ian Rogers7b078e82014-09-10 14:44:24 -0700427 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100428 Runtime::Current()->ExitTransactionMode();
429 ASSERT_FALSE(soa.Self()->IsExceptionPending());
430}
431
432TEST_F(TransactionTest, StaticFieldClass) {
433 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700434 StackHandleScope<2> hs(soa.Self());
435 Handle<mirror::ClassLoader> class_loader(
436 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(LoadDex("Transaction"))));
437 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100438
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700439 Handle<mirror::Class> h_klass(
440 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LTransaction$StaticFieldClass;",
441 class_loader)));
442 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700443 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700444 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100445
446 Transaction transaction;
447 Runtime::Current()->EnterTransactionMode(&transaction);
Ian Rogers7b078e82014-09-10 14:44:24 -0700448 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100449 Runtime::Current()->ExitTransactionMode();
450 ASSERT_FALSE(soa.Self()->IsExceptionPending());
451}
452
453TEST_F(TransactionTest, BlacklistedClass) {
454 ScopedObjectAccess soa(Thread::Current());
455 jobject jclass_loader = LoadDex("Transaction");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700456 StackHandleScope<2> hs(soa.Self());
457 Handle<mirror::ClassLoader> class_loader(
458 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
459 ASSERT_TRUE(class_loader.Get() != nullptr);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100460
461 // Load and verify java.lang.ExceptionInInitializerError and java.lang.InternalError which will
462 // be thrown by class initialization due to native call.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 Handle<mirror::Class> h_klass(
464 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(),
465 "Ljava/lang/ExceptionInInitializerError;")));
466 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700467 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700468 ASSERT_TRUE(h_klass->IsVerified());
469 h_klass.Assign(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/InternalError;"));
470 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700471 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700472 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100473
474 // Load and verify Transaction$NativeSupport used in class initialization.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700475 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$NativeSupport;",
476 class_loader));
477 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700478 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700479 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100480
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700481 h_klass.Assign(class_linker_->FindClass(soa.Self(), "LTransaction$BlacklistedClass;",
482 class_loader));
483 ASSERT_TRUE(h_klass.Get() != nullptr);
Ian Rogers7b078e82014-09-10 14:44:24 -0700484 class_linker_->VerifyClass(soa.Self(), h_klass);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700485 ASSERT_TRUE(h_klass->IsVerified());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100486
487 Transaction transaction;
488 Runtime::Current()->EnterTransactionMode(&transaction);
Ian Rogers7b078e82014-09-10 14:44:24 -0700489 class_linker_->EnsureInitialized(soa.Self(), h_klass, true, true);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100490 Runtime::Current()->ExitTransactionMode();
491 ASSERT_TRUE(soa.Self()->IsExceptionPending());
492}
493
494
495} // namespace art