1 /*
2 * Copyright (C) 2011 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 "object.h"
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <memory>
22
23 #include "array-alloc-inl.h"
24 #include "array-inl.h"
25 #include "art_field-inl.h"
26 #include "art_method-inl.h"
27 #include "asm_support.h"
28 #include "base/enums.h"
29 #include "class-alloc-inl.h"
30 #include "class-inl.h"
31 #include "class_linker-inl.h"
32 #include "class_linker.h"
33 #include "class_root-inl.h"
34 #include "common_runtime_test.h"
35 #include "dex/dex_file.h"
36 #include "entrypoints/entrypoint_utils-inl.h"
37 #include "gc/accounting/card_table-inl.h"
38 #include "gc/heap.h"
39 #include "handle_scope-inl.h"
40 #include "iftable-inl.h"
41 #include "obj_ptr.h"
42 #include "object-inl.h"
43 #include "object_array-alloc-inl.h"
44 #include "object_array-inl.h"
45 #include "scoped_thread_state_change-inl.h"
46 #include "string-inl.h"
47
48 namespace art {
49 namespace mirror {
50
51 class ObjectTest : public CommonRuntimeTest {
52 protected:
ObjectTest()53 ObjectTest() {
54 use_boot_image_ = true; // Make the Runtime creation cheaper.
55 }
56
AssertString(int32_t expected_utf16_length,const char * utf8_in,const char * utf16_expected_le,int32_t expected_hash)57 void AssertString(int32_t expected_utf16_length,
58 const char* utf8_in,
59 const char* utf16_expected_le,
60 int32_t expected_hash)
61 REQUIRES_SHARED(Locks::mutator_lock_) {
62 std::unique_ptr<uint16_t[]> utf16_expected(new uint16_t[expected_utf16_length]);
63 for (int32_t i = 0; i < expected_utf16_length; i++) {
64 uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) |
65 ((utf16_expected_le[i*2 + 1] & 0xff) << 0));
66 utf16_expected[i] = ch;
67 }
68
69 Thread* self = Thread::Current();
70 StackHandleScope<1> hs(self);
71 Handle<String> string(
72 hs.NewHandle(String::AllocFromModifiedUtf8(self, expected_utf16_length, utf8_in)));
73 ASSERT_EQ(expected_utf16_length, string->GetLength());
74 ASSERT_EQ(string->IsValueNull(), false);
75 // strlen is necessary because the 1-character string "\x00\x00" is interpreted as ""
76 ASSERT_TRUE(string->Equals(utf8_in) || (expected_utf16_length == 1 && strlen(utf8_in) == 0));
77 for (int32_t i = 0; i < expected_utf16_length; i++) {
78 EXPECT_EQ(utf16_expected[i], string->CharAt(i));
79 }
80 EXPECT_EQ(expected_hash, string->GetHashCode());
81 }
82
83 template <class T>
AllocObjectArray(Thread * self,size_t length)84 ObjPtr<mirror::ObjectArray<T>> AllocObjectArray(Thread* self, size_t length)
85 REQUIRES_SHARED(Locks::mutator_lock_) {
86 return mirror::ObjectArray<T>::Alloc(
87 self, GetClassRoot(ClassRoot::kObjectArrayClass, class_linker_), length);
88 }
89 };
90
91 // Keep constants in sync.
TEST_F(ObjectTest,Constants)92 TEST_F(ObjectTest, Constants) {
93 EXPECT_EQ(kObjectReferenceSize, sizeof(HeapReference<Object>));
94 EXPECT_EQ(kObjectHeaderSize, sizeof(Object));
95 EXPECT_EQ(ART_METHOD_QUICK_CODE_OFFSET_32,
96 ArtMethod::EntryPointFromQuickCompiledCodeOffset(PointerSize::k32).
97 Int32Value());
98 EXPECT_EQ(ART_METHOD_QUICK_CODE_OFFSET_64,
99 ArtMethod::EntryPointFromQuickCompiledCodeOffset(PointerSize::k64).
100 Int32Value());
101 }
102
TEST_F(ObjectTest,IsInSamePackage)103 TEST_F(ObjectTest, IsInSamePackage) {
104 // Matches
105 EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/Class;"));
106 EXPECT_TRUE(Class::IsInSamePackage("LFoo;", "LBar;"));
107
108 // Mismatches
109 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/io/File;"));
110 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", "Ljava/lang/reflect/Method;"));
111 }
112
TEST_F(ObjectTest,Clone)113 TEST_F(ObjectTest, Clone) {
114 ScopedObjectAccess soa(Thread::Current());
115 StackHandleScope<2> hs(soa.Self());
116 Handle<ObjectArray<Object>> a1(hs.NewHandle(AllocObjectArray<Object>(soa.Self(), 256)));
117 size_t s1 = a1->SizeOf();
118 ObjPtr<Object> clone = Object::Clone(a1, soa.Self());
119 EXPECT_EQ(s1, clone->SizeOf());
120 EXPECT_TRUE(clone->GetClass() == a1->GetClass());
121 }
122
TEST_F(ObjectTest,AllocObjectArray)123 TEST_F(ObjectTest, AllocObjectArray) {
124 ScopedObjectAccess soa(Thread::Current());
125 StackHandleScope<3> hs(soa.Self());
126 Handle<ObjectArray<Object>> oa(hs.NewHandle(AllocObjectArray<Object>(soa.Self(), 2)));
127 EXPECT_EQ(2, oa->GetLength());
128 EXPECT_TRUE(oa->Get(0) == nullptr);
129 EXPECT_TRUE(oa->Get(1) == nullptr);
130 oa->Set<false>(0, oa.Get());
131 EXPECT_TRUE(oa->Get(0) == oa.Get());
132 EXPECT_TRUE(oa->Get(1) == nullptr);
133 oa->Set<false>(1, oa.Get());
134 EXPECT_TRUE(oa->Get(0) == oa.Get());
135 EXPECT_TRUE(oa->Get(1) == oa.Get());
136
137 Handle<Class> aioobe = hs.NewHandle(
138 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;"));
139
140 EXPECT_TRUE(oa->Get(-1) == nullptr);
141 EXPECT_TRUE(soa.Self()->IsExceptionPending());
142 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
143 soa.Self()->ClearException();
144
145 EXPECT_TRUE(oa->Get(2) == nullptr);
146 EXPECT_TRUE(soa.Self()->IsExceptionPending());
147 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
148 soa.Self()->ClearException();
149
150 ASSERT_TRUE(oa->GetClass() != nullptr);
151 Handle<mirror::Class> klass(hs.NewHandle(oa->GetClass()));
152 ASSERT_EQ(2U, klass->NumDirectInterfaces());
153 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"),
154 klass->GetDirectInterface(0));
155 EXPECT_OBJ_PTR_EQ(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"),
156 klass->GetDirectInterface(1));
157 }
158
TEST_F(ObjectTest,AllocArray)159 TEST_F(ObjectTest, AllocArray) {
160 ScopedObjectAccess soa(Thread::Current());
161 StackHandleScope<2> hs(soa.Self());
162 MutableHandle<Class> c = hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[I"));
163 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
164 MutableHandle<Array> a = hs.NewHandle(
165 Array::Alloc(soa.Self(), c.Get(), 1, c->GetComponentSizeShift(), allocator_type));
166 EXPECT_TRUE(c.Get() == a->GetClass());
167 EXPECT_EQ(1, a->GetLength());
168
169 c.Assign(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"));
170 a.Assign(Array::Alloc(soa.Self(), c.Get(), 1, c->GetComponentSizeShift(), allocator_type));
171 EXPECT_TRUE(c.Get() == a->GetClass());
172 EXPECT_EQ(1, a->GetLength());
173
174 c.Assign(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"));
175 a.Assign(Array::Alloc(soa.Self(), c.Get(), 1, c->GetComponentSizeShift(), allocator_type));
176 EXPECT_TRUE(c.Get() == a->GetClass());
177 EXPECT_EQ(1, a->GetLength());
178 }
179
TEST_F(ObjectTest,AllocArray_FillUsable)180 TEST_F(ObjectTest, AllocArray_FillUsable) {
181 ScopedObjectAccess soa(Thread::Current());
182 StackHandleScope<2> hs(soa.Self());
183 MutableHandle<Class> c = hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[B"));
184 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
185 MutableHandle<Array> a = hs.NewHandle(
186 Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
187 soa.Self(), c.Get(), 1, c->GetComponentSizeShift(), allocator_type));
188 EXPECT_TRUE(c.Get() == a->GetClass());
189 EXPECT_LE(1, a->GetLength());
190
191 c.Assign(class_linker_->FindSystemClass(soa.Self(), "[I"));
192 a.Assign(Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
193 soa.Self(), c.Get(), 2, c->GetComponentSizeShift(), allocator_type));
194 EXPECT_TRUE(c.Get() == a->GetClass());
195 EXPECT_LE(2, a->GetLength());
196
197 c.Assign(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"));
198 a.Assign(Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
199 soa.Self(), c.Get(), 2, c->GetComponentSizeShift(), allocator_type));
200 EXPECT_TRUE(c.Get() == a->GetClass());
201 EXPECT_LE(2, a->GetLength());
202
203 c.Assign(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"));
204 a.Assign(Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
205 soa.Self(), c.Get(), 2, c->GetComponentSizeShift(), allocator_type));
206 EXPECT_TRUE(c.Get() == a->GetClass());
207 EXPECT_LE(2, a->GetLength());
208 }
209
210 template<typename ArrayT>
TestPrimitiveArray(ClassLinker * cl)211 void TestPrimitiveArray(ClassLinker* cl) {
212 ScopedObjectAccess soa(Thread::Current());
213 using T = typename ArrayT::ElementType;
214
215 StackHandleScope<2> hs(soa.Self());
216 Handle<ArrayT> a = hs.NewHandle(ArrayT::Alloc(soa.Self(), 2));
217 EXPECT_EQ(2, a->GetLength());
218 EXPECT_EQ(0, a->Get(0));
219 EXPECT_EQ(0, a->Get(1));
220 a->Set(0, T(123));
221 EXPECT_EQ(T(123), a->Get(0));
222 EXPECT_EQ(0, a->Get(1));
223 a->Set(1, T(321));
224 EXPECT_EQ(T(123), a->Get(0));
225 EXPECT_EQ(T(321), a->Get(1));
226
227 Handle<Class> aioobe = hs.NewHandle(
228 cl->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;"));
229
230 EXPECT_EQ(0, a->Get(-1));
231 EXPECT_TRUE(soa.Self()->IsExceptionPending());
232 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
233 soa.Self()->ClearException();
234
235 EXPECT_EQ(0, a->Get(2));
236 EXPECT_TRUE(soa.Self()->IsExceptionPending());
237 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
238 soa.Self()->ClearException();
239 }
240
TEST_F(ObjectTest,PrimitiveArray_Boolean_Alloc)241 TEST_F(ObjectTest, PrimitiveArray_Boolean_Alloc) {
242 TestPrimitiveArray<BooleanArray>(class_linker_);
243 }
TEST_F(ObjectTest,PrimitiveArray_Byte_Alloc)244 TEST_F(ObjectTest, PrimitiveArray_Byte_Alloc) {
245 TestPrimitiveArray<ByteArray>(class_linker_);
246 }
TEST_F(ObjectTest,PrimitiveArray_Char_Alloc)247 TEST_F(ObjectTest, PrimitiveArray_Char_Alloc) {
248 TestPrimitiveArray<CharArray>(class_linker_);
249 }
TEST_F(ObjectTest,PrimitiveArray_Int_Alloc)250 TEST_F(ObjectTest, PrimitiveArray_Int_Alloc) {
251 TestPrimitiveArray<IntArray>(class_linker_);
252 }
TEST_F(ObjectTest,PrimitiveArray_Long_Alloc)253 TEST_F(ObjectTest, PrimitiveArray_Long_Alloc) {
254 TestPrimitiveArray<LongArray>(class_linker_);
255 }
TEST_F(ObjectTest,PrimitiveArray_Short_Alloc)256 TEST_F(ObjectTest, PrimitiveArray_Short_Alloc) {
257 TestPrimitiveArray<ShortArray>(class_linker_);
258 }
259
TEST_F(ObjectTest,PointerArrayWriteRead)260 TEST_F(ObjectTest, PointerArrayWriteRead) {
261 ScopedObjectAccess soa(Thread::Current());
262 StackHandleScope<2> hs(soa.Self());
263
264 Handle<PointerArray> a32 =
265 hs.NewHandle(ObjPtr<PointerArray>::DownCast<Array>(IntArray::Alloc(soa.Self(), 1)));
266 ASSERT_TRUE(a32 != nullptr);
267 ASSERT_EQ(1, a32->GetLength());
268 EXPECT_EQ(0u, (a32->GetElementPtrSize<uint32_t, PointerSize::k32>(0u)));
269 EXPECT_EQ(0u, (a32->GetElementPtrSizeUnchecked<uint32_t, PointerSize::k32>(0u)));
270 for (uint32_t value : { 0u, 1u, 0x7fffffffu, 0x80000000u, 0xffffffffu }) {
271 a32->SetElementPtrSize(0u, value, PointerSize::k32);
272 EXPECT_EQ(value, (a32->GetElementPtrSize<uint32_t, PointerSize::k32>(0u)));
273 EXPECT_EQ(value, (a32->GetElementPtrSizeUnchecked<uint32_t, PointerSize::k32>(0u)));
274 // Check that the value matches also when retrieved as `uint64_t`.
275 // This is a regression test for unintended sign-extension. b/155780442
276 // (Using `uint64_t` rather than `uintptr_t`, so that the 32-bit test checks this too.)
277 EXPECT_EQ(value, (a32->GetElementPtrSize<uint64_t, PointerSize::k32>(0u)));
278 EXPECT_EQ(value, (a32->GetElementPtrSizeUnchecked<uint64_t, PointerSize::k32>(0u)));
279 }
280
281 Handle<PointerArray> a64 =
282 hs.NewHandle(ObjPtr<PointerArray>::DownCast<Array>(LongArray::Alloc(soa.Self(), 1)));
283 ASSERT_TRUE(a64 != nullptr);
284 ASSERT_EQ(1, a64->GetLength());
285 EXPECT_EQ(0u, (a64->GetElementPtrSize<uint32_t, PointerSize::k64>(0u)));
286 EXPECT_EQ(0u, (a64->GetElementPtrSizeUnchecked<uint32_t, PointerSize::k64>(0u)));
287 for (uint64_t value : { UINT64_C(0),
288 UINT64_C(1),
289 UINT64_C(0x7fffffff),
290 UINT64_C(0x80000000),
291 UINT64_C(0xffffffff),
292 UINT64_C(0x100000000),
293 UINT64_C(0x7fffffffffffffff),
294 UINT64_C(0x8000000000000000),
295 UINT64_C(0xffffffffffffffff) }) {
296 a64->SetElementPtrSize(0u, value, PointerSize::k64);
297 EXPECT_EQ(value, (a64->GetElementPtrSize<uint64_t, PointerSize::k64>(0u)));
298 EXPECT_EQ(value, (a64->GetElementPtrSizeUnchecked<uint64_t, PointerSize::k64>(0u)));
299 }
300 }
301
TEST_F(ObjectTest,PrimitiveArray_Double_Alloc)302 TEST_F(ObjectTest, PrimitiveArray_Double_Alloc) {
303 using ArrayT = DoubleArray;
304 ScopedObjectAccess soa(Thread::Current());
305 using T = typename ArrayT::ElementType;
306
307 StackHandleScope<2> hs(soa.Self());
308 Handle<ArrayT> a = hs.NewHandle(ArrayT::Alloc(soa.Self(), 2));
309 EXPECT_EQ(2, a->GetLength());
310 EXPECT_DOUBLE_EQ(0, a->Get(0));
311 EXPECT_DOUBLE_EQ(0, a->Get(1));
312 a->Set(0, T(123));
313 EXPECT_DOUBLE_EQ(T(123), a->Get(0));
314 EXPECT_DOUBLE_EQ(0, a->Get(1));
315 a->Set(1, T(321));
316 EXPECT_DOUBLE_EQ(T(123), a->Get(0));
317 EXPECT_DOUBLE_EQ(T(321), a->Get(1));
318
319 Handle<Class> aioobe = hs.NewHandle(
320 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;"));
321
322 EXPECT_DOUBLE_EQ(0, a->Get(-1));
323 EXPECT_TRUE(soa.Self()->IsExceptionPending());
324 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
325 soa.Self()->ClearException();
326
327 EXPECT_DOUBLE_EQ(0, a->Get(2));
328 EXPECT_TRUE(soa.Self()->IsExceptionPending());
329 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
330 soa.Self()->ClearException();
331 }
332
TEST_F(ObjectTest,PrimitiveArray_Float_Alloc)333 TEST_F(ObjectTest, PrimitiveArray_Float_Alloc) {
334 using ArrayT = FloatArray;
335 ScopedObjectAccess soa(Thread::Current());
336 using T = typename ArrayT::ElementType;
337
338 StackHandleScope<2> hs(soa.Self());
339 Handle<ArrayT> a = hs.NewHandle(ArrayT::Alloc(soa.Self(), 2));
340 EXPECT_FLOAT_EQ(2, a->GetLength());
341 EXPECT_FLOAT_EQ(0, a->Get(0));
342 EXPECT_FLOAT_EQ(0, a->Get(1));
343 a->Set(0, T(123));
344 EXPECT_FLOAT_EQ(T(123), a->Get(0));
345 EXPECT_FLOAT_EQ(0, a->Get(1));
346 a->Set(1, T(321));
347 EXPECT_FLOAT_EQ(T(123), a->Get(0));
348 EXPECT_FLOAT_EQ(T(321), a->Get(1));
349
350 Handle<Class> aioobe = hs.NewHandle(
351 class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/ArrayIndexOutOfBoundsException;"));
352
353 EXPECT_FLOAT_EQ(0, a->Get(-1));
354 EXPECT_TRUE(soa.Self()->IsExceptionPending());
355 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
356 soa.Self()->ClearException();
357
358 EXPECT_FLOAT_EQ(0, a->Get(2));
359 EXPECT_TRUE(soa.Self()->IsExceptionPending());
360 EXPECT_OBJ_PTR_EQ(aioobe.Get(), soa.Self()->GetException()->GetClass());
361 soa.Self()->ClearException();
362 }
363
364
TEST_F(ObjectTest,CreateMultiArray)365 TEST_F(ObjectTest, CreateMultiArray) {
366 ScopedObjectAccess soa(Thread::Current());
367
368 StackHandleScope<4> hs(soa.Self());
369 Handle<Class> int_class(hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "I")));
370 Handle<Class> int_array_class = hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[I"));
371 MutableHandle<IntArray> dims(hs.NewHandle(IntArray::Alloc(soa.Self(), 1)));
372 dims->Set<false>(0, 1);
373 MutableHandle<Array> multi = hs.NewHandle(Array::CreateMultiArray(soa.Self(), int_class, dims));
374 EXPECT_OBJ_PTR_EQ(int_array_class.Get(), multi->GetClass());
375 EXPECT_EQ(1, multi->GetLength());
376
377 dims->Set<false>(0, -1);
378 multi.Assign(Array::CreateMultiArray(soa.Self(), int_class, dims));
379 EXPECT_TRUE(soa.Self()->IsExceptionPending());
380 EXPECT_EQ(mirror::Class::PrettyDescriptor(soa.Self()->GetException()->GetClass()),
381 "java.lang.NegativeArraySizeException");
382 soa.Self()->ClearException();
383
384 dims.Assign(IntArray::Alloc(soa.Self(), 2));
385 for (int i = 1; i < 20; ++i) {
386 for (int j = 0; j < 20; ++j) {
387 dims->Set<false>(0, i);
388 dims->Set<false>(1, j);
389 multi.Assign(Array::CreateMultiArray(soa.Self(), int_class, dims));
390 ObjPtr<mirror::Class> expected_class = class_linker_->FindSystemClass(soa.Self(), "[[I");
391 EXPECT_OBJ_PTR_EQ(multi->GetClass(), expected_class);
392 EXPECT_EQ(i, multi->GetLength());
393 for (int k = 0; k < i; ++k) {
394 ObjPtr<Array> outer = multi->AsObjectArray<Array>()->Get(k);
395 EXPECT_OBJ_PTR_EQ(int_array_class.Get(), outer->GetClass());
396 EXPECT_EQ(j, outer->GetLength());
397 }
398 }
399 }
400 }
401
TEST_F(ObjectTest,StaticFieldFromCode)402 TEST_F(ObjectTest, StaticFieldFromCode) {
403 // pretend we are trying to access 'Static.s0' from StaticsFromCode.<clinit>
404 ScopedObjectAccess soa(Thread::Current());
405 jobject class_loader = LoadDex("StaticsFromCode");
406 const DexFile* dex_file = GetFirstDexFile(class_loader);
407
408 StackHandleScope<3> hs(soa.Self());
409 Handle<mirror::ClassLoader> loader(hs.NewHandle(soa.Decode<ClassLoader>(class_loader)));
410 Handle<Class> klass =
411 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LStaticsFromCode;", loader));
412 ArtMethod* clinit = klass->FindClassInitializer(kRuntimePointerSize);
413 const dex::TypeId* klass_type_id = dex_file->FindTypeId("LStaticsFromCode;");
414 ASSERT_TRUE(klass_type_id != nullptr);
415
416 const dex::TypeId* type_type_id = dex_file->FindTypeId("Ljava/lang/Object;");
417 ASSERT_TRUE(type_type_id != nullptr);
418
419 const dex::StringId* name_str_id = dex_file->FindStringId("s0");
420 ASSERT_TRUE(name_str_id != nullptr);
421
422 const dex::FieldId* field_id = dex_file->FindFieldId(
423 *klass_type_id, *name_str_id, *type_type_id);
424 ASSERT_TRUE(field_id != nullptr);
425 uint32_t field_idx = dex_file->GetIndexForFieldId(*field_id);
426
427 ArtField* field = FindFieldFromCode<StaticObjectRead>(field_idx,
428 clinit,
429 Thread::Current(),
430 sizeof(HeapReference<Object>));
431 ObjPtr<Object> s0 = field->GetObj(klass.Get());
432 EXPECT_TRUE(s0 != nullptr) << field->PrettyField();
433
434 Handle<CharArray> char_array(hs.NewHandle(CharArray::Alloc(soa.Self(), 0)));
435 field->SetObj<false>(field->GetDeclaringClass(), char_array.Get());
436 EXPECT_OBJ_PTR_EQ(char_array.Get(), field->GetObj(klass.Get()));
437
438 field->SetObj<false>(field->GetDeclaringClass(), nullptr);
439 EXPECT_EQ(nullptr, field->GetObj(klass.Get()));
440
441 // TODO: more exhaustive tests of all 6 cases of ArtField::*FromCode
442 }
443
TEST_F(ObjectTest,String)444 TEST_F(ObjectTest, String) {
445 ScopedObjectAccess soa(Thread::Current());
446 // Test the empty string.
447 AssertString(0, "", "", 0);
448
449 // Test one-byte characters.
450 AssertString(1, " ", "\x00\x20", 0x20);
451 AssertString(1, "", "\x00\x00", 0);
452 AssertString(1, "\x7f", "\x00\x7f", 0x7f);
453 AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
454
455 // Test two-byte characters.
456 AssertString(1, "\xc2\x80", "\x00\x80", 0x80);
457 AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666);
458 AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff);
459 AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69",
460 (31 * ((31 * 0x68) + 0x0666)) + 0x69);
461
462 // Test three-byte characters.
463 AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800);
464 AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234);
465 AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff);
466 AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69",
467 (31 * ((31 * 0x68) + 0x1234)) + 0x69);
468
469 // Test four-byte characters.
470 AssertString(2, "\xf0\x9f\x8f\xa0", "\xd8\x3c\xdf\xe0", (31 * 0xd83c) + 0xdfe0);
471 AssertString(2, "\xf0\x9f\x9a\x80", "\xd8\x3d\xde\x80", (31 * 0xd83d) + 0xde80);
472 AssertString(4, "h\xf0\x9f\x9a\x80i", "\x00\x68\xd8\x3d\xde\x80\x00\x69",
473 (31 * (31 * (31 * 0x68 + 0xd83d) + 0xde80) + 0x69));
474 }
475
TEST_F(ObjectTest,StringEqualsUtf8)476 TEST_F(ObjectTest, StringEqualsUtf8) {
477 ScopedObjectAccess soa(Thread::Current());
478 StackHandleScope<2> hs(soa.Self());
479 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
480 EXPECT_TRUE(string->Equals("android"));
481 EXPECT_FALSE(string->Equals("Android"));
482 EXPECT_FALSE(string->Equals("ANDROID"));
483 EXPECT_FALSE(string->Equals(""));
484 EXPECT_FALSE(string->Equals("and"));
485 EXPECT_FALSE(string->Equals("androids"));
486
487 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
488 EXPECT_TRUE(empty->Equals(""));
489 EXPECT_FALSE(empty->Equals("a"));
490 }
491
TEST_F(ObjectTest,StringEquals)492 TEST_F(ObjectTest, StringEquals) {
493 ScopedObjectAccess soa(Thread::Current());
494 StackHandleScope<3> hs(soa.Self());
495 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
496 Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
497 EXPECT_TRUE(string->Equals(string_2.Get()));
498 EXPECT_FALSE(string->Equals("Android"));
499 EXPECT_FALSE(string->Equals("ANDROID"));
500 EXPECT_FALSE(string->Equals(""));
501 EXPECT_FALSE(string->Equals("and"));
502 EXPECT_FALSE(string->Equals("androids"));
503
504 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
505 EXPECT_TRUE(empty->Equals(""));
506 EXPECT_FALSE(empty->Equals("a"));
507 }
508
TEST_F(ObjectTest,StringCompareTo)509 TEST_F(ObjectTest, StringCompareTo) {
510 ScopedObjectAccess soa(Thread::Current());
511 StackHandleScope<5> hs(soa.Self());
512 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
513 Handle<String> string_2(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
514 Handle<String> string_3(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "Android")));
515 Handle<String> string_4(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "and")));
516 Handle<String> string_5(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
517 EXPECT_EQ(0, string->CompareTo(string_2.Get()));
518 EXPECT_LT(0, string->CompareTo(string_3.Get()));
519 EXPECT_GT(0, string_3->CompareTo(string.Get()));
520 EXPECT_LT(0, string->CompareTo(string_4.Get()));
521 EXPECT_GT(0, string_4->CompareTo(string.Get()));
522 EXPECT_LT(0, string->CompareTo(string_5.Get()));
523 EXPECT_GT(0, string_5->CompareTo(string.Get()));
524 }
525
TEST_F(ObjectTest,StringLength)526 TEST_F(ObjectTest, StringLength) {
527 ScopedObjectAccess soa(Thread::Current());
528 StackHandleScope<1> hs(soa.Self());
529 Handle<String> string(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "android")));
530 EXPECT_EQ(string->GetLength(), 7);
531 EXPECT_EQ(string->GetModifiedUtf8Length(), 7);
532 }
533
TEST_F(ObjectTest,DescriptorCompare)534 TEST_F(ObjectTest, DescriptorCompare) {
535 // Two classloaders conflicts in compile_time_class_paths_.
536 ScopedObjectAccess soa(Thread::Current());
537 ClassLinker* linker = class_linker_;
538
539 jobject jclass_loader_1 = LoadDex("ProtoCompare");
540 jobject jclass_loader_2 = LoadDex("ProtoCompare2");
541 StackHandleScope<4> hs(soa.Self());
542 Handle<ClassLoader> class_loader_1(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader_1)));
543 Handle<ClassLoader> class_loader_2(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader_2)));
544
545 Handle<Class> klass1 =
546 hs.NewHandle(linker->FindClass(soa.Self(), "LProtoCompare;", class_loader_1));
547 ASSERT_TRUE(klass1 != nullptr);
548 Handle<Class> klass2 =
549 hs.NewHandle(linker->FindClass(soa.Self(), "LProtoCompare2;", class_loader_2));
550 ASSERT_TRUE(klass2 != nullptr);
551
552 ArtMethod* m1_1 = klass1->GetVirtualMethod(0, kRuntimePointerSize);
553 EXPECT_STREQ(m1_1->GetName(), "m1");
554 ArtMethod* m2_1 = klass1->GetVirtualMethod(1, kRuntimePointerSize);
555 EXPECT_STREQ(m2_1->GetName(), "m2");
556 ArtMethod* m3_1 = klass1->GetVirtualMethod(2, kRuntimePointerSize);
557 EXPECT_STREQ(m3_1->GetName(), "m3");
558 ArtMethod* m4_1 = klass1->GetVirtualMethod(3, kRuntimePointerSize);
559 EXPECT_STREQ(m4_1->GetName(), "m4");
560
561 ArtMethod* m1_2 = klass2->GetVirtualMethod(0, kRuntimePointerSize);
562 EXPECT_STREQ(m1_2->GetName(), "m1");
563 ArtMethod* m2_2 = klass2->GetVirtualMethod(1, kRuntimePointerSize);
564 EXPECT_STREQ(m2_2->GetName(), "m2");
565 ArtMethod* m3_2 = klass2->GetVirtualMethod(2, kRuntimePointerSize);
566 EXPECT_STREQ(m3_2->GetName(), "m3");
567 ArtMethod* m4_2 = klass2->GetVirtualMethod(3, kRuntimePointerSize);
568 EXPECT_STREQ(m4_2->GetName(), "m4");
569 }
570
TEST_F(ObjectTest,StringHashCode)571 TEST_F(ObjectTest, StringHashCode) {
572 ScopedObjectAccess soa(Thread::Current());
573 StackHandleScope<3> hs(soa.Self());
574 Handle<String> empty(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "")));
575 Handle<String> A(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "A")));
576 Handle<String> ABC(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
577
578 EXPECT_EQ(0, empty->GetHashCode());
579 EXPECT_EQ(65, A->GetHashCode());
580 EXPECT_EQ(64578, ABC->GetHashCode());
581 }
582
TEST_F(ObjectTest,InstanceOf)583 TEST_F(ObjectTest, InstanceOf) {
584 ScopedObjectAccess soa(Thread::Current());
585 jobject jclass_loader = LoadDex("XandY");
586 StackHandleScope<10> hs(soa.Self());
587 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader)));
588
589 Handle<Class> X = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LX;", class_loader));
590 Handle<Class> Y = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LY;", class_loader));
591 ASSERT_TRUE(X != nullptr);
592 ASSERT_TRUE(Y != nullptr);
593
594 Handle<Object> x(hs.NewHandle(X->AllocObject(soa.Self())));
595 Handle<Object> y(hs.NewHandle(Y->AllocObject(soa.Self())));
596 ASSERT_TRUE(x != nullptr);
597 ASSERT_TRUE(y != nullptr);
598
599 EXPECT_TRUE(x->InstanceOf(X.Get()));
600 EXPECT_FALSE(x->InstanceOf(Y.Get()));
601 EXPECT_TRUE(y->InstanceOf(X.Get()));
602 EXPECT_TRUE(y->InstanceOf(Y.Get()));
603
604 Handle<Class> java_lang_Class =
605 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Class;"));
606 Handle<Class> Object_array_class =
607 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"));
608
609 EXPECT_FALSE(java_lang_Class->InstanceOf(Object_array_class.Get()));
610 EXPECT_TRUE(Object_array_class->InstanceOf(java_lang_Class.Get()));
611
612 // All array classes implement Cloneable and Serializable.
613 Handle<Object> array =
614 hs.NewHandle<Object>(ObjectArray<Object>::Alloc(soa.Self(), Object_array_class.Get(), 1));
615 Handle<Class> java_lang_Cloneable =
616 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Cloneable;"));
617 Handle<Class> java_io_Serializable =
618 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"));
619 EXPECT_TRUE(array->InstanceOf(java_lang_Cloneable.Get()));
620 EXPECT_TRUE(array->InstanceOf(java_io_Serializable.Get()));
621 }
622
TEST_F(ObjectTest,IsAssignableFrom)623 TEST_F(ObjectTest, IsAssignableFrom) {
624 ScopedObjectAccess soa(Thread::Current());
625 jobject jclass_loader = LoadDex("XandY");
626 StackHandleScope<5> hs(soa.Self());
627 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader)));
628 Handle<Class> X = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LX;", class_loader));
629 Handle<Class> Y = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LY;", class_loader));
630
631 EXPECT_TRUE(X->IsAssignableFrom(X.Get()));
632 EXPECT_TRUE(X->IsAssignableFrom(Y.Get()));
633 EXPECT_FALSE(Y->IsAssignableFrom(X.Get()));
634 EXPECT_TRUE(Y->IsAssignableFrom(Y.Get()));
635
636 // class final String implements CharSequence, ..
637 Handle<Class> string =
638 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"));
639 Handle<Class> charseq =
640 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/CharSequence;"));
641 // Can String be assigned to CharSequence without a cast?
642 EXPECT_TRUE(charseq->IsAssignableFrom(string.Get()));
643 // Can CharSequence be assigned to String without a cast?
644 EXPECT_FALSE(string->IsAssignableFrom(charseq.Get()));
645
646 // Primitive types are only assignable to themselves
647 const char* prims = "ZBCSIJFD";
648 std::vector<ObjPtr<Class>> prim_types(strlen(prims));
649 for (size_t i = 0; i < strlen(prims); i++) {
650 prim_types[i] = class_linker_->FindPrimitiveClass(prims[i]);
651 }
652 for (size_t i = 0; i < strlen(prims); i++) {
653 for (size_t j = 0; i < strlen(prims); i++) {
654 if (i == j) {
655 EXPECT_TRUE(prim_types[i]->IsAssignableFrom(prim_types[j]));
656 } else {
657 EXPECT_FALSE(prim_types[i]->IsAssignableFrom(prim_types[j]));
658 }
659 }
660 }
661 }
662
TEST_F(ObjectTest,IsAssignableFromArray)663 TEST_F(ObjectTest, IsAssignableFromArray) {
664 ScopedObjectAccess soa(Thread::Current());
665 jobject jclass_loader = LoadDex("XandY");
666 StackHandleScope<14> hs(soa.Self());
667 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader)));
668 Handle<Class> X = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LX;", class_loader));
669 Handle<Class> Y = hs.NewHandle(class_linker_->FindClass(soa.Self(), "LY;", class_loader));
670 ASSERT_TRUE(X != nullptr);
671 ASSERT_TRUE(Y != nullptr);
672
673 Handle<Class> YA = hs.NewHandle(class_linker_->FindClass(soa.Self(), "[LY;", class_loader));
674 Handle<Class> YAA = hs.NewHandle(class_linker_->FindClass(soa.Self(), "[[LY;", class_loader));
675 ASSERT_TRUE(YA != nullptr);
676 ASSERT_TRUE(YAA != nullptr);
677
678 Handle<Class> XAA = hs.NewHandle(class_linker_->FindClass(soa.Self(), "[[LX;", class_loader));
679 ASSERT_TRUE(XAA != nullptr);
680
681 Handle<Class> O = hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
682 Handle<Class> OA =
683 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;"));
684 Handle<Class> OAA =
685 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Object;"));
686 Handle<Class> OAAA =
687 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[[Ljava/lang/Object;"));
688 ASSERT_TRUE(O != nullptr);
689 ASSERT_TRUE(OA != nullptr);
690 ASSERT_TRUE(OAA != nullptr);
691 ASSERT_TRUE(OAAA != nullptr);
692
693 Handle<Class> S =
694 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "Ljava/io/Serializable;"));
695 Handle<Class> SA =
696 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/io/Serializable;"));
697 Handle<Class> SAA =
698 hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/io/Serializable;"));
699 ASSERT_TRUE(S != nullptr);
700 ASSERT_TRUE(SA != nullptr);
701 ASSERT_TRUE(SAA != nullptr);
702
703 Handle<Class> IA = hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[I"));
704 ASSERT_TRUE(IA != nullptr);
705
706 EXPECT_TRUE(YAA->IsAssignableFrom(YAA.Get())); // identity
707 EXPECT_TRUE(XAA->IsAssignableFrom(YAA.Get())); // element superclass
708 EXPECT_FALSE(YAA->IsAssignableFrom(XAA.Get()));
709 EXPECT_FALSE(Y->IsAssignableFrom(YAA.Get()));
710 EXPECT_FALSE(YA->IsAssignableFrom(YAA.Get()));
711 EXPECT_TRUE(O->IsAssignableFrom(YAA.Get())); // everything is an Object
712 EXPECT_TRUE(OA->IsAssignableFrom(YAA.Get()));
713 EXPECT_TRUE(OAA->IsAssignableFrom(YAA.Get()));
714 EXPECT_TRUE(S->IsAssignableFrom(YAA.Get())); // all arrays are Serializable
715 EXPECT_TRUE(SA->IsAssignableFrom(YAA.Get()));
716 EXPECT_FALSE(SAA->IsAssignableFrom(YAA.Get())); // unless Y was Serializable
717
718 EXPECT_FALSE(IA->IsAssignableFrom(OA.Get()));
719 EXPECT_FALSE(OA->IsAssignableFrom(IA.Get()));
720 EXPECT_TRUE(O->IsAssignableFrom(IA.Get()));
721 }
722
TEST_F(ObjectTest,FindInstanceField)723 TEST_F(ObjectTest, FindInstanceField) {
724 ScopedObjectAccess soa(Thread::Current());
725 StackHandleScope<1> hs(soa.Self());
726 Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
727 ASSERT_TRUE(s != nullptr);
728 ObjPtr<Class> c = s->GetClass();
729 ASSERT_TRUE(c != nullptr);
730
731 // Wrong type.
732 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "J") == nullptr);
733 EXPECT_TRUE(c->FindInstanceField("count", "J") == nullptr);
734
735 // Wrong name.
736 EXPECT_TRUE(c->FindDeclaredInstanceField("Count", "I") == nullptr);
737 EXPECT_TRUE(c->FindInstanceField("Count", "I") == nullptr);
738
739 // Right name and type.
740 ArtField* f1 = c->FindDeclaredInstanceField("count", "I");
741 ArtField* f2 = c->FindInstanceField("count", "I");
742 EXPECT_TRUE(f1 != nullptr);
743 EXPECT_TRUE(f2 != nullptr);
744 EXPECT_EQ(f1, f2);
745
746 // TODO: check that s.count == 3.
747
748 // Ensure that we handle superclass fields correctly...
749 c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/StringBuilder;");
750 ASSERT_TRUE(c != nullptr);
751 // No StringBuilder.count...
752 EXPECT_TRUE(c->FindDeclaredInstanceField("count", "I") == nullptr);
753 // ...but there is an AbstractStringBuilder.count.
754 EXPECT_TRUE(c->FindInstanceField("count", "I") != nullptr);
755 }
756
TEST_F(ObjectTest,FindStaticField)757 TEST_F(ObjectTest, FindStaticField) {
758 ScopedObjectAccess soa(Thread::Current());
759 StackHandleScope<4> hs(soa.Self());
760 Handle<String> s(hs.NewHandle(String::AllocFromModifiedUtf8(soa.Self(), "ABC")));
761 ASSERT_TRUE(s != nullptr);
762 Handle<Class> c(hs.NewHandle(s->GetClass()));
763 ASSERT_TRUE(c != nullptr);
764
765 // Wrong type.
766 EXPECT_TRUE(c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "I") == nullptr);
767 EXPECT_TRUE(c->FindStaticField("CASE_INSENSITIVE_ORDER", "I") == nullptr);
768
769 // Wrong name.
770 EXPECT_TRUE(c->FindDeclaredStaticField(
771 "cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == nullptr);
772 EXPECT_TRUE(c->FindStaticField("cASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;") == nullptr);
773
774 // Right name and type.
775 ArtField* f1 = c->FindDeclaredStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
776 ArtField* f2 = c->FindStaticField("CASE_INSENSITIVE_ORDER", "Ljava/util/Comparator;");
777 EXPECT_TRUE(f1 != nullptr);
778 EXPECT_TRUE(f2 != nullptr);
779 EXPECT_EQ(f1, f2);
780
781 // TODO: test static fields via superclasses.
782 // TODO: test static fields via interfaces.
783 // TODO: test that interfaces trump superclasses.
784 }
785
TEST_F(ObjectTest,IdentityHashCode)786 TEST_F(ObjectTest, IdentityHashCode) {
787 // Regression test for b/19046417 which had an infinite loop if the
788 // (seed & LockWord::kHashMask) == 0. seed 0 triggered the infinite loop since we did the check
789 // before the CAS which resulted in the same seed the next loop iteration.
790 mirror::Object::SetHashCodeSeed(0);
791 int32_t hash_code = mirror::Object::GenerateIdentityHashCode();
792 EXPECT_NE(hash_code, 0);
793 }
794
TEST_F(ObjectTest,ObjectPointer)795 TEST_F(ObjectTest, ObjectPointer) {
796 ScopedObjectAccess soa(Thread::Current());
797 jobject jclass_loader = LoadDex("XandY");
798 StackHandleScope<2> hs(soa.Self());
799 Handle<ClassLoader> class_loader(hs.NewHandle(soa.Decode<ClassLoader>(jclass_loader)));
800 Handle<mirror::Class> h_X(
801 hs.NewHandle(class_linker_->FindClass(soa.Self(), "LX;", class_loader)));
802
803 if (kObjPtrPoisoning) {
804 ObjPtr<mirror::Object> null_ptr;
805 EXPECT_TRUE(null_ptr.IsNull());
806 EXPECT_TRUE(null_ptr.IsValid());
807 EXPECT_TRUE(null_ptr.Ptr() == nullptr);
808 EXPECT_TRUE(null_ptr == nullptr);
809 EXPECT_TRUE(null_ptr == null_ptr);
810 EXPECT_FALSE(null_ptr != null_ptr);
811 EXPECT_FALSE(null_ptr != nullptr);
812 null_ptr.AssertValid();
813 ObjPtr<Class> X(h_X.Get());
814 EXPECT_TRUE(!X.IsNull());
815 EXPECT_TRUE(X.IsValid());
816 EXPECT_TRUE(X.Ptr() != nullptr);
817 EXPECT_OBJ_PTR_EQ(h_X.Get(), X);
818 // FindClass may cause thread suspension, it should invalidate X.
819 ObjPtr<Class> Y(class_linker_->FindClass(soa.Self(), "LY;", class_loader));
820 EXPECT_TRUE(!Y.IsNull());
821 EXPECT_TRUE(Y.IsValid());
822 EXPECT_TRUE(Y.Ptr() != nullptr);
823
824 // Should IsNull be safe to call on null ObjPtr? I'll allow it for now.
825 EXPECT_TRUE(!X.IsNull());
826 EXPECT_TRUE(!X.IsValid());
827 // Make X valid again by copying out of handle.
828 X.Assign(h_X.Get());
829 EXPECT_TRUE(!X.IsNull());
830 EXPECT_TRUE(X.IsValid());
831 EXPECT_OBJ_PTR_EQ(h_X.Get(), X);
832
833 // Allow thread suspension to invalidate Y.
834 soa.Self()->AllowThreadSuspension();
835 EXPECT_TRUE(!Y.IsNull());
836 EXPECT_TRUE(!Y.IsValid());
837 } else {
838 // Test unpoisoned.
839 ObjPtr<mirror::Object> unpoisoned;
840 EXPECT_TRUE(unpoisoned.IsNull());
841 EXPECT_TRUE(unpoisoned.IsValid());
842 EXPECT_TRUE(unpoisoned.Ptr() == nullptr);
843 EXPECT_TRUE(unpoisoned == nullptr);
844 EXPECT_TRUE(unpoisoned == unpoisoned);
845 EXPECT_FALSE(unpoisoned != unpoisoned);
846 EXPECT_FALSE(unpoisoned != nullptr);
847
848 unpoisoned = h_X.Get();
849 EXPECT_FALSE(unpoisoned.IsNull());
850 EXPECT_TRUE(unpoisoned == h_X.Get());
851 EXPECT_OBJ_PTR_EQ(unpoisoned, h_X.Get());
852 }
853 }
854
TEST_F(ObjectTest,PrettyTypeOf)855 TEST_F(ObjectTest, PrettyTypeOf) {
856 ScopedObjectAccess soa(Thread::Current());
857 EXPECT_EQ("null", mirror::Object::PrettyTypeOf(nullptr));
858
859 StackHandleScope<2> hs(soa.Self());
860 Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "")));
861 EXPECT_EQ("java.lang.String", mirror::Object::PrettyTypeOf(s.Get()));
862
863 Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2)));
864 EXPECT_EQ("short[]", mirror::Object::PrettyTypeOf(a.Get()));
865
866 ObjPtr<mirror::Class> c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
867 ASSERT_TRUE(c != nullptr);
868 ObjPtr<mirror::Object> o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
869 EXPECT_EQ("java.lang.String[]", mirror::Object::PrettyTypeOf(o));
870 EXPECT_EQ("java.lang.Class<java.lang.String[]>", mirror::Object::PrettyTypeOf(o->GetClass()));
871 }
872
873 } // namespace mirror
874 } // namespace art
875