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