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