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