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