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