1 /* 2 * Copyright (C) 2016 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 "dex_file_annotations.h" 18 19 #include <stdlib.h> 20 21 #include "android-base/stringprintf.h" 22 23 #include "art_field-inl.h" 24 #include "art_method-inl.h" 25 #include "class_linker-inl.h" 26 #include "dex_file-inl.h" 27 #include "jni_internal.h" 28 #include "jvalue-inl.h" 29 #include "mirror/field.h" 30 #include "mirror/method.h" 31 #include "reflection.h" 32 #include "thread.h" 33 34 namespace art { 35 36 using android::base::StringPrintf; 37 38 struct DexFile::AnnotationValue { 39 JValue value_; 40 uint8_t type_; 41 }; 42 43 namespace { 44 45 // A helper class that contains all the data needed to do annotation lookup. 46 class ClassData { 47 public: 48 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) 49 : ClassData(ScopedNullHandle<mirror::Class>(), // klass 50 method, 51 *method->GetDexFile(), 52 &method->GetClassDef()) {} 53 54 // Requires Scope to be able to create at least 1 handles. 55 template <typename Scope> 56 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) 57 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { } 58 59 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) 60 : ClassData(klass, // klass 61 nullptr, // method 62 klass->GetDexFile(), 63 klass->GetClassDef()) {} 64 65 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) { 66 return dex_file_; 67 } 68 69 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) { 70 return class_def_; 71 } 72 73 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) { 74 if (method_ != nullptr) { 75 return method_->GetDexCache(); 76 } else { 77 return real_klass_->GetDexCache(); 78 } 79 } 80 81 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) { 82 if (method_ != nullptr) { 83 return method_->GetDeclaringClass()->GetClassLoader(); 84 } else { 85 return real_klass_->GetClassLoader(); 86 } 87 } 88 89 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) { 90 if (method_ != nullptr) { 91 return method_->GetDeclaringClass(); 92 } else { 93 return real_klass_.Get(); 94 } 95 } 96 97 private: 98 ClassData(Handle<mirror::Class> klass, 99 ArtMethod* method, 100 const DexFile& dex_file, 101 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_) 102 : real_klass_(klass), 103 method_(method), 104 dex_file_(dex_file), 105 class_def_(class_def) { 106 DCHECK((method_ == nullptr) || real_klass_.IsNull()); 107 } 108 109 Handle<mirror::Class> real_klass_; 110 ArtMethod* method_; 111 const DexFile& dex_file_; 112 const DexFile::ClassDef* class_def_; 113 114 DISALLOW_COPY_AND_ASSIGN(ClassData); 115 }; 116 117 mirror::Object* CreateAnnotationMember(const ClassData& klass, 118 Handle<mirror::Class> annotation_class, 119 const uint8_t** annotation) 120 REQUIRES_SHARED(Locks::mutator_lock_); 121 122 bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) { 123 if (expected == DexFile::kDexVisibilityRuntime) { 124 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion(); 125 if (sdk_version > 0 && sdk_version <= 23) { 126 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild; 127 } 128 } 129 return actual == expected; 130 } 131 132 const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field) 133 REQUIRES_SHARED(Locks::mutator_lock_) { 134 const DexFile* dex_file = field->GetDexFile(); 135 ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); 136 const DexFile::AnnotationsDirectoryItem* annotations_dir = 137 dex_file->GetAnnotationsDirectory(*klass->GetClassDef()); 138 if (annotations_dir == nullptr) { 139 return nullptr; 140 } 141 const DexFile::FieldAnnotationsItem* field_annotations = 142 dex_file->GetFieldAnnotations(annotations_dir); 143 if (field_annotations == nullptr) { 144 return nullptr; 145 } 146 uint32_t field_index = field->GetDexFieldIndex(); 147 uint32_t field_count = annotations_dir->fields_size_; 148 for (uint32_t i = 0; i < field_count; ++i) { 149 if (field_annotations[i].field_idx_ == field_index) { 150 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]); 151 } 152 } 153 return nullptr; 154 } 155 156 const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file, 157 const DexFile::AnnotationSetItem* annotation_set, 158 const char* descriptor, 159 uint32_t visibility) 160 REQUIRES_SHARED(Locks::mutator_lock_) { 161 const DexFile::AnnotationItem* result = nullptr; 162 for (uint32_t i = 0; i < annotation_set->size_; ++i) { 163 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); 164 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { 165 continue; 166 } 167 const uint8_t* annotation = annotation_item->annotation_; 168 uint32_t type_index = DecodeUnsignedLeb128(&annotation); 169 170 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) { 171 result = annotation_item; 172 break; 173 } 174 } 175 return result; 176 } 177 178 bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr) 179 REQUIRES_SHARED(Locks::mutator_lock_) { 180 const uint8_t* annotation = *annotation_ptr; 181 uint8_t header_byte = *(annotation++); 182 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; 183 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; 184 int32_t width = value_arg + 1; 185 186 switch (value_type) { 187 case DexFile::kDexAnnotationByte: 188 case DexFile::kDexAnnotationShort: 189 case DexFile::kDexAnnotationChar: 190 case DexFile::kDexAnnotationInt: 191 case DexFile::kDexAnnotationLong: 192 case DexFile::kDexAnnotationFloat: 193 case DexFile::kDexAnnotationDouble: 194 case DexFile::kDexAnnotationString: 195 case DexFile::kDexAnnotationType: 196 case DexFile::kDexAnnotationMethod: 197 case DexFile::kDexAnnotationField: 198 case DexFile::kDexAnnotationEnum: 199 break; 200 case DexFile::kDexAnnotationArray: 201 { 202 uint32_t size = DecodeUnsignedLeb128(&annotation); 203 while (size--) { 204 if (!SkipAnnotationValue(dex_file, &annotation)) { 205 return false; 206 } 207 } 208 width = 0; 209 break; 210 } 211 case DexFile::kDexAnnotationAnnotation: 212 { 213 DecodeUnsignedLeb128(&annotation); // unused type_index 214 uint32_t size = DecodeUnsignedLeb128(&annotation); 215 while (size--) { 216 DecodeUnsignedLeb128(&annotation); // unused element_name_index 217 if (!SkipAnnotationValue(dex_file, &annotation)) { 218 return false; 219 } 220 } 221 width = 0; 222 break; 223 } 224 case DexFile::kDexAnnotationBoolean: 225 case DexFile::kDexAnnotationNull: 226 width = 0; 227 break; 228 default: 229 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type); 230 return false; 231 } 232 233 annotation += width; 234 *annotation_ptr = annotation; 235 return true; 236 } 237 238 const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file, 239 const uint8_t* annotation, 240 const char* name) 241 REQUIRES_SHARED(Locks::mutator_lock_) { 242 DecodeUnsignedLeb128(&annotation); // unused type_index 243 uint32_t size = DecodeUnsignedLeb128(&annotation); 244 245 while (size != 0) { 246 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation); 247 const char* element_name = 248 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index))); 249 if (strcmp(name, element_name) == 0) { 250 return annotation; 251 } 252 SkipAnnotationValue(dex_file, &annotation); 253 size--; 254 } 255 return nullptr; 256 } 257 258 const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method) 259 REQUIRES_SHARED(Locks::mutator_lock_) { 260 const DexFile* dex_file = method->GetDexFile(); 261 const DexFile::AnnotationsDirectoryItem* annotations_dir = 262 dex_file->GetAnnotationsDirectory(method->GetClassDef()); 263 if (annotations_dir == nullptr) { 264 return nullptr; 265 } 266 const DexFile::MethodAnnotationsItem* method_annotations = 267 dex_file->GetMethodAnnotations(annotations_dir); 268 if (method_annotations == nullptr) { 269 return nullptr; 270 } 271 uint32_t method_index = method->GetDexMethodIndex(); 272 uint32_t method_count = annotations_dir->methods_size_; 273 for (uint32_t i = 0; i < method_count; ++i) { 274 if (method_annotations[i].method_idx_ == method_index) { 275 return dex_file->GetMethodAnnotationSetItem(method_annotations[i]); 276 } 277 } 278 return nullptr; 279 } 280 281 const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method) 282 REQUIRES_SHARED(Locks::mutator_lock_) { 283 const DexFile* dex_file = method->GetDexFile(); 284 const DexFile::AnnotationsDirectoryItem* annotations_dir = 285 dex_file->GetAnnotationsDirectory(method->GetClassDef()); 286 if (annotations_dir == nullptr) { 287 return nullptr; 288 } 289 const DexFile::ParameterAnnotationsItem* parameter_annotations = 290 dex_file->GetParameterAnnotations(annotations_dir); 291 if (parameter_annotations == nullptr) { 292 return nullptr; 293 } 294 uint32_t method_index = method->GetDexMethodIndex(); 295 uint32_t parameter_count = annotations_dir->parameters_size_; 296 for (uint32_t i = 0; i < parameter_count; ++i) { 297 if (parameter_annotations[i].method_idx_ == method_index) { 298 return ¶meter_annotations[i]; 299 } 300 } 301 return nullptr; 302 } 303 304 const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass) 305 REQUIRES_SHARED(Locks::mutator_lock_) { 306 const DexFile& dex_file = klass.GetDexFile(); 307 const DexFile::AnnotationsDirectoryItem* annotations_dir = 308 dex_file.GetAnnotationsDirectory(*klass.GetClassDef()); 309 if (annotations_dir == nullptr) { 310 return nullptr; 311 } 312 return dex_file.GetClassAnnotationSet(annotations_dir); 313 } 314 315 mirror::Object* ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation) 316 REQUIRES_SHARED(Locks::mutator_lock_) { 317 uint32_t type_index = DecodeUnsignedLeb128(annotation); 318 uint32_t size = DecodeUnsignedLeb128(annotation); 319 320 Thread* self = Thread::Current(); 321 ScopedObjectAccessUnchecked soa(self); 322 StackHandleScope<4> hs(self); 323 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 324 Handle<mirror::Class> annotation_class(hs.NewHandle( 325 class_linker->ResolveType(klass.GetDexFile(), 326 dex::TypeIndex(type_index), 327 hs.NewHandle(klass.GetDexCache()), 328 hs.NewHandle(klass.GetClassLoader())))); 329 if (annotation_class == nullptr) { 330 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass() 331 << " annotation class " << type_index; 332 DCHECK(Thread::Current()->IsExceptionPending()); 333 Thread::Current()->ClearException(); 334 return nullptr; 335 } 336 337 ObjPtr<mirror::Class> annotation_member_class = 338 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr(); 339 mirror::Class* annotation_member_array_class = 340 class_linker->FindArrayClass(self, &annotation_member_class); 341 if (annotation_member_array_class == nullptr) { 342 return nullptr; 343 } 344 mirror::ObjectArray<mirror::Object>* element_array = nullptr; 345 if (size > 0) { 346 element_array = 347 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size); 348 if (element_array == nullptr) { 349 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)"; 350 return nullptr; 351 } 352 } 353 354 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array)); 355 for (uint32_t i = 0; i < size; ++i) { 356 mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation); 357 if (new_member == nullptr) { 358 return nullptr; 359 } 360 h_element_array->SetWithoutChecks<false>(i, new_member); 361 } 362 363 JValue result; 364 ArtMethod* create_annotation_method = 365 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation); 366 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())), 367 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) }; 368 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL"); 369 if (self->IsExceptionPending()) { 370 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation"; 371 return nullptr; 372 } 373 374 return result.GetL(); 375 } 376 377 template <bool kTransactionActive> 378 bool ProcessAnnotationValue(const ClassData& klass, 379 const uint8_t** annotation_ptr, 380 DexFile::AnnotationValue* annotation_value, 381 Handle<mirror::Class> array_class, 382 DexFile::AnnotationResultStyle result_style) 383 REQUIRES_SHARED(Locks::mutator_lock_) { 384 const DexFile& dex_file = klass.GetDexFile(); 385 Thread* self = Thread::Current(); 386 ObjPtr<mirror::Object> element_object = nullptr; 387 bool set_object = false; 388 Primitive::Type primitive_type = Primitive::kPrimVoid; 389 const uint8_t* annotation = *annotation_ptr; 390 uint8_t header_byte = *(annotation++); 391 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; 392 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; 393 int32_t width = value_arg + 1; 394 annotation_value->type_ = value_type; 395 396 switch (value_type) { 397 case DexFile::kDexAnnotationByte: 398 annotation_value->value_.SetB( 399 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg))); 400 primitive_type = Primitive::kPrimByte; 401 break; 402 case DexFile::kDexAnnotationShort: 403 annotation_value->value_.SetS( 404 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg))); 405 primitive_type = Primitive::kPrimShort; 406 break; 407 case DexFile::kDexAnnotationChar: 408 annotation_value->value_.SetC( 409 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false))); 410 primitive_type = Primitive::kPrimChar; 411 break; 412 case DexFile::kDexAnnotationInt: 413 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg)); 414 primitive_type = Primitive::kPrimInt; 415 break; 416 case DexFile::kDexAnnotationLong: 417 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg)); 418 primitive_type = Primitive::kPrimLong; 419 break; 420 case DexFile::kDexAnnotationFloat: 421 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true)); 422 primitive_type = Primitive::kPrimFloat; 423 break; 424 case DexFile::kDexAnnotationDouble: 425 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true)); 426 primitive_type = Primitive::kPrimDouble; 427 break; 428 case DexFile::kDexAnnotationBoolean: 429 annotation_value->value_.SetZ(value_arg != 0); 430 primitive_type = Primitive::kPrimBoolean; 431 width = 0; 432 break; 433 case DexFile::kDexAnnotationString: { 434 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); 435 if (result_style == DexFile::kAllRaw) { 436 annotation_value->value_.SetI(index); 437 } else { 438 StackHandleScope<1> hs(self); 439 element_object = Runtime::Current()->GetClassLinker()->ResolveString( 440 klass.GetDexFile(), dex::StringIndex(index), hs.NewHandle(klass.GetDexCache())); 441 set_object = true; 442 if (element_object == nullptr) { 443 return false; 444 } 445 } 446 break; 447 } 448 case DexFile::kDexAnnotationType: { 449 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); 450 if (result_style == DexFile::kAllRaw) { 451 annotation_value->value_.SetI(index); 452 } else { 453 dex::TypeIndex type_index(index); 454 StackHandleScope<2> hs(self); 455 element_object = Runtime::Current()->GetClassLinker()->ResolveType( 456 klass.GetDexFile(), 457 type_index, 458 hs.NewHandle(klass.GetDexCache()), 459 hs.NewHandle(klass.GetClassLoader())); 460 set_object = true; 461 if (element_object == nullptr) { 462 CHECK(self->IsExceptionPending()); 463 if (result_style == DexFile::kAllObjects) { 464 const char* msg = dex_file.StringByTypeIdx(type_index); 465 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg); 466 element_object = self->GetException(); 467 self->ClearException(); 468 } else { 469 return false; 470 } 471 } 472 } 473 break; 474 } 475 case DexFile::kDexAnnotationMethod: { 476 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); 477 if (result_style == DexFile::kAllRaw) { 478 annotation_value->value_.SetI(index); 479 } else { 480 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 481 StackHandleScope<2> hs(self); 482 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( 483 klass.GetDexFile(), 484 index, 485 hs.NewHandle(klass.GetDexCache()), 486 hs.NewHandle(klass.GetClassLoader())); 487 if (method == nullptr) { 488 return false; 489 } 490 PointerSize pointer_size = class_linker->GetImagePointerSize(); 491 set_object = true; 492 if (method->IsConstructor()) { 493 if (pointer_size == PointerSize::k64) { 494 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64, 495 kTransactionActive>(self, method); 496 } else { 497 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32, 498 kTransactionActive>(self, method); 499 } 500 } else { 501 if (pointer_size == PointerSize::k64) { 502 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64, 503 kTransactionActive>(self, method); 504 } else { 505 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32, 506 kTransactionActive>(self, method); 507 } 508 } 509 if (element_object == nullptr) { 510 return false; 511 } 512 } 513 break; 514 } 515 case DexFile::kDexAnnotationField: { 516 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); 517 if (result_style == DexFile::kAllRaw) { 518 annotation_value->value_.SetI(index); 519 } else { 520 StackHandleScope<2> hs(self); 521 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS( 522 klass.GetDexFile(), 523 index, 524 hs.NewHandle(klass.GetDexCache()), 525 hs.NewHandle(klass.GetClassLoader())); 526 if (field == nullptr) { 527 return false; 528 } 529 set_object = true; 530 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); 531 if (pointer_size == PointerSize::k64) { 532 element_object = mirror::Field::CreateFromArtField<PointerSize::k64, 533 kTransactionActive>(self, field, true); 534 } else { 535 element_object = mirror::Field::CreateFromArtField<PointerSize::k32, 536 kTransactionActive>(self, field, true); 537 } 538 if (element_object == nullptr) { 539 return false; 540 } 541 } 542 break; 543 } 544 case DexFile::kDexAnnotationEnum: { 545 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); 546 if (result_style == DexFile::kAllRaw) { 547 annotation_value->value_.SetI(index); 548 } else { 549 StackHandleScope<3> hs(self); 550 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField( 551 klass.GetDexFile(), 552 index, 553 hs.NewHandle(klass.GetDexCache()), 554 hs.NewHandle(klass.GetClassLoader()), 555 true); 556 if (enum_field == nullptr) { 557 return false; 558 } else { 559 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass())); 560 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true); 561 element_object = enum_field->GetObject(field_class.Get()); 562 set_object = true; 563 } 564 } 565 break; 566 } 567 case DexFile::kDexAnnotationArray: 568 if (result_style == DexFile::kAllRaw || array_class == nullptr) { 569 return false; 570 } else { 571 ScopedObjectAccessUnchecked soa(self); 572 StackHandleScope<2> hs(self); 573 uint32_t size = DecodeUnsignedLeb128(&annotation); 574 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType())); 575 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>( 576 self, array_class.Get(), size, array_class->GetComponentSizeShift(), 577 Runtime::Current()->GetHeap()->GetCurrentAllocator()))); 578 if (new_array == nullptr) { 579 LOG(ERROR) << "Annotation element array allocation failed with size " << size; 580 return false; 581 } 582 DexFile::AnnotationValue new_annotation_value; 583 for (uint32_t i = 0; i < size; ++i) { 584 if (!ProcessAnnotationValue<kTransactionActive>(klass, 585 &annotation, 586 &new_annotation_value, 587 component_type, 588 DexFile::kPrimitivesOrObjects)) { 589 return false; 590 } 591 if (!component_type->IsPrimitive()) { 592 mirror::Object* obj = new_annotation_value.value_.GetL(); 593 new_array->AsObjectArray<mirror::Object>()-> 594 SetWithoutChecks<kTransactionActive>(i, obj); 595 } else { 596 switch (new_annotation_value.type_) { 597 case DexFile::kDexAnnotationByte: 598 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>( 599 i, new_annotation_value.value_.GetB()); 600 break; 601 case DexFile::kDexAnnotationShort: 602 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>( 603 i, new_annotation_value.value_.GetS()); 604 break; 605 case DexFile::kDexAnnotationChar: 606 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>( 607 i, new_annotation_value.value_.GetC()); 608 break; 609 case DexFile::kDexAnnotationInt: 610 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>( 611 i, new_annotation_value.value_.GetI()); 612 break; 613 case DexFile::kDexAnnotationLong: 614 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>( 615 i, new_annotation_value.value_.GetJ()); 616 break; 617 case DexFile::kDexAnnotationFloat: 618 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>( 619 i, new_annotation_value.value_.GetF()); 620 break; 621 case DexFile::kDexAnnotationDouble: 622 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>( 623 i, new_annotation_value.value_.GetD()); 624 break; 625 case DexFile::kDexAnnotationBoolean: 626 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>( 627 i, new_annotation_value.value_.GetZ()); 628 break; 629 default: 630 LOG(FATAL) << "Found invalid annotation value type while building annotation array"; 631 return false; 632 } 633 } 634 } 635 element_object = new_array.Get(); 636 set_object = true; 637 width = 0; 638 } 639 break; 640 case DexFile::kDexAnnotationAnnotation: 641 if (result_style == DexFile::kAllRaw) { 642 return false; 643 } 644 element_object = ProcessEncodedAnnotation(klass, &annotation); 645 if (element_object == nullptr) { 646 return false; 647 } 648 set_object = true; 649 width = 0; 650 break; 651 case DexFile::kDexAnnotationNull: 652 if (result_style == DexFile::kAllRaw) { 653 annotation_value->value_.SetI(0); 654 } else { 655 CHECK(element_object == nullptr); 656 set_object = true; 657 } 658 width = 0; 659 break; 660 default: 661 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type); 662 return false; 663 } 664 665 annotation += width; 666 *annotation_ptr = annotation; 667 668 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) { 669 element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr(); 670 set_object = true; 671 } 672 673 if (set_object) { 674 annotation_value->value_.SetL(element_object.Ptr()); 675 } 676 677 return true; 678 } 679 680 mirror::Object* CreateAnnotationMember(const ClassData& klass, 681 Handle<mirror::Class> annotation_class, 682 const uint8_t** annotation) { 683 const DexFile& dex_file = klass.GetDexFile(); 684 Thread* self = Thread::Current(); 685 ScopedObjectAccessUnchecked soa(self); 686 StackHandleScope<5> hs(self); 687 uint32_t element_name_index = DecodeUnsignedLeb128(annotation); 688 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index)); 689 Handle<mirror::String> string_name( 690 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name))); 691 692 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); 693 ArtMethod* annotation_method = 694 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size); 695 if (annotation_method == nullptr) { 696 return nullptr; 697 } 698 Handle<mirror::Class> method_return(hs.NewHandle( 699 annotation_method->GetReturnType(true /* resolve */))); 700 701 DexFile::AnnotationValue annotation_value; 702 if (!ProcessAnnotationValue<false>(klass, 703 annotation, 704 &annotation_value, 705 method_return, 706 DexFile::kAllObjects)) { 707 return nullptr; 708 } 709 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL())); 710 711 ObjPtr<mirror::Class> annotation_member_class = 712 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember); 713 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self))); 714 mirror::Method* method_obj_ptr; 715 DCHECK(!Runtime::Current()->IsActiveTransaction()); 716 if (pointer_size == PointerSize::k64) { 717 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>( 718 self, annotation_method); 719 } else { 720 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>( 721 self, annotation_method); 722 } 723 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr)); 724 725 if (new_member == nullptr || string_name == nullptr || 726 method_object == nullptr || method_return == nullptr) { 727 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p", 728 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get()); 729 return nullptr; 730 } 731 732 JValue result; 733 ArtMethod* annotation_member_init = 734 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init); 735 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())), 736 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())), 737 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())), 738 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())), 739 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get())) 740 }; 741 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL"); 742 if (self->IsExceptionPending()) { 743 LOG(INFO) << "Exception in AnnotationMember.<init>"; 744 return nullptr; 745 } 746 747 return new_member.Get(); 748 } 749 750 const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet( 751 const ClassData& klass, 752 const DexFile::AnnotationSetItem* annotation_set, 753 uint32_t visibility, 754 Handle<mirror::Class> annotation_class, 755 bool lookup_in_resolved_boot_classes = false) 756 REQUIRES_SHARED(Locks::mutator_lock_) { 757 const DexFile& dex_file = klass.GetDexFile(); 758 for (uint32_t i = 0; i < annotation_set->size_; ++i) { 759 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); 760 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { 761 continue; 762 } 763 const uint8_t* annotation = annotation_item->annotation_; 764 uint32_t type_index = DecodeUnsignedLeb128(&annotation); 765 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 766 Thread* self = Thread::Current(); 767 mirror::Class* resolved_class; 768 if (lookup_in_resolved_boot_classes) { 769 // Note: We cannot use ClassLinker::LookupResolvedType() because the current DexCache 770 // may not be registered with the boot class path ClassLoader and we must not pollute 771 // the DexCache with classes that are not in the associated ClassLoader's ClassTable. 772 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index)); 773 ObjPtr<mirror::Class> looked_up_class = 774 class_linker->LookupClass(self, descriptor, /* class_loader */ nullptr); 775 resolved_class = looked_up_class.Ptr(); 776 if (resolved_class == nullptr) { 777 // If `resolved_class` is null, this is fine: just ignore that 778 // annotation item. We expect this to happen, as we do not 779 // attempt to resolve the annotation's class in this code path. 780 continue; 781 } 782 } else { 783 StackHandleScope<2> hs(self); 784 resolved_class = class_linker->ResolveType( 785 klass.GetDexFile(), 786 dex::TypeIndex(type_index), 787 hs.NewHandle(klass.GetDexCache()), 788 hs.NewHandle(klass.GetClassLoader())); 789 if (resolved_class == nullptr) { 790 std::string temp; 791 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d", 792 klass.GetRealClass()->GetDescriptor(&temp), type_index); 793 CHECK(self->IsExceptionPending()); 794 self->ClearException(); 795 continue; 796 } 797 } 798 if (resolved_class == annotation_class.Get()) { 799 return annotation_item; 800 } 801 } 802 803 return nullptr; 804 } 805 806 mirror::Object* GetAnnotationObjectFromAnnotationSet( 807 const ClassData& klass, 808 const DexFile::AnnotationSetItem* annotation_set, 809 uint32_t visibility, 810 Handle<mirror::Class> annotation_class) 811 REQUIRES_SHARED(Locks::mutator_lock_) { 812 const DexFile::AnnotationItem* annotation_item = 813 GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class); 814 if (annotation_item == nullptr) { 815 return nullptr; 816 } 817 const uint8_t* annotation = annotation_item->annotation_; 818 return ProcessEncodedAnnotation(klass, &annotation); 819 } 820 821 mirror::Object* GetAnnotationValue(const ClassData& klass, 822 const DexFile::AnnotationItem* annotation_item, 823 const char* annotation_name, 824 Handle<mirror::Class> array_class, 825 uint32_t expected_type) 826 REQUIRES_SHARED(Locks::mutator_lock_) { 827 const DexFile& dex_file = klass.GetDexFile(); 828 const uint8_t* annotation = 829 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name); 830 if (annotation == nullptr) { 831 return nullptr; 832 } 833 DexFile::AnnotationValue annotation_value; 834 bool result = Runtime::Current()->IsActiveTransaction() 835 ? ProcessAnnotationValue<true>(klass, 836 &annotation, 837 &annotation_value, 838 array_class, 839 DexFile::kAllObjects) 840 : ProcessAnnotationValue<false>(klass, 841 &annotation, 842 &annotation_value, 843 array_class, 844 DexFile::kAllObjects); 845 if (!result) { 846 return nullptr; 847 } 848 if (annotation_value.type_ != expected_type) { 849 return nullptr; 850 } 851 return annotation_value.value_.GetL(); 852 } 853 854 mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass, 855 const DexFile::AnnotationSetItem* annotation_set) 856 REQUIRES_SHARED(Locks::mutator_lock_) { 857 const DexFile& dex_file = klass.GetDexFile(); 858 StackHandleScope<1> hs(Thread::Current()); 859 const DexFile::AnnotationItem* annotation_item = 860 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;", 861 DexFile::kDexVisibilitySystem); 862 if (annotation_item == nullptr) { 863 return nullptr; 864 } 865 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString(); 866 Handle<mirror::Class> string_array_class(hs.NewHandle( 867 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class))); 868 if (string_array_class == nullptr) { 869 return nullptr; 870 } 871 mirror::Object* obj = 872 GetAnnotationValue(klass, annotation_item, "value", string_array_class, 873 DexFile::kDexAnnotationArray); 874 if (obj == nullptr) { 875 return nullptr; 876 } 877 return obj->AsObjectArray<mirror::String>(); 878 } 879 880 mirror::ObjectArray<mirror::Class>* GetThrowsValue(const ClassData& klass, 881 const DexFile::AnnotationSetItem* annotation_set) 882 REQUIRES_SHARED(Locks::mutator_lock_) { 883 const DexFile& dex_file = klass.GetDexFile(); 884 StackHandleScope<1> hs(Thread::Current()); 885 const DexFile::AnnotationItem* annotation_item = 886 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;", 887 DexFile::kDexVisibilitySystem); 888 if (annotation_item == nullptr) { 889 return nullptr; 890 } 891 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass(); 892 Handle<mirror::Class> class_array_class(hs.NewHandle( 893 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class))); 894 if (class_array_class == nullptr) { 895 return nullptr; 896 } 897 mirror::Object* obj = 898 GetAnnotationValue(klass, annotation_item, "value", class_array_class, 899 DexFile::kDexAnnotationArray); 900 if (obj == nullptr) { 901 return nullptr; 902 } 903 return obj->AsObjectArray<mirror::Class>(); 904 } 905 906 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet( 907 const ClassData& klass, 908 const DexFile::AnnotationSetItem* annotation_set, 909 uint32_t visibility) 910 REQUIRES_SHARED(Locks::mutator_lock_) { 911 const DexFile& dex_file = klass.GetDexFile(); 912 Thread* self = Thread::Current(); 913 ScopedObjectAccessUnchecked soa(self); 914 StackHandleScope<2> hs(self); 915 Handle<mirror::Class> annotation_array_class(hs.NewHandle( 916 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array))); 917 if (annotation_set == nullptr) { 918 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0); 919 } 920 921 uint32_t size = annotation_set->size_; 922 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle( 923 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size))); 924 if (result == nullptr) { 925 return nullptr; 926 } 927 928 uint32_t dest_index = 0; 929 for (uint32_t i = 0; i < size; ++i) { 930 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); 931 // Note that we do not use IsVisibilityCompatible here because older code 932 // was correct for this case. 933 if (annotation_item->visibility_ != visibility) { 934 continue; 935 } 936 const uint8_t* annotation = annotation_item->annotation_; 937 mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation); 938 if (annotation_obj != nullptr) { 939 result->SetWithoutChecks<false>(dest_index, annotation_obj); 940 ++dest_index; 941 } else if (self->IsExceptionPending()) { 942 return nullptr; 943 } 944 } 945 946 if (dest_index == size) { 947 return result.Get(); 948 } 949 950 mirror::ObjectArray<mirror::Object>* trimmed_result = 951 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index); 952 if (trimmed_result == nullptr) { 953 return nullptr; 954 } 955 956 for (uint32_t i = 0; i < dest_index; ++i) { 957 mirror::Object* obj = result->GetWithoutChecks(i); 958 trimmed_result->SetWithoutChecks<false>(i, obj); 959 } 960 961 return trimmed_result; 962 } 963 964 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList( 965 const ClassData& klass, 966 const DexFile::AnnotationSetRefList* set_ref_list, 967 uint32_t size) 968 REQUIRES_SHARED(Locks::mutator_lock_) { 969 const DexFile& dex_file = klass.GetDexFile(); 970 Thread* self = Thread::Current(); 971 ScopedObjectAccessUnchecked soa(self); 972 StackHandleScope<1> hs(self); 973 ObjPtr<mirror::Class> annotation_array_class = 974 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array); 975 mirror::Class* annotation_array_array_class = 976 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class); 977 if (annotation_array_array_class == nullptr) { 978 return nullptr; 979 } 980 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle( 981 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size))); 982 if (annotation_array_array == nullptr) { 983 LOG(ERROR) << "Annotation set ref array allocation failed"; 984 return nullptr; 985 } 986 for (uint32_t index = 0; index < size; ++index) { 987 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index]; 988 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item); 989 mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item, 990 DexFile::kDexVisibilityRuntime); 991 if (annotation_set == nullptr) { 992 return nullptr; 993 } 994 annotation_array_array->SetWithoutChecks<false>(index, annotation_set); 995 } 996 return annotation_array_array.Get(); 997 } 998 } // namespace 999 1000 namespace annotations { 1001 1002 mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) { 1003 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); 1004 if (annotation_set == nullptr) { 1005 return nullptr; 1006 } 1007 StackHandleScope<1> hs(Thread::Current()); 1008 const ClassData field_class(hs, field); 1009 return GetAnnotationObjectFromAnnotationSet(field_class, 1010 annotation_set, 1011 DexFile::kDexVisibilityRuntime, 1012 annotation_class); 1013 } 1014 1015 mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) { 1016 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); 1017 StackHandleScope<1> hs(Thread::Current()); 1018 const ClassData field_class(hs, field); 1019 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime); 1020 } 1021 1022 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) { 1023 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); 1024 if (annotation_set == nullptr) { 1025 return nullptr; 1026 } 1027 StackHandleScope<1> hs(Thread::Current()); 1028 const ClassData field_class(hs, field); 1029 return GetSignatureValue(field_class, annotation_set); 1030 } 1031 1032 bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) { 1033 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); 1034 if (annotation_set == nullptr) { 1035 return false; 1036 } 1037 StackHandleScope<1> hs(Thread::Current()); 1038 const ClassData field_class(hs, field); 1039 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( 1040 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); 1041 return annotation_item != nullptr; 1042 } 1043 1044 mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) { 1045 const ClassData klass(method); 1046 const DexFile* dex_file = &klass.GetDexFile(); 1047 const DexFile::AnnotationsDirectoryItem* annotations_dir = 1048 dex_file->GetAnnotationsDirectory(*klass.GetClassDef()); 1049 if (annotations_dir == nullptr) { 1050 return nullptr; 1051 } 1052 const DexFile::AnnotationSetItem* annotation_set = 1053 dex_file->GetClassAnnotationSet(annotations_dir); 1054 if (annotation_set == nullptr) { 1055 return nullptr; 1056 } 1057 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set, 1058 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem); 1059 if (annotation_item == nullptr) { 1060 return nullptr; 1061 } 1062 const uint8_t* annotation = 1063 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value"); 1064 if (annotation == nullptr) { 1065 return nullptr; 1066 } 1067 uint8_t header_byte = *(annotation++); 1068 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) { 1069 return nullptr; 1070 } 1071 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName()); 1072 if (annotation == nullptr) { 1073 return nullptr; 1074 } 1075 DexFile::AnnotationValue annotation_value; 1076 StackHandleScope<1> hs(Thread::Current()); 1077 Handle<mirror::Class> return_type(hs.NewHandle(method->GetReturnType(true /* resolve */))); 1078 if (!ProcessAnnotationValue<false>(klass, 1079 &annotation, 1080 &annotation_value, 1081 return_type, 1082 DexFile::kAllObjects)) { 1083 return nullptr; 1084 } 1085 return annotation_value.value_.GetL(); 1086 } 1087 1088 mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) { 1089 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); 1090 if (annotation_set == nullptr) { 1091 return nullptr; 1092 } 1093 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set, 1094 DexFile::kDexVisibilityRuntime, annotation_class); 1095 } 1096 1097 mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) { 1098 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); 1099 return ProcessAnnotationSet(ClassData(method), 1100 annotation_set, 1101 DexFile::kDexVisibilityRuntime); 1102 } 1103 1104 mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) { 1105 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); 1106 if (annotation_set == nullptr) { 1107 return nullptr; 1108 } 1109 return GetThrowsValue(ClassData(method), annotation_set); 1110 } 1111 1112 mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) { 1113 const DexFile* dex_file = method->GetDexFile(); 1114 const DexFile::ParameterAnnotationsItem* parameter_annotations = 1115 FindAnnotationsItemForMethod(method); 1116 if (parameter_annotations == nullptr) { 1117 return nullptr; 1118 } 1119 const DexFile::AnnotationSetRefList* set_ref_list = 1120 dex_file->GetParameterAnnotationSetRefList(parameter_annotations); 1121 if (set_ref_list == nullptr) { 1122 return nullptr; 1123 } 1124 uint32_t size = set_ref_list->size_; 1125 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size); 1126 } 1127 1128 mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method, 1129 uint32_t parameter_idx, 1130 Handle<mirror::Class> annotation_class) { 1131 const DexFile* dex_file = method->GetDexFile(); 1132 const DexFile::ParameterAnnotationsItem* parameter_annotations = 1133 FindAnnotationsItemForMethod(method); 1134 if (parameter_annotations == nullptr) { 1135 return nullptr; 1136 } 1137 const DexFile::AnnotationSetRefList* set_ref_list = 1138 dex_file->GetParameterAnnotationSetRefList(parameter_annotations); 1139 if (set_ref_list == nullptr) { 1140 return nullptr; 1141 } 1142 if (parameter_idx >= set_ref_list->size_) { 1143 return nullptr; 1144 } 1145 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx]; 1146 const DexFile::AnnotationSetItem* annotation_set = 1147 dex_file->GetSetRefItemItem(annotation_set_ref); 1148 1149 return GetAnnotationObjectFromAnnotationSet(ClassData(method), 1150 annotation_set, 1151 DexFile::kDexVisibilityRuntime, 1152 annotation_class); 1153 } 1154 1155 bool GetParametersMetadataForMethod(ArtMethod* method, 1156 MutableHandle<mirror::ObjectArray<mirror::String>>* names, 1157 MutableHandle<mirror::IntArray>* access_flags) { 1158 const DexFile::AnnotationSetItem* annotation_set = 1159 FindAnnotationSetForMethod(method); 1160 if (annotation_set == nullptr) { 1161 return false; 1162 } 1163 1164 const DexFile* dex_file = method->GetDexFile(); 1165 const DexFile::AnnotationItem* annotation_item = 1166 SearchAnnotationSet(*dex_file, 1167 annotation_set, 1168 "Ldalvik/annotation/MethodParameters;", 1169 DexFile::kDexVisibilitySystem); 1170 if (annotation_item == nullptr) { 1171 return false; 1172 } 1173 1174 StackHandleScope<4> hs(Thread::Current()); 1175 1176 // Extract the parameters' names String[]. 1177 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString(); 1178 Handle<mirror::Class> string_array_class(hs.NewHandle( 1179 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class))); 1180 if (UNLIKELY(string_array_class == nullptr)) { 1181 return false; 1182 } 1183 1184 ClassData data(method); 1185 Handle<mirror::Object> names_obj = 1186 hs.NewHandle(GetAnnotationValue(data, 1187 annotation_item, 1188 "names", 1189 string_array_class, 1190 DexFile::kDexAnnotationArray)); 1191 if (names_obj == nullptr) { 1192 return false; 1193 } 1194 1195 // Extract the parameters' access flags int[]. 1196 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass())); 1197 if (UNLIKELY(int_array_class == nullptr)) { 1198 return false; 1199 } 1200 Handle<mirror::Object> access_flags_obj = 1201 hs.NewHandle(GetAnnotationValue(data, 1202 annotation_item, 1203 "accessFlags", 1204 int_array_class, 1205 DexFile::kDexAnnotationArray)); 1206 if (access_flags_obj == nullptr) { 1207 return false; 1208 } 1209 1210 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>()); 1211 access_flags->Assign(access_flags_obj.Get()->AsIntArray()); 1212 return true; 1213 } 1214 1215 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) { 1216 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); 1217 if (annotation_set == nullptr) { 1218 return nullptr; 1219 } 1220 return GetSignatureValue(ClassData(method), annotation_set); 1221 } 1222 1223 bool IsMethodAnnotationPresent(ArtMethod* method, 1224 Handle<mirror::Class> annotation_class, 1225 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */, 1226 bool lookup_in_resolved_boot_classes /* = false */) { 1227 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); 1228 if (annotation_set == nullptr) { 1229 return false; 1230 } 1231 const DexFile::AnnotationItem* annotation_item = 1232 GetAnnotationItemFromAnnotationSet(ClassData(method), 1233 annotation_set, 1234 visibility, 1235 annotation_class, 1236 lookup_in_resolved_boot_classes); 1237 return annotation_item != nullptr; 1238 } 1239 1240 mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass, 1241 Handle<mirror::Class> annotation_class) { 1242 ClassData data(klass); 1243 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1244 if (annotation_set == nullptr) { 1245 return nullptr; 1246 } 1247 return GetAnnotationObjectFromAnnotationSet(data, 1248 annotation_set, 1249 DexFile::kDexVisibilityRuntime, 1250 annotation_class); 1251 } 1252 1253 mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) { 1254 ClassData data(klass); 1255 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1256 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime); 1257 } 1258 1259 mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) { 1260 ClassData data(klass); 1261 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1262 if (annotation_set == nullptr) { 1263 return nullptr; 1264 } 1265 const DexFile::AnnotationItem* annotation_item = 1266 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;", 1267 DexFile::kDexVisibilitySystem); 1268 if (annotation_item == nullptr) { 1269 return nullptr; 1270 } 1271 StackHandleScope<1> hs(Thread::Current()); 1272 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass(); 1273 Handle<mirror::Class> class_array_class(hs.NewHandle( 1274 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class))); 1275 if (class_array_class == nullptr) { 1276 return nullptr; 1277 } 1278 mirror::Object* obj = 1279 GetAnnotationValue(data, annotation_item, "value", class_array_class, 1280 DexFile::kDexAnnotationArray); 1281 if (obj == nullptr) { 1282 return nullptr; 1283 } 1284 return obj->AsObjectArray<mirror::Class>(); 1285 } 1286 1287 mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) { 1288 ClassData data(klass); 1289 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1290 if (annotation_set == nullptr) { 1291 return nullptr; 1292 } 1293 const DexFile::AnnotationItem* annotation_item = 1294 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;", 1295 DexFile::kDexVisibilitySystem); 1296 if (annotation_item == nullptr) { 1297 return nullptr; 1298 } 1299 mirror::Object* obj = GetAnnotationValue(data, annotation_item, "value", 1300 ScopedNullHandle<mirror::Class>(), 1301 DexFile::kDexAnnotationType); 1302 if (obj == nullptr) { 1303 return nullptr; 1304 } 1305 return obj->AsClass(); 1306 } 1307 1308 mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) { 1309 mirror::Class* declaring_class = GetDeclaringClass(klass); 1310 if (declaring_class != nullptr) { 1311 return declaring_class; 1312 } 1313 ClassData data(klass); 1314 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1315 if (annotation_set == nullptr) { 1316 return nullptr; 1317 } 1318 const DexFile::AnnotationItem* annotation_item = 1319 SearchAnnotationSet(data.GetDexFile(), 1320 annotation_set, 1321 "Ldalvik/annotation/EnclosingMethod;", 1322 DexFile::kDexVisibilitySystem); 1323 if (annotation_item == nullptr) { 1324 return nullptr; 1325 } 1326 const uint8_t* annotation = 1327 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value"); 1328 if (annotation == nullptr) { 1329 return nullptr; 1330 } 1331 DexFile::AnnotationValue annotation_value; 1332 if (!ProcessAnnotationValue<false>(data, 1333 &annotation, 1334 &annotation_value, 1335 ScopedNullHandle<mirror::Class>(), 1336 DexFile::kAllRaw)) { 1337 return nullptr; 1338 } 1339 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) { 1340 return nullptr; 1341 } 1342 StackHandleScope<2> hs(Thread::Current()); 1343 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType( 1344 data.GetDexFile(), 1345 annotation_value.value_.GetI(), 1346 hs.NewHandle(data.GetDexCache()), 1347 hs.NewHandle(data.GetClassLoader())); 1348 if (method == nullptr) { 1349 return nullptr; 1350 } 1351 return method->GetDeclaringClass(); 1352 } 1353 1354 mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) { 1355 ClassData data(klass); 1356 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1357 if (annotation_set == nullptr) { 1358 return nullptr; 1359 } 1360 const DexFile::AnnotationItem* annotation_item = 1361 SearchAnnotationSet(data.GetDexFile(), 1362 annotation_set, 1363 "Ldalvik/annotation/EnclosingMethod;", 1364 DexFile::kDexVisibilitySystem); 1365 if (annotation_item == nullptr) { 1366 return nullptr; 1367 } 1368 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(), 1369 DexFile::kDexAnnotationMethod); 1370 } 1371 1372 bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) { 1373 ClassData data(klass); 1374 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1375 if (annotation_set == nullptr) { 1376 return false; 1377 } 1378 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet( 1379 data.GetDexFile(), 1380 annotation_set, 1381 "Ldalvik/annotation/InnerClass;", 1382 DexFile::kDexVisibilitySystem); 1383 if (annotation_item == nullptr) { 1384 return false; 1385 } 1386 const uint8_t* annotation = 1387 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name"); 1388 if (annotation == nullptr) { 1389 return false; 1390 } 1391 DexFile::AnnotationValue annotation_value; 1392 if (!ProcessAnnotationValue<false>(data, 1393 &annotation, 1394 &annotation_value, 1395 ScopedNullHandle<mirror::Class>(), 1396 DexFile::kAllObjects)) { 1397 return false; 1398 } 1399 if (annotation_value.type_ != DexFile::kDexAnnotationNull && 1400 annotation_value.type_ != DexFile::kDexAnnotationString) { 1401 return false; 1402 } 1403 *name = down_cast<mirror::String*>(annotation_value.value_.GetL()); 1404 return true; 1405 } 1406 1407 bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) { 1408 ClassData data(klass); 1409 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1410 if (annotation_set == nullptr) { 1411 return false; 1412 } 1413 const DexFile::AnnotationItem* annotation_item = 1414 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;", 1415 DexFile::kDexVisibilitySystem); 1416 if (annotation_item == nullptr) { 1417 return false; 1418 } 1419 const uint8_t* annotation = 1420 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags"); 1421 if (annotation == nullptr) { 1422 return false; 1423 } 1424 DexFile::AnnotationValue annotation_value; 1425 if (!ProcessAnnotationValue<false>(data, 1426 &annotation, 1427 &annotation_value, 1428 ScopedNullHandle<mirror::Class>(), 1429 DexFile::kAllRaw)) { 1430 return false; 1431 } 1432 if (annotation_value.type_ != DexFile::kDexAnnotationInt) { 1433 return false; 1434 } 1435 *flags = annotation_value.value_.GetI(); 1436 return true; 1437 } 1438 1439 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) { 1440 ClassData data(klass); 1441 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1442 if (annotation_set == nullptr) { 1443 return nullptr; 1444 } 1445 return GetSignatureValue(data, annotation_set); 1446 } 1447 1448 const char* GetSourceDebugExtension(Handle<mirror::Class> klass) { 1449 // Before instantiating ClassData, check that klass has a DexCache 1450 // assigned. The ClassData constructor indirectly dereferences it 1451 // when calling klass->GetDexFile(). 1452 if (klass->GetDexCache() == nullptr) { 1453 DCHECK(klass->IsPrimitive() || klass->IsArrayClass()); 1454 return nullptr; 1455 } 1456 1457 ClassData data(klass); 1458 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1459 if (annotation_set == nullptr) { 1460 return nullptr; 1461 } 1462 1463 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet( 1464 data.GetDexFile(), 1465 annotation_set, 1466 "Ldalvik/annotation/SourceDebugExtension;", 1467 DexFile::kDexVisibilitySystem); 1468 if (annotation_item == nullptr) { 1469 return nullptr; 1470 } 1471 1472 const uint8_t* annotation = 1473 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value"); 1474 if (annotation == nullptr) { 1475 return nullptr; 1476 } 1477 DexFile::AnnotationValue annotation_value; 1478 if (!ProcessAnnotationValue<false>(data, 1479 &annotation, 1480 &annotation_value, 1481 ScopedNullHandle<mirror::Class>(), 1482 DexFile::kAllRaw)) { 1483 return nullptr; 1484 } 1485 if (annotation_value.type_ != DexFile::kDexAnnotationString) { 1486 return nullptr; 1487 } 1488 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI())); 1489 return data.GetDexFile().StringDataByIdx(index); 1490 } 1491 1492 bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) { 1493 ClassData data(klass); 1494 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); 1495 if (annotation_set == nullptr) { 1496 return false; 1497 } 1498 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( 1499 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); 1500 return annotation_item != nullptr; 1501 } 1502 1503 int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) { 1504 // For native method, lineno should be -2 to indicate it is native. Note that 1505 // "line number == -2" is how libcore tells from StackTraceElement. 1506 if (method->GetCodeItemOffset() == 0) { 1507 return -2; 1508 } 1509 1510 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 1511 DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation(); 1512 1513 // A method with no line number info should return -1 1514 DexFile::LineNumFromPcContext context(rel_pc, -1); 1515 dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context); 1516 return context.line_num_; 1517 } 1518 1519 template<bool kTransactionActive> 1520 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const { 1521 DCHECK(dex_cache_ != nullptr); 1522 DCHECK(class_loader_ != nullptr); 1523 switch (type_) { 1524 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); 1525 break; 1526 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break; 1527 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break; 1528 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break; 1529 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break; 1530 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break; 1531 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break; 1532 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break; 1533 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break; 1534 case kString: { 1535 mirror::String* resolved = linker_->ResolveString(dex_file_, 1536 dex::StringIndex(jval_.i), 1537 *dex_cache_); 1538 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); 1539 break; 1540 } 1541 case kType: { 1542 mirror::Class* resolved = linker_->ResolveType(dex_file_, 1543 dex::TypeIndex(jval_.i), 1544 *dex_cache_, 1545 *class_loader_); 1546 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); 1547 break; 1548 } 1549 default: UNIMPLEMENTED(FATAL) << ": type " << type_; 1550 } 1551 } 1552 template 1553 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const; 1554 template 1555 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const; 1556 1557 } // namespace annotations 1558 1559 } // namespace art 1560