David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 21 | #include "android-base/stringprintf.h" |
| 22 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
| 24 | #include "art_method-inl.h" |
| 25 | #include "class_linker-inl.h" |
| 26 | #include "dex_file-inl.h" |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 27 | #include "jni_internal.h" |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 28 | #include "jvalue-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 29 | #include "mirror/field.h" |
| 30 | #include "mirror/method.h" |
| 31 | #include "reflection.h" |
| 32 | #include "thread.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; |
| 37 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 38 | struct DexFile::AnnotationValue { |
| 39 | JValue value_; |
| 40 | uint8_t type_; |
| 41 | }; |
| 42 | |
| 43 | namespace { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 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, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 135 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 170 | if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 247 | const char* element_name = |
| 248 | dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 261 | const DexFile::AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 262 | dex_file->GetAnnotationsDirectory(method->GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 284 | const DexFile::AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 285 | dex_file->GetAnnotationsDirectory(method->GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 304 | const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 305 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 306 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 307 | const DexFile::AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 308 | dex_file.GetAnnotationsDirectory(*klass.GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 309 | if (annotations_dir == nullptr) { |
| 310 | return nullptr; |
| 311 | } |
| 312 | return dex_file.GetClassAnnotationSet(annotations_dir); |
| 313 | } |
| 314 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 315 | mirror::Object* ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 322 | StackHandleScope<4> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 323 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 324 | Handle<mirror::Class> annotation_class(hs.NewHandle( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 325 | class_linker->ResolveType(klass.GetDexFile(), |
| 326 | dex::TypeIndex(type_index), |
| 327 | hs.NewHandle(klass.GetDexCache()), |
| 328 | hs.NewHandle(klass.GetClassLoader())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 329 | if (annotation_class == nullptr) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 330 | LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass() |
| 331 | << " annotation class " << type_index; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 332 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 333 | Thread::Current()->ClearException(); |
| 334 | return nullptr; |
| 335 | } |
| 336 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 337 | ObjPtr<mirror::Class> annotation_member_class = |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 338 | soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 = |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 365 | jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 377 | template <bool kTransactionActive> |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 378 | bool ProcessAnnotationValue(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 384 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 385 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 386 | ObjPtr<mirror::Object> element_object = nullptr; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 439 | element_object = Runtime::Current()->GetClassLinker()->ResolveString( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 440 | klass.GetDexFile(), dex::StringIndex(index), hs.NewHandle(klass.GetDexCache())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 453 | dex::TypeIndex type_index(index); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 454 | StackHandleScope<2> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 455 | element_object = Runtime::Current()->GetClassLinker()->ResolveType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 456 | klass.GetDexFile(), |
| 457 | type_index, |
| 458 | hs.NewHandle(klass.GetDexCache()), |
| 459 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 460 | set_object = true; |
| 461 | if (element_object == nullptr) { |
| 462 | CHECK(self->IsExceptionPending()); |
| 463 | if (result_style == DexFile::kAllObjects) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 464 | const char* msg = dex_file.StringByTypeIdx(type_index); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 { |
Nicolas Geoffray | 65e0775 | 2017-03-15 06:56:35 +0000 | [diff] [blame] | 480 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 481 | StackHandleScope<2> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 482 | ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 483 | klass.GetDexFile(), |
| 484 | index, |
| 485 | hs.NewHandle(klass.GetDexCache()), |
| 486 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 487 | if (method == nullptr) { |
| 488 | return false; |
| 489 | } |
| 490 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
| 491 | set_object = true; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 492 | if (method->IsConstructor()) { |
| 493 | if (pointer_size == PointerSize::k64) { |
| 494 | element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 495 | kTransactionActive>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 496 | } else { |
| 497 | element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 498 | kTransactionActive>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 499 | } |
| 500 | } else { |
| 501 | if (pointer_size == PointerSize::k64) { |
| 502 | element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 503 | kTransactionActive>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 504 | } else { |
| 505 | element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 506 | kTransactionActive>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 521 | ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 522 | klass.GetDexFile(), |
| 523 | index, |
| 524 | hs.NewHandle(klass.GetDexCache()), |
| 525 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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) { |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 532 | element_object = mirror::Field::CreateFromArtField<PointerSize::k64, |
| 533 | kTransactionActive>(self, field, true); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 534 | } else { |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 535 | element_object = mirror::Field::CreateFromArtField<PointerSize::k32, |
| 536 | kTransactionActive>(self, field, true); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 550 | ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 551 | klass.GetDexFile(), |
| 552 | index, |
| 553 | hs.NewHandle(klass.GetDexCache()), |
| 554 | hs.NewHandle(klass.GetClassLoader()), |
| 555 | true); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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: |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 568 | if (result_style == DexFile::kAllRaw || array_class == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 578 | if (new_array == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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) { |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 584 | if (!ProcessAnnotationValue<kTransactionActive>(klass, |
| 585 | &annotation, |
| 586 | &new_annotation_value, |
| 587 | component_type, |
| 588 | DexFile::kPrimitivesOrObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 589 | return false; |
| 590 | } |
| 591 | if (!component_type->IsPrimitive()) { |
| 592 | mirror::Object* obj = new_annotation_value.value_.GetL(); |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 593 | new_array->AsObjectArray<mirror::Object>()-> |
| 594 | SetWithoutChecks<kTransactionActive>(i, obj); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 595 | } else { |
| 596 | switch (new_annotation_value.type_) { |
| 597 | case DexFile::kDexAnnotationByte: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 598 | new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 599 | i, new_annotation_value.value_.GetB()); |
| 600 | break; |
| 601 | case DexFile::kDexAnnotationShort: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 602 | new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 603 | i, new_annotation_value.value_.GetS()); |
| 604 | break; |
| 605 | case DexFile::kDexAnnotationChar: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 606 | new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 607 | i, new_annotation_value.value_.GetC()); |
| 608 | break; |
| 609 | case DexFile::kDexAnnotationInt: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 610 | new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 611 | i, new_annotation_value.value_.GetI()); |
| 612 | break; |
| 613 | case DexFile::kDexAnnotationLong: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 614 | new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 615 | i, new_annotation_value.value_.GetJ()); |
| 616 | break; |
| 617 | case DexFile::kDexAnnotationFloat: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 618 | new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 619 | i, new_annotation_value.value_.GetF()); |
| 620 | break; |
| 621 | case DexFile::kDexAnnotationDouble: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 622 | new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 623 | i, new_annotation_value.value_.GetD()); |
| 624 | break; |
| 625 | case DexFile::kDexAnnotationBoolean: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 626 | new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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) { |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 669 | element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 670 | set_object = true; |
| 671 | } |
| 672 | |
| 673 | if (set_object) { |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 674 | annotation_value->value_.SetL(element_object.Ptr()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | return true; |
| 678 | } |
| 679 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 680 | mirror::Object* CreateAnnotationMember(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 681 | Handle<mirror::Class> annotation_class, |
| 682 | const uint8_t** annotation) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 683 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 684 | Thread* self = Thread::Current(); |
| 685 | ScopedObjectAccessUnchecked soa(self); |
| 686 | StackHandleScope<5> hs(self); |
| 687 | uint32_t element_name_index = DecodeUnsignedLeb128(annotation); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 688 | const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index)); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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( |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 699 | annotation_method->GetReturnType(true /* resolve */))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 700 | |
| 701 | DexFile::AnnotationValue annotation_value; |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 702 | if (!ProcessAnnotationValue<false>(klass, |
| 703 | annotation, |
| 704 | &annotation_value, |
| 705 | method_return, |
| 706 | DexFile::kAllObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 707 | return nullptr; |
| 708 | } |
| 709 | Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL())); |
| 710 | |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 711 | ObjPtr<mirror::Class> annotation_member_class = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 725 | if (new_member == nullptr || string_name == nullptr || |
| 726 | method_object == nullptr || method_return == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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 = |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 734 | jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 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( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 751 | const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 752 | const DexFile::AnnotationSetItem* annotation_set, |
| 753 | uint32_t visibility, |
| 754 | Handle<mirror::Class> annotation_class) |
| 755 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 756 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 757 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
| 758 | const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
| 759 | if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { |
| 760 | continue; |
| 761 | } |
| 762 | const uint8_t* annotation = annotation_item->annotation_; |
| 763 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 764 | StackHandleScope<2> hs(Thread::Current()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 765 | mirror::Class* resolved_class = Runtime::Current()->GetClassLinker()->ResolveType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 766 | klass.GetDexFile(), |
| 767 | dex::TypeIndex(type_index), |
| 768 | hs.NewHandle(klass.GetDexCache()), |
| 769 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 770 | if (resolved_class == nullptr) { |
| 771 | std::string temp; |
| 772 | LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d", |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 773 | klass.GetRealClass()->GetDescriptor(&temp), type_index); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 774 | CHECK(Thread::Current()->IsExceptionPending()); |
| 775 | Thread::Current()->ClearException(); |
| 776 | continue; |
| 777 | } |
| 778 | if (resolved_class == annotation_class.Get()) { |
| 779 | return annotation_item; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | return nullptr; |
| 784 | } |
| 785 | |
| 786 | mirror::Object* GetAnnotationObjectFromAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 787 | const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 788 | const DexFile::AnnotationSetItem* annotation_set, |
| 789 | uint32_t visibility, |
| 790 | Handle<mirror::Class> annotation_class) |
| 791 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 792 | const DexFile::AnnotationItem* annotation_item = |
| 793 | GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class); |
| 794 | if (annotation_item == nullptr) { |
| 795 | return nullptr; |
| 796 | } |
| 797 | const uint8_t* annotation = annotation_item->annotation_; |
| 798 | return ProcessEncodedAnnotation(klass, &annotation); |
| 799 | } |
| 800 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 801 | mirror::Object* GetAnnotationValue(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 802 | const DexFile::AnnotationItem* annotation_item, |
| 803 | const char* annotation_name, |
| 804 | Handle<mirror::Class> array_class, |
| 805 | uint32_t expected_type) |
| 806 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 807 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 808 | const uint8_t* annotation = |
| 809 | SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name); |
| 810 | if (annotation == nullptr) { |
| 811 | return nullptr; |
| 812 | } |
| 813 | DexFile::AnnotationValue annotation_value; |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 814 | bool result = Runtime::Current()->IsActiveTransaction() |
| 815 | ? ProcessAnnotationValue<true>(klass, |
| 816 | &annotation, |
| 817 | &annotation_value, |
| 818 | array_class, |
| 819 | DexFile::kAllObjects) |
| 820 | : ProcessAnnotationValue<false>(klass, |
| 821 | &annotation, |
| 822 | &annotation_value, |
| 823 | array_class, |
| 824 | DexFile::kAllObjects); |
| 825 | if (!result) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 826 | return nullptr; |
| 827 | } |
| 828 | if (annotation_value.type_ != expected_type) { |
| 829 | return nullptr; |
| 830 | } |
| 831 | return annotation_value.value_.GetL(); |
| 832 | } |
| 833 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 834 | mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 835 | const DexFile::AnnotationSetItem* annotation_set) |
| 836 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 837 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 838 | StackHandleScope<1> hs(Thread::Current()); |
| 839 | const DexFile::AnnotationItem* annotation_item = |
| 840 | SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;", |
| 841 | DexFile::kDexVisibilitySystem); |
| 842 | if (annotation_item == nullptr) { |
| 843 | return nullptr; |
| 844 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 845 | ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 846 | Handle<mirror::Class> string_array_class(hs.NewHandle( |
| 847 | Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 848 | if (string_array_class == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 849 | return nullptr; |
| 850 | } |
| 851 | mirror::Object* obj = |
| 852 | GetAnnotationValue(klass, annotation_item, "value", string_array_class, |
| 853 | DexFile::kDexAnnotationArray); |
| 854 | if (obj == nullptr) { |
| 855 | return nullptr; |
| 856 | } |
| 857 | return obj->AsObjectArray<mirror::String>(); |
| 858 | } |
| 859 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 860 | mirror::ObjectArray<mirror::Class>* GetThrowsValue(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 861 | const DexFile::AnnotationSetItem* annotation_set) |
| 862 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 863 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 864 | StackHandleScope<1> hs(Thread::Current()); |
| 865 | const DexFile::AnnotationItem* annotation_item = |
| 866 | SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;", |
| 867 | DexFile::kDexVisibilitySystem); |
| 868 | if (annotation_item == nullptr) { |
| 869 | return nullptr; |
| 870 | } |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 871 | ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 872 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 873 | Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 874 | if (class_array_class == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 875 | return nullptr; |
| 876 | } |
| 877 | mirror::Object* obj = |
| 878 | GetAnnotationValue(klass, annotation_item, "value", class_array_class, |
| 879 | DexFile::kDexAnnotationArray); |
| 880 | if (obj == nullptr) { |
| 881 | return nullptr; |
| 882 | } |
| 883 | return obj->AsObjectArray<mirror::Class>(); |
| 884 | } |
| 885 | |
| 886 | mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 887 | const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 888 | const DexFile::AnnotationSetItem* annotation_set, |
| 889 | uint32_t visibility) |
| 890 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 891 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 892 | Thread* self = Thread::Current(); |
| 893 | ScopedObjectAccessUnchecked soa(self); |
| 894 | StackHandleScope<2> hs(self); |
| 895 | Handle<mirror::Class> annotation_array_class(hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 896 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 897 | if (annotation_set == nullptr) { |
| 898 | return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0); |
| 899 | } |
| 900 | |
| 901 | uint32_t size = annotation_set->size_; |
| 902 | Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle( |
| 903 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 904 | if (result == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 905 | return nullptr; |
| 906 | } |
| 907 | |
| 908 | uint32_t dest_index = 0; |
| 909 | for (uint32_t i = 0; i < size; ++i) { |
| 910 | const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
| 911 | // Note that we do not use IsVisibilityCompatible here because older code |
| 912 | // was correct for this case. |
| 913 | if (annotation_item->visibility_ != visibility) { |
| 914 | continue; |
| 915 | } |
| 916 | const uint8_t* annotation = annotation_item->annotation_; |
| 917 | mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation); |
| 918 | if (annotation_obj != nullptr) { |
| 919 | result->SetWithoutChecks<false>(dest_index, annotation_obj); |
| 920 | ++dest_index; |
| 921 | } else if (self->IsExceptionPending()) { |
| 922 | return nullptr; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if (dest_index == size) { |
| 927 | return result.Get(); |
| 928 | } |
| 929 | |
| 930 | mirror::ObjectArray<mirror::Object>* trimmed_result = |
| 931 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index); |
| 932 | if (trimmed_result == nullptr) { |
| 933 | return nullptr; |
| 934 | } |
| 935 | |
| 936 | for (uint32_t i = 0; i < dest_index; ++i) { |
| 937 | mirror::Object* obj = result->GetWithoutChecks(i); |
| 938 | trimmed_result->SetWithoutChecks<false>(i, obj); |
| 939 | } |
| 940 | |
| 941 | return trimmed_result; |
| 942 | } |
| 943 | |
| 944 | mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 945 | const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 946 | const DexFile::AnnotationSetRefList* set_ref_list, |
| 947 | uint32_t size) |
| 948 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 949 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 950 | Thread* self = Thread::Current(); |
| 951 | ScopedObjectAccessUnchecked soa(self); |
| 952 | StackHandleScope<1> hs(self); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 953 | ObjPtr<mirror::Class> annotation_array_class = |
| 954 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 955 | mirror::Class* annotation_array_array_class = |
| 956 | Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class); |
| 957 | if (annotation_array_array_class == nullptr) { |
| 958 | return nullptr; |
| 959 | } |
| 960 | Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle( |
| 961 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 962 | if (annotation_array_array == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 963 | LOG(ERROR) << "Annotation set ref array allocation failed"; |
| 964 | return nullptr; |
| 965 | } |
| 966 | for (uint32_t index = 0; index < size; ++index) { |
| 967 | const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index]; |
| 968 | const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item); |
| 969 | mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item, |
| 970 | DexFile::kDexVisibilityRuntime); |
| 971 | if (annotation_set == nullptr) { |
| 972 | return nullptr; |
| 973 | } |
| 974 | annotation_array_array->SetWithoutChecks<false>(index, annotation_set); |
| 975 | } |
| 976 | return annotation_array_array.Get(); |
| 977 | } |
| 978 | } // namespace |
| 979 | |
| 980 | namespace annotations { |
| 981 | |
| 982 | mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) { |
| 983 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 984 | if (annotation_set == nullptr) { |
| 985 | return nullptr; |
| 986 | } |
| 987 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 988 | const ClassData field_class(hs, field); |
| 989 | return GetAnnotationObjectFromAnnotationSet(field_class, |
| 990 | annotation_set, |
| 991 | DexFile::kDexVisibilityRuntime, |
| 992 | annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) { |
| 996 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 997 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 998 | const ClassData field_class(hs, field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 999 | return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime); |
| 1000 | } |
| 1001 | |
| 1002 | mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) { |
| 1003 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1004 | if (annotation_set == nullptr) { |
| 1005 | return nullptr; |
| 1006 | } |
| 1007 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1008 | const ClassData field_class(hs, field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1009 | return GetSignatureValue(field_class, annotation_set); |
| 1010 | } |
| 1011 | |
| 1012 | bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) { |
| 1013 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
| 1014 | if (annotation_set == nullptr) { |
| 1015 | return false; |
| 1016 | } |
| 1017 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1018 | const ClassData field_class(hs, field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1019 | const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
| 1020 | field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); |
| 1021 | return annotation_item != nullptr; |
| 1022 | } |
| 1023 | |
| 1024 | mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1025 | const ClassData klass(method); |
| 1026 | const DexFile* dex_file = &klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1027 | const DexFile::AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1028 | dex_file->GetAnnotationsDirectory(*klass.GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1029 | if (annotations_dir == nullptr) { |
| 1030 | return nullptr; |
| 1031 | } |
| 1032 | const DexFile::AnnotationSetItem* annotation_set = |
| 1033 | dex_file->GetClassAnnotationSet(annotations_dir); |
| 1034 | if (annotation_set == nullptr) { |
| 1035 | return nullptr; |
| 1036 | } |
| 1037 | const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set, |
| 1038 | "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem); |
| 1039 | if (annotation_item == nullptr) { |
| 1040 | return nullptr; |
| 1041 | } |
| 1042 | const uint8_t* annotation = |
| 1043 | SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value"); |
| 1044 | if (annotation == nullptr) { |
| 1045 | return nullptr; |
| 1046 | } |
| 1047 | uint8_t header_byte = *(annotation++); |
| 1048 | if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) { |
| 1049 | return nullptr; |
| 1050 | } |
| 1051 | annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName()); |
| 1052 | if (annotation == nullptr) { |
| 1053 | return nullptr; |
| 1054 | } |
| 1055 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1056 | StackHandleScope<1> hs(Thread::Current()); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1057 | Handle<mirror::Class> return_type(hs.NewHandle(method->GetReturnType(true /* resolve */))); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1058 | if (!ProcessAnnotationValue<false>(klass, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1059 | &annotation, |
| 1060 | &annotation_value, |
| 1061 | return_type, |
| 1062 | DexFile::kAllObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1063 | return nullptr; |
| 1064 | } |
| 1065 | return annotation_value.value_.GetL(); |
| 1066 | } |
| 1067 | |
| 1068 | mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) { |
| 1069 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1070 | if (annotation_set == nullptr) { |
| 1071 | return nullptr; |
| 1072 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1073 | return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1074 | DexFile::kDexVisibilityRuntime, annotation_class); |
| 1075 | } |
| 1076 | |
| 1077 | mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) { |
| 1078 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1079 | return ProcessAnnotationSet(ClassData(method), |
| 1080 | annotation_set, |
| 1081 | DexFile::kDexVisibilityRuntime); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) { |
| 1085 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1086 | if (annotation_set == nullptr) { |
| 1087 | return nullptr; |
| 1088 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1089 | return GetThrowsValue(ClassData(method), annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) { |
| 1093 | const DexFile* dex_file = method->GetDexFile(); |
| 1094 | const DexFile::ParameterAnnotationsItem* parameter_annotations = |
| 1095 | FindAnnotationsItemForMethod(method); |
| 1096 | if (parameter_annotations == nullptr) { |
| 1097 | return nullptr; |
| 1098 | } |
| 1099 | const DexFile::AnnotationSetRefList* set_ref_list = |
| 1100 | dex_file->GetParameterAnnotationSetRefList(parameter_annotations); |
| 1101 | if (set_ref_list == nullptr) { |
| 1102 | return nullptr; |
| 1103 | } |
| 1104 | uint32_t size = set_ref_list->size_; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1105 | return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method, |
| 1109 | uint32_t parameter_idx, |
| 1110 | Handle<mirror::Class> annotation_class) { |
| 1111 | const DexFile* dex_file = method->GetDexFile(); |
| 1112 | const DexFile::ParameterAnnotationsItem* parameter_annotations = |
| 1113 | FindAnnotationsItemForMethod(method); |
| 1114 | if (parameter_annotations == nullptr) { |
| 1115 | return nullptr; |
| 1116 | } |
| 1117 | const DexFile::AnnotationSetRefList* set_ref_list = |
| 1118 | dex_file->GetParameterAnnotationSetRefList(parameter_annotations); |
| 1119 | if (set_ref_list == nullptr) { |
| 1120 | return nullptr; |
| 1121 | } |
| 1122 | if (parameter_idx >= set_ref_list->size_) { |
| 1123 | return nullptr; |
| 1124 | } |
| 1125 | const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx]; |
| 1126 | const DexFile::AnnotationSetItem* annotation_set = |
| 1127 | dex_file->GetSetRefItemItem(annotation_set_ref); |
| 1128 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1129 | return GetAnnotationObjectFromAnnotationSet(ClassData(method), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1130 | annotation_set, |
| 1131 | DexFile::kDexVisibilityRuntime, |
| 1132 | annotation_class); |
| 1133 | } |
| 1134 | |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1135 | bool GetParametersMetadataForMethod(ArtMethod* method, |
| 1136 | MutableHandle<mirror::ObjectArray<mirror::String>>* names, |
| 1137 | MutableHandle<mirror::IntArray>* access_flags) { |
| 1138 | const DexFile::AnnotationSetItem::AnnotationSetItem* annotation_set = |
| 1139 | FindAnnotationSetForMethod(method); |
| 1140 | if (annotation_set == nullptr) { |
| 1141 | return false; |
| 1142 | } |
| 1143 | |
| 1144 | const DexFile* dex_file = method->GetDexFile(); |
| 1145 | const DexFile::AnnotationItem* annotation_item = |
| 1146 | SearchAnnotationSet(*dex_file, |
| 1147 | annotation_set, |
| 1148 | "Ldalvik/annotation/MethodParameters;", |
| 1149 | DexFile::kDexVisibilitySystem); |
| 1150 | if (annotation_item == nullptr) { |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1154 | StackHandleScope<4> hs(Thread::Current()); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1155 | |
| 1156 | // Extract the parameters' names String[]. |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 1157 | ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString(); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1158 | Handle<mirror::Class> string_array_class(hs.NewHandle( |
| 1159 | Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1160 | if (UNLIKELY(string_array_class == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1161 | return false; |
| 1162 | } |
| 1163 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1164 | ClassData data(method); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1165 | Handle<mirror::Object> names_obj = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1166 | hs.NewHandle(GetAnnotationValue(data, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1167 | annotation_item, |
| 1168 | "names", |
| 1169 | string_array_class, |
| 1170 | DexFile::kDexAnnotationArray)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1171 | if (names_obj == nullptr) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | |
| 1175 | // Extract the parameters' access flags int[]. |
| 1176 | Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1177 | if (UNLIKELY(int_array_class == nullptr)) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1178 | return false; |
| 1179 | } |
| 1180 | Handle<mirror::Object> access_flags_obj = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1181 | hs.NewHandle(GetAnnotationValue(data, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1182 | annotation_item, |
| 1183 | "accessFlags", |
| 1184 | int_array_class, |
| 1185 | DexFile::kDexAnnotationArray)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1186 | if (access_flags_obj == nullptr) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | names->Assign(names_obj.Get()->AsObjectArray<mirror::String>()); |
| 1191 | access_flags->Assign(access_flags_obj.Get()->AsIntArray()); |
| 1192 | return true; |
| 1193 | } |
| 1194 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1195 | mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) { |
| 1196 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1197 | if (annotation_set == nullptr) { |
| 1198 | return nullptr; |
| 1199 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1200 | return GetSignatureValue(ClassData(method), annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class, |
| 1204 | uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) { |
| 1205 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
| 1206 | if (annotation_set == nullptr) { |
| 1207 | return false; |
| 1208 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1209 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1210 | GetAnnotationItemFromAnnotationSet(ClassData(method), |
| 1211 | annotation_set, visibility, annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1212 | return annotation_item != nullptr; |
| 1213 | } |
| 1214 | |
| 1215 | mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass, |
| 1216 | Handle<mirror::Class> annotation_class) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1217 | ClassData data(klass); |
| 1218 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1219 | if (annotation_set == nullptr) { |
| 1220 | return nullptr; |
| 1221 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1222 | return GetAnnotationObjectFromAnnotationSet(data, |
| 1223 | annotation_set, |
| 1224 | DexFile::kDexVisibilityRuntime, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1225 | annotation_class); |
| 1226 | } |
| 1227 | |
| 1228 | mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1229 | ClassData data(klass); |
| 1230 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
| 1231 | return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1235 | ClassData data(klass); |
| 1236 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1237 | if (annotation_set == nullptr) { |
| 1238 | return nullptr; |
| 1239 | } |
| 1240 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1241 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1242 | DexFile::kDexVisibilitySystem); |
| 1243 | if (annotation_item == nullptr) { |
| 1244 | return nullptr; |
| 1245 | } |
| 1246 | StackHandleScope<1> hs(Thread::Current()); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 1247 | ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1248 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
| 1249 | Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1250 | if (class_array_class == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1251 | return nullptr; |
| 1252 | } |
| 1253 | mirror::Object* obj = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1254 | GetAnnotationValue(data, annotation_item, "value", class_array_class, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1255 | DexFile::kDexAnnotationArray); |
| 1256 | if (obj == nullptr) { |
| 1257 | return nullptr; |
| 1258 | } |
| 1259 | return obj->AsObjectArray<mirror::Class>(); |
| 1260 | } |
| 1261 | |
| 1262 | mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1263 | ClassData data(klass); |
| 1264 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1265 | if (annotation_set == nullptr) { |
| 1266 | return nullptr; |
| 1267 | } |
| 1268 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1269 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1270 | DexFile::kDexVisibilitySystem); |
| 1271 | if (annotation_item == nullptr) { |
| 1272 | return nullptr; |
| 1273 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1274 | mirror::Object* obj = GetAnnotationValue(data, annotation_item, "value", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1275 | ScopedNullHandle<mirror::Class>(), |
| 1276 | DexFile::kDexAnnotationType); |
| 1277 | if (obj == nullptr) { |
| 1278 | return nullptr; |
| 1279 | } |
| 1280 | return obj->AsClass(); |
| 1281 | } |
| 1282 | |
| 1283 | mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1284 | mirror::Class* declaring_class = GetDeclaringClass(klass); |
| 1285 | if (declaring_class != nullptr) { |
| 1286 | return declaring_class; |
| 1287 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1288 | ClassData data(klass); |
| 1289 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1290 | if (annotation_set == nullptr) { |
| 1291 | return nullptr; |
| 1292 | } |
| 1293 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1294 | SearchAnnotationSet(data.GetDexFile(), |
| 1295 | annotation_set, |
| 1296 | "Ldalvik/annotation/EnclosingMethod;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1297 | DexFile::kDexVisibilitySystem); |
| 1298 | if (annotation_item == nullptr) { |
| 1299 | return nullptr; |
| 1300 | } |
| 1301 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1302 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1303 | if (annotation == nullptr) { |
| 1304 | return nullptr; |
| 1305 | } |
| 1306 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1307 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1308 | &annotation, |
| 1309 | &annotation_value, |
| 1310 | ScopedNullHandle<mirror::Class>(), |
| 1311 | DexFile::kAllRaw)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1312 | return nullptr; |
| 1313 | } |
| 1314 | if (annotation_value.type_ != DexFile::kDexAnnotationMethod) { |
| 1315 | return nullptr; |
| 1316 | } |
| 1317 | StackHandleScope<2> hs(Thread::Current()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1318 | ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1319 | data.GetDexFile(), |
| 1320 | annotation_value.value_.GetI(), |
| 1321 | hs.NewHandle(data.GetDexCache()), |
| 1322 | hs.NewHandle(data.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1323 | if (method == nullptr) { |
| 1324 | return nullptr; |
| 1325 | } |
| 1326 | return method->GetDeclaringClass(); |
| 1327 | } |
| 1328 | |
| 1329 | mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1330 | ClassData data(klass); |
| 1331 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1332 | if (annotation_set == nullptr) { |
| 1333 | return nullptr; |
| 1334 | } |
| 1335 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1336 | SearchAnnotationSet(data.GetDexFile(), |
| 1337 | annotation_set, |
| 1338 | "Ldalvik/annotation/EnclosingMethod;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1339 | DexFile::kDexVisibilitySystem); |
| 1340 | if (annotation_item == nullptr) { |
| 1341 | return nullptr; |
| 1342 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1343 | return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1344 | DexFile::kDexAnnotationMethod); |
| 1345 | } |
| 1346 | |
| 1347 | bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1348 | ClassData data(klass); |
| 1349 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1350 | if (annotation_set == nullptr) { |
| 1351 | return false; |
| 1352 | } |
| 1353 | const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1354 | data.GetDexFile(), |
| 1355 | annotation_set, |
| 1356 | "Ldalvik/annotation/InnerClass;", |
| 1357 | DexFile::kDexVisibilitySystem); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1358 | if (annotation_item == nullptr) { |
| 1359 | return false; |
| 1360 | } |
| 1361 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1362 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1363 | if (annotation == nullptr) { |
| 1364 | return false; |
| 1365 | } |
| 1366 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1367 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1368 | &annotation, |
| 1369 | &annotation_value, |
| 1370 | ScopedNullHandle<mirror::Class>(), |
| 1371 | DexFile::kAllObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1372 | return false; |
| 1373 | } |
| 1374 | if (annotation_value.type_ != DexFile::kDexAnnotationNull && |
| 1375 | annotation_value.type_ != DexFile::kDexAnnotationString) { |
| 1376 | return false; |
| 1377 | } |
| 1378 | *name = down_cast<mirror::String*>(annotation_value.value_.GetL()); |
| 1379 | return true; |
| 1380 | } |
| 1381 | |
| 1382 | bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1383 | ClassData data(klass); |
| 1384 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1385 | if (annotation_set == nullptr) { |
| 1386 | return false; |
| 1387 | } |
| 1388 | const DexFile::AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1389 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1390 | DexFile::kDexVisibilitySystem); |
| 1391 | if (annotation_item == nullptr) { |
| 1392 | return false; |
| 1393 | } |
| 1394 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1395 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1396 | if (annotation == nullptr) { |
| 1397 | return false; |
| 1398 | } |
| 1399 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1400 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1401 | &annotation, |
| 1402 | &annotation_value, |
| 1403 | ScopedNullHandle<mirror::Class>(), |
| 1404 | DexFile::kAllRaw)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
| 1407 | if (annotation_value.type_ != DexFile::kDexAnnotationInt) { |
| 1408 | return false; |
| 1409 | } |
| 1410 | *flags = annotation_value.value_.GetI(); |
| 1411 | return true; |
| 1412 | } |
| 1413 | |
| 1414 | mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1415 | ClassData data(klass); |
| 1416 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1417 | if (annotation_set == nullptr) { |
| 1418 | return nullptr; |
| 1419 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1420 | return GetSignatureValue(data, annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1424 | ClassData data(klass); |
| 1425 | const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1426 | if (annotation_set == nullptr) { |
| 1427 | return false; |
| 1428 | } |
| 1429 | const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1430 | data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1431 | return annotation_item != nullptr; |
| 1432 | } |
| 1433 | |
| 1434 | int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) { |
| 1435 | // For native method, lineno should be -2 to indicate it is native. Note that |
| 1436 | // "line number == -2" is how libcore tells from StackTraceElement. |
| 1437 | if (method->GetCodeItemOffset() == 0) { |
| 1438 | return -2; |
| 1439 | } |
| 1440 | |
| 1441 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1442 | DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1443 | |
| 1444 | // A method with no line number info should return -1 |
| 1445 | DexFile::LineNumFromPcContext context(rel_pc, -1); |
| 1446 | dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context); |
| 1447 | return context.line_num_; |
| 1448 | } |
| 1449 | |
| 1450 | template<bool kTransactionActive> |
| 1451 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const { |
| 1452 | DCHECK(dex_cache_ != nullptr); |
| 1453 | DCHECK(class_loader_ != nullptr); |
| 1454 | switch (type_) { |
| 1455 | case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); |
| 1456 | break; |
| 1457 | case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break; |
| 1458 | case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break; |
| 1459 | case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break; |
| 1460 | case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break; |
| 1461 | case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break; |
| 1462 | case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break; |
| 1463 | case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break; |
| 1464 | case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break; |
| 1465 | case kString: { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1466 | mirror::String* resolved = linker_->ResolveString(dex_file_, |
| 1467 | dex::StringIndex(jval_.i), |
| 1468 | *dex_cache_); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1469 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
| 1470 | break; |
| 1471 | } |
| 1472 | case kType: { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1473 | mirror::Class* resolved = linker_->ResolveType(dex_file_, |
| 1474 | dex::TypeIndex(jval_.i), |
| 1475 | *dex_cache_, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1476 | *class_loader_); |
| 1477 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
| 1478 | break; |
| 1479 | } |
| 1480 | default: UNIMPLEMENTED(FATAL) << ": type " << type_; |
| 1481 | } |
| 1482 | } |
| 1483 | template |
| 1484 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const; |
| 1485 | template |
| 1486 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const; |
| 1487 | |
| 1488 | } // namespace annotations |
| 1489 | |
| 1490 | } // namespace art |