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