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 | |
Victor Chang | c8507c8 | 2023-04-22 09:24:57 +0100 | [diff] [blame] | 21 | #include "android-base/macros.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 22 | #include "android-base/stringprintf.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 23 | #include "art_field-inl.h" |
Vladimir Marko | b9c73f8 | 2022-11-22 15:18:43 +0000 | [diff] [blame] | 24 | #include "art_method-alloc-inl.h" |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 25 | #include "base/sdk_version.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 26 | #include "class_linker-inl.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 27 | #include "class_root-inl.h" |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 28 | #include "dex/dex_file-inl.h" |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 29 | #include "dex/dex_instruction-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 30 | #include "jni/jni_internal.h" |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 31 | #include "jvalue-inl.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 32 | #include "mirror/array-alloc-inl.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 33 | #include "mirror/class-alloc-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 34 | #include "mirror/field.h" |
| 35 | #include "mirror/method.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 36 | #include "mirror/object_array-alloc-inl.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 37 | #include "mirror/object_array-inl.h" |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 38 | #include "oat_file.h" |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 39 | #include "obj_ptr-inl.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 40 | #include "reflection.h" |
| 41 | #include "thread.h" |
Andreas Gampe | a7c83ac | 2017-09-11 08:14:23 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 46 | using android::base::StringPrintf; |
| 47 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 48 | using dex::AnnotationItem; |
| 49 | using dex::AnnotationSetItem; |
| 50 | using dex::AnnotationSetRefItem; |
| 51 | using dex::AnnotationSetRefList; |
| 52 | using dex::AnnotationsDirectoryItem; |
| 53 | using dex::FieldAnnotationsItem; |
| 54 | using dex::MethodAnnotationsItem; |
| 55 | using dex::ParameterAnnotationsItem; |
| 56 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 57 | struct DexFile::AnnotationValue { |
| 58 | JValue value_; |
| 59 | uint8_t type_; |
| 60 | }; |
| 61 | |
| 62 | namespace { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 63 | |
| 64 | // A helper class that contains all the data needed to do annotation lookup. |
| 65 | class ClassData { |
| 66 | public: |
| 67 | explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) |
| 68 | : ClassData(ScopedNullHandle<mirror::Class>(), // klass |
| 69 | method, |
| 70 | *method->GetDexFile(), |
| 71 | &method->GetClassDef()) {} |
| 72 | |
| 73 | // Requires Scope to be able to create at least 1 handles. |
| 74 | template <typename Scope> |
| 75 | ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_) |
| 76 | : ClassData(hs.NewHandle(field->GetDeclaringClass())) { } |
| 77 | |
| 78 | explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 79 | : ClassData(klass, // klass |
| 80 | nullptr, // method |
| 81 | klass->GetDexFile(), |
| 82 | klass->GetClassDef()) {} |
| 83 | |
| 84 | const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 85 | return dex_file_; |
| 86 | } |
| 87 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 88 | const dex::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 89 | return class_def_; |
| 90 | } |
| 91 | |
| 92 | ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 93 | if (method_ != nullptr) { |
| 94 | return method_->GetDexCache(); |
| 95 | } else { |
| 96 | return real_klass_->GetDexCache(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 101 | if (method_ != nullptr) { |
| 102 | return method_->GetDeclaringClass()->GetClassLoader(); |
| 103 | } else { |
| 104 | return real_klass_->GetClassLoader(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 109 | if (method_ != nullptr) { |
| 110 | return method_->GetDeclaringClass(); |
| 111 | } else { |
| 112 | return real_klass_.Get(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | ClassData(Handle<mirror::Class> klass, |
| 118 | ArtMethod* method, |
| 119 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 120 | const dex::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_) |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 121 | : real_klass_(klass), |
| 122 | method_(method), |
| 123 | dex_file_(dex_file), |
| 124 | class_def_(class_def) { |
| 125 | DCHECK((method_ == nullptr) || real_klass_.IsNull()); |
| 126 | } |
| 127 | |
| 128 | Handle<mirror::Class> real_klass_; |
| 129 | ArtMethod* method_; |
| 130 | const DexFile& dex_file_; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 131 | const dex::ClassDef* class_def_; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 132 | |
| 133 | DISALLOW_COPY_AND_ASSIGN(ClassData); |
| 134 | }; |
| 135 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 136 | ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass, |
| 137 | Handle<mirror::Class> annotation_class, |
| 138 | const uint8_t** annotation) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 139 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 140 | |
| 141 | bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) { |
| 142 | if (expected == DexFile::kDexVisibilityRuntime) { |
David Brazdil | 2bb2fbd | 2018-11-13 18:24:26 +0000 | [diff] [blame] | 143 | if (IsSdkVersionSetAndAtMost(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kM)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 144 | return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild; |
| 145 | } |
| 146 | } |
| 147 | return actual == expected; |
| 148 | } |
| 149 | |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 150 | static const AnnotationSetItem* FindAnnotationSetForField(const DexFile& dex_file, |
| 151 | const dex::ClassDef& class_def, |
| 152 | uint32_t field_index) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 153 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 154 | const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def); |
| 155 | if (annotations_dir == nullptr) { |
| 156 | return nullptr; |
| 157 | } |
| 158 | const FieldAnnotationsItem* field_annotations = dex_file.GetFieldAnnotations(annotations_dir); |
| 159 | if (field_annotations == nullptr) { |
| 160 | return nullptr; |
| 161 | } |
| 162 | uint32_t field_count = annotations_dir->fields_size_; |
| 163 | for (uint32_t i = 0; i < field_count; ++i) { |
| 164 | if (field_annotations[i].field_idx_ == field_index) { |
| 165 | return dex_file.GetFieldAnnotationSetItem(field_annotations[i]); |
| 166 | } |
| 167 | } |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | static const AnnotationSetItem* FindAnnotationSetForField(ArtField* field) |
| 172 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 173 | ObjPtr<mirror::Class> klass = field->GetDeclaringClass(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 174 | const dex::ClassDef* class_def = klass->GetClassDef(); |
Alex Light | 89f33b8 | 2017-10-23 16:37:03 -0700 | [diff] [blame] | 175 | if (class_def == nullptr) { |
Alex Light | e0d8ae2 | 2017-10-24 10:23:16 -0700 | [diff] [blame] | 176 | DCHECK(klass->IsProxyClass()); |
Alex Light | 89f33b8 | 2017-10-23 16:37:03 -0700 | [diff] [blame] | 177 | return nullptr; |
| 178 | } |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 179 | return FindAnnotationSetForField(*field->GetDexFile(), *class_def, field->GetDexFieldIndex()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 182 | const AnnotationItem* SearchAnnotationSet(const DexFile& dex_file, |
| 183 | const AnnotationSetItem* annotation_set, |
| 184 | const char* descriptor, |
| 185 | uint32_t visibility) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 186 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 187 | const AnnotationItem* result = nullptr; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 188 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 189 | const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 190 | if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { |
| 191 | continue; |
| 192 | } |
| 193 | const uint8_t* annotation = annotation_item->annotation_; |
| 194 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
| 195 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 196 | if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 197 | result = annotation_item; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
Victor Chang | c8507c8 | 2023-04-22 09:24:57 +0100 | [diff] [blame] | 204 | inline static void SkipEncodedValueHeaderByte(const uint8_t** annotation_ptr) { |
| 205 | (*annotation_ptr)++; |
| 206 | } |
| 207 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 208 | bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr) |
| 209 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 210 | const uint8_t* annotation = *annotation_ptr; |
| 211 | uint8_t header_byte = *(annotation++); |
| 212 | uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; |
| 213 | uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; |
| 214 | int32_t width = value_arg + 1; |
| 215 | |
| 216 | switch (value_type) { |
| 217 | case DexFile::kDexAnnotationByte: |
| 218 | case DexFile::kDexAnnotationShort: |
| 219 | case DexFile::kDexAnnotationChar: |
| 220 | case DexFile::kDexAnnotationInt: |
| 221 | case DexFile::kDexAnnotationLong: |
| 222 | case DexFile::kDexAnnotationFloat: |
| 223 | case DexFile::kDexAnnotationDouble: |
| 224 | case DexFile::kDexAnnotationString: |
| 225 | case DexFile::kDexAnnotationType: |
| 226 | case DexFile::kDexAnnotationMethod: |
| 227 | case DexFile::kDexAnnotationField: |
| 228 | case DexFile::kDexAnnotationEnum: |
| 229 | break; |
| 230 | case DexFile::kDexAnnotationArray: |
| 231 | { |
| 232 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
Andreas Gampe | c74d9cb | 2018-09-20 13:44:44 -0700 | [diff] [blame] | 233 | for (; size != 0u; --size) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 234 | if (!SkipAnnotationValue(dex_file, &annotation)) { |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | width = 0; |
| 239 | break; |
| 240 | } |
| 241 | case DexFile::kDexAnnotationAnnotation: |
| 242 | { |
| 243 | DecodeUnsignedLeb128(&annotation); // unused type_index |
| 244 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
Andreas Gampe | c74d9cb | 2018-09-20 13:44:44 -0700 | [diff] [blame] | 245 | for (; size != 0u; --size) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 246 | DecodeUnsignedLeb128(&annotation); // unused element_name_index |
| 247 | if (!SkipAnnotationValue(dex_file, &annotation)) { |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | width = 0; |
| 252 | break; |
| 253 | } |
| 254 | case DexFile::kDexAnnotationBoolean: |
| 255 | case DexFile::kDexAnnotationNull: |
| 256 | width = 0; |
| 257 | break; |
| 258 | default: |
| 259 | LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type); |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 260 | UNREACHABLE(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | annotation += width; |
| 264 | *annotation_ptr = annotation; |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file, |
| 269 | const uint8_t* annotation, |
| 270 | const char* name) |
| 271 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 272 | DecodeUnsignedLeb128(&annotation); // unused type_index |
| 273 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 274 | |
| 275 | while (size != 0) { |
| 276 | uint32_t element_name_index = DecodeUnsignedLeb128(&annotation); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 277 | const char* element_name = |
| 278 | dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 279 | if (strcmp(name, element_name) == 0) { |
| 280 | return annotation; |
| 281 | } |
| 282 | SkipAnnotationValue(dex_file, &annotation); |
| 283 | size--; |
| 284 | } |
| 285 | return nullptr; |
| 286 | } |
| 287 | |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 288 | static const AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file, |
| 289 | const dex::ClassDef& class_def, |
| 290 | uint32_t method_index) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 291 | const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 292 | if (annotations_dir == nullptr) { |
| 293 | return nullptr; |
| 294 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 295 | const MethodAnnotationsItem* method_annotations = dex_file.GetMethodAnnotations(annotations_dir); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 296 | if (method_annotations == nullptr) { |
| 297 | return nullptr; |
| 298 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 299 | uint32_t method_count = annotations_dir->methods_size_; |
| 300 | for (uint32_t i = 0; i < method_count; ++i) { |
| 301 | if (method_annotations[i].method_idx_ == method_index) { |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 302 | return dex_file.GetMethodAnnotationSetItem(method_annotations[i]); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | return nullptr; |
| 306 | } |
| 307 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 308 | inline const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method) |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 309 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 310 | if (method->IsProxyMethod()) { |
| 311 | return nullptr; |
| 312 | } |
| 313 | return FindAnnotationSetForMethod(*method->GetDexFile(), |
| 314 | method->GetClassDef(), |
| 315 | method->GetDexMethodIndex()); |
| 316 | } |
| 317 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 318 | const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 319 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 320 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 321 | const AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 322 | dex_file->GetAnnotationsDirectory(method->GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 323 | if (annotations_dir == nullptr) { |
| 324 | return nullptr; |
| 325 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 326 | const ParameterAnnotationsItem* parameter_annotations = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 327 | dex_file->GetParameterAnnotations(annotations_dir); |
| 328 | if (parameter_annotations == nullptr) { |
| 329 | return nullptr; |
| 330 | } |
| 331 | uint32_t method_index = method->GetDexMethodIndex(); |
| 332 | uint32_t parameter_count = annotations_dir->parameters_size_; |
| 333 | for (uint32_t i = 0; i < parameter_count; ++i) { |
| 334 | if (parameter_annotations[i].method_idx_ == method_index) { |
| 335 | return ¶meter_annotations[i]; |
| 336 | } |
| 337 | } |
| 338 | return nullptr; |
| 339 | } |
| 340 | |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 341 | static const AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 342 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 343 | const DexFile& dex_file = klass.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 344 | const dex::ClassDef* class_def = klass.GetClassDef(); |
Alex Light | 89f33b8 | 2017-10-23 16:37:03 -0700 | [diff] [blame] | 345 | if (class_def == nullptr) { |
Alex Light | e0d8ae2 | 2017-10-24 10:23:16 -0700 | [diff] [blame] | 346 | DCHECK(klass.GetRealClass()->IsProxyClass()); |
Alex Light | 89f33b8 | 2017-10-23 16:37:03 -0700 | [diff] [blame] | 347 | return nullptr; |
| 348 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 349 | const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(*class_def); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 350 | if (annotations_dir == nullptr) { |
| 351 | return nullptr; |
| 352 | } |
| 353 | return dex_file.GetClassAnnotationSet(annotations_dir); |
| 354 | } |
| 355 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 356 | ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 357 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 358 | uint32_t type_index = DecodeUnsignedLeb128(annotation); |
| 359 | uint32_t size = DecodeUnsignedLeb128(annotation); |
| 360 | |
| 361 | Thread* self = Thread::Current(); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 362 | StackHandleScope<4> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 363 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 364 | Handle<mirror::Class> annotation_class(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 365 | class_linker->ResolveType(dex::TypeIndex(type_index), |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 366 | hs.NewHandle(klass.GetDexCache()), |
| 367 | hs.NewHandle(klass.GetClassLoader())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 368 | if (annotation_class == nullptr) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 369 | LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass() |
| 370 | << " annotation class " << type_index; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 371 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 372 | Thread::Current()->ClearException(); |
| 373 | return nullptr; |
| 374 | } |
| 375 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 376 | ObjPtr<mirror::Class> annotation_member_array_class = |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 377 | WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember__array); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 378 | if (annotation_member_array_class == nullptr) { |
| 379 | return nullptr; |
| 380 | } |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 381 | ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 382 | if (size > 0) { |
| 383 | element_array = |
| 384 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size); |
| 385 | if (element_array == nullptr) { |
| 386 | LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)"; |
| 387 | return nullptr; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array)); |
| 392 | for (uint32_t i = 0; i < size; ++i) { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 393 | ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 394 | if (new_member == nullptr) { |
| 395 | return nullptr; |
| 396 | } |
| 397 | h_element_array->SetWithoutChecks<false>(i, new_member); |
| 398 | } |
| 399 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 400 | ArtMethod* create_annotation_method = |
Vladimir Marko | f776c17 | 2022-11-03 17:29:49 +0000 | [diff] [blame] | 401 | WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation; |
Vladimir Marko | f776c17 | 2022-11-03 17:29:49 +0000 | [diff] [blame] | 402 | ObjPtr<mirror::Object> result = create_annotation_method->InvokeStatic<'L', 'L', 'L'>( |
| 403 | self, annotation_class.Get(), h_element_array.Get()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 404 | if (self->IsExceptionPending()) { |
| 405 | LOG(INFO) << "Exception in AnnotationFactory.createAnnotation"; |
| 406 | return nullptr; |
| 407 | } |
| 408 | |
Vladimir Marko | f776c17 | 2022-11-03 17:29:49 +0000 | [diff] [blame] | 409 | return result; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 412 | template <bool kTransactionActive> |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 413 | bool ProcessAnnotationValue(const ClassData& klass, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 414 | const uint8_t** annotation_ptr, |
| 415 | DexFile::AnnotationValue* annotation_value, |
| 416 | Handle<mirror::Class> array_class, |
| 417 | DexFile::AnnotationResultStyle result_style) |
| 418 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 419 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 420 | Thread* self = Thread::Current(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 421 | ObjPtr<mirror::Object> element_object = nullptr; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 422 | bool set_object = false; |
| 423 | Primitive::Type primitive_type = Primitive::kPrimVoid; |
| 424 | const uint8_t* annotation = *annotation_ptr; |
| 425 | uint8_t header_byte = *(annotation++); |
| 426 | uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask; |
| 427 | uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift; |
| 428 | int32_t width = value_arg + 1; |
| 429 | annotation_value->type_ = value_type; |
| 430 | |
| 431 | switch (value_type) { |
| 432 | case DexFile::kDexAnnotationByte: |
| 433 | annotation_value->value_.SetB( |
| 434 | static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg))); |
| 435 | primitive_type = Primitive::kPrimByte; |
| 436 | break; |
| 437 | case DexFile::kDexAnnotationShort: |
| 438 | annotation_value->value_.SetS( |
| 439 | static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg))); |
| 440 | primitive_type = Primitive::kPrimShort; |
| 441 | break; |
| 442 | case DexFile::kDexAnnotationChar: |
| 443 | annotation_value->value_.SetC( |
| 444 | static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false))); |
| 445 | primitive_type = Primitive::kPrimChar; |
| 446 | break; |
| 447 | case DexFile::kDexAnnotationInt: |
| 448 | annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg)); |
| 449 | primitive_type = Primitive::kPrimInt; |
| 450 | break; |
| 451 | case DexFile::kDexAnnotationLong: |
| 452 | annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg)); |
| 453 | primitive_type = Primitive::kPrimLong; |
| 454 | break; |
| 455 | case DexFile::kDexAnnotationFloat: |
| 456 | annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true)); |
| 457 | primitive_type = Primitive::kPrimFloat; |
| 458 | break; |
| 459 | case DexFile::kDexAnnotationDouble: |
| 460 | annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true)); |
| 461 | primitive_type = Primitive::kPrimDouble; |
| 462 | break; |
| 463 | case DexFile::kDexAnnotationBoolean: |
| 464 | annotation_value->value_.SetZ(value_arg != 0); |
| 465 | primitive_type = Primitive::kPrimBoolean; |
| 466 | width = 0; |
| 467 | break; |
| 468 | case DexFile::kDexAnnotationString: { |
| 469 | uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); |
| 470 | if (result_style == DexFile::kAllRaw) { |
| 471 | annotation_value->value_.SetI(index); |
| 472 | } else { |
| 473 | StackHandleScope<1> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 474 | element_object = Runtime::Current()->GetClassLinker()->ResolveString( |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 475 | dex::StringIndex(index), hs.NewHandle(klass.GetDexCache())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 476 | set_object = true; |
| 477 | if (element_object == nullptr) { |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | break; |
| 482 | } |
| 483 | case DexFile::kDexAnnotationType: { |
| 484 | uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); |
| 485 | if (result_style == DexFile::kAllRaw) { |
| 486 | annotation_value->value_.SetI(index); |
| 487 | } else { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 488 | dex::TypeIndex type_index(index); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 489 | StackHandleScope<2> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 490 | element_object = Runtime::Current()->GetClassLinker()->ResolveType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 491 | type_index, |
| 492 | hs.NewHandle(klass.GetDexCache()), |
| 493 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 494 | set_object = true; |
| 495 | if (element_object == nullptr) { |
| 496 | CHECK(self->IsExceptionPending()); |
| 497 | if (result_style == DexFile::kAllObjects) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 498 | const char* msg = dex_file.StringByTypeIdx(type_index); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 499 | self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg); |
| 500 | element_object = self->GetException(); |
| 501 | self->ClearException(); |
| 502 | } else { |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | case DexFile::kDexAnnotationMethod: { |
| 510 | uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); |
| 511 | if (result_style == DexFile::kAllRaw) { |
| 512 | annotation_value->value_.SetI(index); |
| 513 | } else { |
Nicolas Geoffray | 65e0775 | 2017-03-15 06:56:35 +0000 | [diff] [blame] | 514 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 515 | StackHandleScope<2> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 516 | ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 517 | index, |
| 518 | hs.NewHandle(klass.GetDexCache()), |
| 519 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 520 | if (method == nullptr) { |
| 521 | return false; |
| 522 | } |
| 523 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
| 524 | set_object = true; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 525 | if (method->IsConstructor()) { |
Vladimir Marko | b6f4c79 | 2020-05-04 15:37:29 +0100 | [diff] [blame] | 526 | element_object = (pointer_size == PointerSize::k64) |
| 527 | ? mirror::Constructor::CreateFromArtMethod<PointerSize::k64>(self, method) |
| 528 | : mirror::Constructor::CreateFromArtMethod<PointerSize::k32>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 529 | } else { |
Vladimir Marko | b6f4c79 | 2020-05-04 15:37:29 +0100 | [diff] [blame] | 530 | element_object = (pointer_size == PointerSize::k64) |
| 531 | ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, method) |
| 532 | : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 533 | } |
| 534 | if (element_object == nullptr) { |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | case DexFile::kDexAnnotationField: { |
| 541 | uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); |
| 542 | if (result_style == DexFile::kAllRaw) { |
| 543 | annotation_value->value_.SetI(index); |
| 544 | } else { |
| 545 | StackHandleScope<2> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 546 | ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 547 | index, |
| 548 | hs.NewHandle(klass.GetDexCache()), |
| 549 | hs.NewHandle(klass.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 550 | if (field == nullptr) { |
| 551 | return false; |
| 552 | } |
| 553 | set_object = true; |
Vladimir Marko | 0a6063a | 2020-05-14 16:39:14 +0100 | [diff] [blame] | 554 | element_object = mirror::Field::CreateFromArtField(self, field, true); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 555 | if (element_object == nullptr) { |
| 556 | return false; |
| 557 | } |
| 558 | } |
| 559 | break; |
| 560 | } |
| 561 | case DexFile::kDexAnnotationEnum: { |
| 562 | uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false); |
| 563 | if (result_style == DexFile::kAllRaw) { |
| 564 | annotation_value->value_.SetI(index); |
| 565 | } else { |
| 566 | StackHandleScope<3> hs(self); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 567 | ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 568 | index, |
| 569 | hs.NewHandle(klass.GetDexCache()), |
| 570 | hs.NewHandle(klass.GetClassLoader()), |
| 571 | true); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 572 | if (enum_field == nullptr) { |
| 573 | return false; |
| 574 | } else { |
| 575 | Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass())); |
| 576 | Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true); |
| 577 | element_object = enum_field->GetObject(field_class.Get()); |
| 578 | set_object = true; |
| 579 | } |
| 580 | } |
| 581 | break; |
| 582 | } |
| 583 | case DexFile::kDexAnnotationArray: |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 584 | if (result_style == DexFile::kAllRaw || array_class == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 585 | return false; |
| 586 | } else { |
| 587 | ScopedObjectAccessUnchecked soa(self); |
| 588 | StackHandleScope<2> hs(self); |
| 589 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 590 | Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType())); |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 591 | Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 592 | self, array_class.Get(), size, array_class->GetComponentSizeShift(), |
| 593 | Runtime::Current()->GetHeap()->GetCurrentAllocator()))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 594 | if (new_array == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 595 | LOG(ERROR) << "Annotation element array allocation failed with size " << size; |
| 596 | return false; |
| 597 | } |
| 598 | DexFile::AnnotationValue new_annotation_value; |
| 599 | for (uint32_t i = 0; i < size; ++i) { |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 600 | if (!ProcessAnnotationValue<kTransactionActive>(klass, |
| 601 | &annotation, |
| 602 | &new_annotation_value, |
| 603 | component_type, |
| 604 | DexFile::kPrimitivesOrObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | if (!component_type->IsPrimitive()) { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 608 | ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL(); |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 609 | new_array->AsObjectArray<mirror::Object>()-> |
| 610 | SetWithoutChecks<kTransactionActive>(i, obj); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 611 | } else { |
| 612 | switch (new_annotation_value.type_) { |
| 613 | case DexFile::kDexAnnotationByte: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 614 | new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 615 | i, new_annotation_value.value_.GetB()); |
| 616 | break; |
| 617 | case DexFile::kDexAnnotationShort: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 618 | new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 619 | i, new_annotation_value.value_.GetS()); |
| 620 | break; |
| 621 | case DexFile::kDexAnnotationChar: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 622 | new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 623 | i, new_annotation_value.value_.GetC()); |
| 624 | break; |
| 625 | case DexFile::kDexAnnotationInt: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 626 | new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 627 | i, new_annotation_value.value_.GetI()); |
| 628 | break; |
| 629 | case DexFile::kDexAnnotationLong: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 630 | new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 631 | i, new_annotation_value.value_.GetJ()); |
| 632 | break; |
| 633 | case DexFile::kDexAnnotationFloat: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 634 | new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 635 | i, new_annotation_value.value_.GetF()); |
| 636 | break; |
| 637 | case DexFile::kDexAnnotationDouble: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 638 | new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 639 | i, new_annotation_value.value_.GetD()); |
| 640 | break; |
| 641 | case DexFile::kDexAnnotationBoolean: |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 642 | new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 643 | i, new_annotation_value.value_.GetZ()); |
| 644 | break; |
| 645 | default: |
| 646 | LOG(FATAL) << "Found invalid annotation value type while building annotation array"; |
| 647 | return false; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | element_object = new_array.Get(); |
| 652 | set_object = true; |
| 653 | width = 0; |
| 654 | } |
| 655 | break; |
| 656 | case DexFile::kDexAnnotationAnnotation: |
| 657 | if (result_style == DexFile::kAllRaw) { |
| 658 | return false; |
| 659 | } |
| 660 | element_object = ProcessEncodedAnnotation(klass, &annotation); |
| 661 | if (element_object == nullptr) { |
| 662 | return false; |
| 663 | } |
| 664 | set_object = true; |
| 665 | width = 0; |
| 666 | break; |
| 667 | case DexFile::kDexAnnotationNull: |
| 668 | if (result_style == DexFile::kAllRaw) { |
| 669 | annotation_value->value_.SetI(0); |
| 670 | } else { |
| 671 | CHECK(element_object == nullptr); |
| 672 | set_object = true; |
| 673 | } |
| 674 | width = 0; |
| 675 | break; |
| 676 | default: |
| 677 | LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type); |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | annotation += width; |
| 682 | *annotation_ptr = annotation; |
| 683 | |
| 684 | if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 685 | element_object = BoxPrimitive(primitive_type, annotation_value->value_); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 686 | set_object = true; |
| 687 | } |
| 688 | |
| 689 | if (set_object) { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 690 | annotation_value->value_.SetL(element_object); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | return true; |
| 694 | } |
| 695 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 696 | ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass, |
| 697 | Handle<mirror::Class> annotation_class, |
| 698 | const uint8_t** annotation) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 699 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 700 | Thread* self = Thread::Current(); |
| 701 | ScopedObjectAccessUnchecked soa(self); |
| 702 | StackHandleScope<5> hs(self); |
| 703 | uint32_t element_name_index = DecodeUnsignedLeb128(annotation); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 704 | const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index)); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 705 | |
| 706 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 707 | ArtMethod* annotation_method = |
| 708 | annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size); |
| 709 | if (annotation_method == nullptr) { |
| 710 | return nullptr; |
| 711 | } |
Vladimir Marko | b1baa73 | 2022-11-11 12:43:11 +0000 | [diff] [blame] | 712 | |
| 713 | Handle<mirror::String> string_name = |
| 714 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)); |
| 715 | if (UNLIKELY(string_name == nullptr)) { |
| 716 | LOG(ERROR) << "Failed to allocate name for annotation member"; |
| 717 | return nullptr; |
| 718 | } |
| 719 | |
| 720 | Handle<mirror::Class> method_return = hs.NewHandle(annotation_method->ResolveReturnType()); |
| 721 | if (UNLIKELY(method_return == nullptr)) { |
| 722 | LOG(ERROR) << "Failed to resolve method return type for annotation member"; |
| 723 | return nullptr; |
| 724 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 725 | |
| 726 | DexFile::AnnotationValue annotation_value; |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 727 | if (!ProcessAnnotationValue<false>(klass, |
| 728 | annotation, |
| 729 | &annotation_value, |
| 730 | method_return, |
| 731 | DexFile::kAllObjects)) { |
Vladimir Marko | b1baa73 | 2022-11-11 12:43:11 +0000 | [diff] [blame] | 732 | // TODO: Logging the error breaks run-test 005-annotations. |
| 733 | // LOG(ERROR) << "Failed to process annotation value for annotation member"; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 734 | return nullptr; |
| 735 | } |
Vladimir Marko | b1baa73 | 2022-11-11 12:43:11 +0000 | [diff] [blame] | 736 | Handle<mirror::Object> value_object = hs.NewHandle(annotation_value.value_.GetL()); |
| 737 | |
| 738 | Handle<mirror::Method> method_object = hs.NewHandle((pointer_size == PointerSize::k64) |
| 739 | ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, annotation_method) |
| 740 | : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, annotation_method)); |
| 741 | if (UNLIKELY(method_object == nullptr)) { |
| 742 | LOG(ERROR) << "Failed to create method object for annotation member"; |
| 743 | return nullptr; |
| 744 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 745 | |
Vladimir Marko | b1baa73 | 2022-11-11 12:43:11 +0000 | [diff] [blame] | 746 | Handle<mirror::Object> new_member = |
Vladimir Marko | b9c73f8 | 2022-11-22 15:18:43 +0000 | [diff] [blame] | 747 | WellKnownClasses::libcore_reflect_AnnotationMember_init->NewObject<'L', 'L', 'L', 'L'>( |
| 748 | hs, self, string_name, value_object, method_return, method_object); |
Vladimir Marko | b1baa73 | 2022-11-11 12:43:11 +0000 | [diff] [blame] | 749 | if (new_member == nullptr) { |
Vladimir Marko | b9c73f8 | 2022-11-22 15:18:43 +0000 | [diff] [blame] | 750 | DCHECK(self->IsExceptionPending()); |
| 751 | LOG(ERROR) << "Failed to create annotation member"; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 752 | return nullptr; |
| 753 | } |
| 754 | |
| 755 | return new_member.Get(); |
| 756 | } |
| 757 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 758 | const AnnotationItem* GetAnnotationItemFromAnnotationSet(const ClassData& klass, |
| 759 | const AnnotationSetItem* annotation_set, |
| 760 | uint32_t visibility, |
| 761 | Handle<mirror::Class> annotation_class) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 762 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 763 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 764 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 765 | const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 766 | if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) { |
| 767 | continue; |
| 768 | } |
| 769 | const uint8_t* annotation = annotation_item->annotation_; |
| 770 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
Vladimir Marko | 370f57e | 2017-07-27 16:36:59 +0100 | [diff] [blame] | 771 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 772 | Thread* self = Thread::Current(); |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 773 | StackHandleScope<2> hs(self); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 774 | ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType( |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 775 | dex::TypeIndex(type_index), |
| 776 | hs.NewHandle(klass.GetDexCache()), |
| 777 | hs.NewHandle(klass.GetClassLoader())); |
| 778 | if (resolved_class == nullptr) { |
| 779 | std::string temp; |
| 780 | LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d", |
| 781 | klass.GetRealClass()->GetDescriptor(&temp), type_index); |
| 782 | CHECK(self->IsExceptionPending()); |
| 783 | self->ClearException(); |
| 784 | continue; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 785 | } |
| 786 | if (resolved_class == annotation_class.Get()) { |
| 787 | return annotation_item; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return nullptr; |
| 792 | } |
| 793 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 794 | ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(const ClassData& klass, |
| 795 | const AnnotationSetItem* annotation_set, |
| 796 | uint32_t visibility, |
| 797 | Handle<mirror::Class> annotation_class) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 798 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 799 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 800 | klass, annotation_set, visibility, annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 801 | if (annotation_item == nullptr) { |
| 802 | return nullptr; |
| 803 | } |
| 804 | const uint8_t* annotation = annotation_item->annotation_; |
| 805 | return ProcessEncodedAnnotation(klass, &annotation); |
| 806 | } |
| 807 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 808 | ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 809 | const AnnotationItem* annotation_item, |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 810 | const char* annotation_name, |
| 811 | Handle<mirror::Class> array_class, |
| 812 | uint32_t expected_type) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 813 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 814 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 815 | const uint8_t* annotation = |
| 816 | SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name); |
| 817 | if (annotation == nullptr) { |
| 818 | return nullptr; |
| 819 | } |
| 820 | DexFile::AnnotationValue annotation_value; |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 821 | bool result = Runtime::Current()->IsActiveTransaction() |
| 822 | ? ProcessAnnotationValue<true>(klass, |
| 823 | &annotation, |
| 824 | &annotation_value, |
| 825 | array_class, |
| 826 | DexFile::kAllObjects) |
| 827 | : ProcessAnnotationValue<false>(klass, |
| 828 | &annotation, |
| 829 | &annotation_value, |
| 830 | array_class, |
| 831 | DexFile::kAllObjects); |
| 832 | if (!result) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 833 | return nullptr; |
| 834 | } |
| 835 | if (annotation_value.type_ != expected_type) { |
| 836 | return nullptr; |
| 837 | } |
| 838 | return annotation_value.value_.GetL(); |
| 839 | } |
| 840 | |
Sorin Basca | a0191bc | 2022-06-15 16:53:48 +0100 | [diff] [blame] | 841 | template<typename T> |
| 842 | static inline ObjPtr<mirror::ObjectArray<T>> GetAnnotationArrayValue( |
| 843 | Handle<mirror::Class> klass, |
| 844 | const char* annotation_name, |
| 845 | const char* value_name) |
| 846 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 847 | ClassData data(klass); |
| 848 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
| 849 | if (annotation_set == nullptr) { |
| 850 | return nullptr; |
| 851 | } |
| 852 | const AnnotationItem* annotation_item = |
| 853 | SearchAnnotationSet(data.GetDexFile(), annotation_set, annotation_name, |
| 854 | DexFile::kDexVisibilitySystem); |
| 855 | if (annotation_item == nullptr) { |
| 856 | return nullptr; |
| 857 | } |
| 858 | StackHandleScope<1> hs(Thread::Current()); |
| 859 | Handle<mirror::Class> class_array_class = |
| 860 | hs.NewHandle(GetClassRoot<mirror::ObjectArray<T>>()); |
| 861 | DCHECK(class_array_class != nullptr); |
| 862 | ObjPtr<mirror::Object> obj = GetAnnotationValue(data, |
| 863 | annotation_item, |
| 864 | value_name, |
| 865 | class_array_class, |
| 866 | DexFile::kDexAnnotationArray); |
| 867 | if (obj == nullptr) { |
| 868 | return nullptr; |
| 869 | } |
| 870 | return obj->AsObjectArray<T>(); |
| 871 | } |
| 872 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 873 | static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue( |
| 874 | const ClassData& klass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 875 | const AnnotationSetItem* annotation_set) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 876 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 877 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 878 | StackHandleScope<1> hs(Thread::Current()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 879 | const AnnotationItem* annotation_item = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 880 | SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;", |
| 881 | DexFile::kDexVisibilitySystem); |
| 882 | if (annotation_item == nullptr) { |
| 883 | return nullptr; |
| 884 | } |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 885 | Handle<mirror::Class> string_array_class = |
| 886 | hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>()); |
| 887 | DCHECK(string_array_class != nullptr); |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 888 | ObjPtr<mirror::Object> obj = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 889 | GetAnnotationValue(klass, annotation_item, "value", string_array_class, |
| 890 | DexFile::kDexAnnotationArray); |
| 891 | if (obj == nullptr) { |
| 892 | return nullptr; |
| 893 | } |
| 894 | return obj->AsObjectArray<mirror::String>(); |
| 895 | } |
| 896 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 897 | ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(const ClassData& klass, |
| 898 | const AnnotationSetItem* annotation_set) |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 899 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 900 | const DexFile& dex_file = klass.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 901 | const AnnotationItem* annotation_item = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 902 | SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;", |
| 903 | DexFile::kDexVisibilitySystem); |
| 904 | if (annotation_item == nullptr) { |
| 905 | return nullptr; |
| 906 | } |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 907 | StackHandleScope<1> hs(Thread::Current()); |
| 908 | Handle<mirror::Class> class_array_class = |
| 909 | hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>()); |
| 910 | DCHECK(class_array_class != nullptr); |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 911 | ObjPtr<mirror::Object> obj = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 912 | GetAnnotationValue(klass, annotation_item, "value", class_array_class, |
| 913 | DexFile::kDexAnnotationArray); |
| 914 | if (obj == nullptr) { |
| 915 | return nullptr; |
| 916 | } |
| 917 | return obj->AsObjectArray<mirror::Class>(); |
| 918 | } |
| 919 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 920 | ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 921 | const ClassData& klass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 922 | const AnnotationSetItem* annotation_set, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 923 | uint32_t visibility) |
| 924 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 925 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 926 | Thread* self = Thread::Current(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 927 | StackHandleScope<2> hs(self); |
| 928 | Handle<mirror::Class> annotation_array_class(hs.NewHandle( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 929 | WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array))); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 930 | if (annotation_set == nullptr) { |
| 931 | return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0); |
| 932 | } |
| 933 | |
| 934 | uint32_t size = annotation_set->size_; |
| 935 | Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle( |
| 936 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 937 | if (result == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 938 | return nullptr; |
| 939 | } |
| 940 | |
| 941 | uint32_t dest_index = 0; |
| 942 | for (uint32_t i = 0; i < size; ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 943 | const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 944 | // Note that we do not use IsVisibilityCompatible here because older code |
| 945 | // was correct for this case. |
| 946 | if (annotation_item->visibility_ != visibility) { |
| 947 | continue; |
| 948 | } |
| 949 | const uint8_t* annotation = annotation_item->annotation_; |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 950 | ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 951 | if (annotation_obj != nullptr) { |
| 952 | result->SetWithoutChecks<false>(dest_index, annotation_obj); |
| 953 | ++dest_index; |
| 954 | } else if (self->IsExceptionPending()) { |
| 955 | return nullptr; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (dest_index == size) { |
| 960 | return result.Get(); |
| 961 | } |
| 962 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 963 | ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 964 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index); |
| 965 | if (trimmed_result == nullptr) { |
| 966 | return nullptr; |
| 967 | } |
| 968 | |
| 969 | for (uint32_t i = 0; i < dest_index; ++i) { |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 970 | ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 971 | trimmed_result->SetWithoutChecks<false>(i, obj); |
| 972 | } |
| 973 | |
| 974 | return trimmed_result; |
| 975 | } |
| 976 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 977 | ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 978 | const ClassData& klass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 979 | const AnnotationSetRefList* set_ref_list, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 980 | uint32_t size) |
| 981 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 982 | const DexFile& dex_file = klass.GetDexFile(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 983 | Thread* self = Thread::Current(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 984 | StackHandleScope<1> hs(self); |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 985 | ObjPtr<mirror::Class> annotation_array_class = |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 986 | WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array); |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 987 | ObjPtr<mirror::Class> annotation_array_array_class = |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 988 | Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 989 | if (annotation_array_array_class == nullptr) { |
| 990 | return nullptr; |
| 991 | } |
| 992 | Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle( |
| 993 | mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 994 | if (annotation_array_array == nullptr) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 995 | LOG(ERROR) << "Annotation set ref array allocation failed"; |
| 996 | return nullptr; |
| 997 | } |
| 998 | for (uint32_t index = 0; index < size; ++index) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 999 | const AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index]; |
| 1000 | const AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item); |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1001 | ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass, |
| 1002 | set_item, |
| 1003 | DexFile::kDexVisibilityRuntime); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1004 | if (annotation_set == nullptr) { |
| 1005 | return nullptr; |
| 1006 | } |
| 1007 | annotation_array_array->SetWithoutChecks<false>(index, annotation_set); |
| 1008 | } |
| 1009 | return annotation_array_array.Get(); |
| 1010 | } |
| 1011 | } // namespace |
| 1012 | |
| 1013 | namespace annotations { |
| 1014 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1015 | ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field, |
| 1016 | Handle<mirror::Class> annotation_class) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1017 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1018 | if (annotation_set == nullptr) { |
| 1019 | return nullptr; |
| 1020 | } |
| 1021 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1022 | const ClassData field_class(hs, field); |
| 1023 | return GetAnnotationObjectFromAnnotationSet(field_class, |
| 1024 | annotation_set, |
| 1025 | DexFile::kDexVisibilityRuntime, |
| 1026 | annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1029 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1030 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1031 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1032 | const ClassData field_class(hs, field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1033 | return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime); |
| 1034 | } |
| 1035 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1036 | ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1037 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1038 | if (annotation_set == nullptr) { |
| 1039 | return nullptr; |
| 1040 | } |
| 1041 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1042 | const ClassData field_class(hs, field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1043 | return GetSignatureValue(field_class, annotation_set); |
| 1044 | } |
| 1045 | |
| 1046 | bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1047 | const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1048 | if (annotation_set == nullptr) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | StackHandleScope<1> hs(Thread::Current()); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1052 | const ClassData field_class(hs, field); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1053 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1054 | field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); |
| 1055 | return annotation_item != nullptr; |
| 1056 | } |
| 1057 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1058 | ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1059 | const ClassData klass(method); |
| 1060 | const DexFile* dex_file = &klass.GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1061 | const AnnotationsDirectoryItem* annotations_dir = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1062 | dex_file->GetAnnotationsDirectory(*klass.GetClassDef()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1063 | if (annotations_dir == nullptr) { |
| 1064 | return nullptr; |
| 1065 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1066 | const AnnotationSetItem* annotation_set = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1067 | dex_file->GetClassAnnotationSet(annotations_dir); |
| 1068 | if (annotation_set == nullptr) { |
| 1069 | return nullptr; |
| 1070 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1071 | const AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1072 | "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem); |
| 1073 | if (annotation_item == nullptr) { |
| 1074 | return nullptr; |
| 1075 | } |
| 1076 | const uint8_t* annotation = |
| 1077 | SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value"); |
| 1078 | if (annotation == nullptr) { |
| 1079 | return nullptr; |
| 1080 | } |
| 1081 | uint8_t header_byte = *(annotation++); |
| 1082 | if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) { |
| 1083 | return nullptr; |
| 1084 | } |
| 1085 | annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName()); |
| 1086 | if (annotation == nullptr) { |
| 1087 | return nullptr; |
| 1088 | } |
| 1089 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1090 | StackHandleScope<1> hs(Thread::Current()); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 1091 | Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType())); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1092 | if (!ProcessAnnotationValue<false>(klass, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1093 | &annotation, |
| 1094 | &annotation_value, |
| 1095 | return_type, |
| 1096 | DexFile::kAllObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1097 | return nullptr; |
| 1098 | } |
| 1099 | return annotation_value.value_.GetL(); |
| 1100 | } |
| 1101 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1102 | ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method, |
| 1103 | Handle<mirror::Class> annotation_class) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1104 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1105 | if (annotation_set == nullptr) { |
| 1106 | return nullptr; |
| 1107 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1108 | return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1109 | DexFile::kDexVisibilityRuntime, annotation_class); |
| 1110 | } |
| 1111 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1112 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1113 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1114 | return ProcessAnnotationSet(ClassData(method), |
| 1115 | annotation_set, |
| 1116 | DexFile::kDexVisibilityRuntime); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1119 | ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1120 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1121 | if (annotation_set == nullptr) { |
| 1122 | return nullptr; |
| 1123 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1124 | return GetThrowsValue(ClassData(method), annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1127 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1128 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1129 | const ParameterAnnotationsItem* parameter_annotations = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1130 | FindAnnotationsItemForMethod(method); |
| 1131 | if (parameter_annotations == nullptr) { |
| 1132 | return nullptr; |
| 1133 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1134 | const AnnotationSetRefList* set_ref_list = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1135 | dex_file->GetParameterAnnotationSetRefList(parameter_annotations); |
| 1136 | if (set_ref_list == nullptr) { |
| 1137 | return nullptr; |
| 1138 | } |
| 1139 | uint32_t size = set_ref_list->size_; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1140 | return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 1143 | uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) { |
| 1144 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1145 | const ParameterAnnotationsItem* parameter_annotations = |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 1146 | FindAnnotationsItemForMethod(method); |
| 1147 | if (parameter_annotations == nullptr) { |
| 1148 | return 0u; |
| 1149 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1150 | const AnnotationSetRefList* set_ref_list = |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 1151 | dex_file->GetParameterAnnotationSetRefList(parameter_annotations); |
| 1152 | if (set_ref_list == nullptr) { |
| 1153 | return 0u; |
| 1154 | } |
| 1155 | return set_ref_list->size_; |
| 1156 | } |
| 1157 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1158 | ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method, |
| 1159 | uint32_t parameter_idx, |
| 1160 | Handle<mirror::Class> annotation_class) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1161 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1162 | const ParameterAnnotationsItem* parameter_annotations = FindAnnotationsItemForMethod(method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1163 | if (parameter_annotations == nullptr) { |
| 1164 | return nullptr; |
| 1165 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1166 | const AnnotationSetRefList* set_ref_list = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1167 | dex_file->GetParameterAnnotationSetRefList(parameter_annotations); |
| 1168 | if (set_ref_list == nullptr) { |
| 1169 | return nullptr; |
| 1170 | } |
| 1171 | if (parameter_idx >= set_ref_list->size_) { |
| 1172 | return nullptr; |
| 1173 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1174 | const AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx]; |
| 1175 | const AnnotationSetItem* annotation_set = |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1176 | dex_file->GetSetRefItemItem(annotation_set_ref); |
Orion Hodson | 58143d2 | 2018-02-20 08:44:20 +0000 | [diff] [blame] | 1177 | if (annotation_set == nullptr) { |
| 1178 | return nullptr; |
| 1179 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1180 | return GetAnnotationObjectFromAnnotationSet(ClassData(method), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1181 | annotation_set, |
| 1182 | DexFile::kDexVisibilityRuntime, |
| 1183 | annotation_class); |
| 1184 | } |
| 1185 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1186 | bool GetParametersMetadataForMethod( |
| 1187 | ArtMethod* method, |
| 1188 | /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names, |
| 1189 | /*out*/ MutableHandle<mirror::IntArray>* access_flags) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1190 | const AnnotationSetItem* annotation_set = |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1191 | FindAnnotationSetForMethod(method); |
| 1192 | if (annotation_set == nullptr) { |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
| 1196 | const DexFile* dex_file = method->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1197 | const AnnotationItem* annotation_item = |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1198 | SearchAnnotationSet(*dex_file, |
| 1199 | annotation_set, |
| 1200 | "Ldalvik/annotation/MethodParameters;", |
| 1201 | DexFile::kDexVisibilitySystem); |
| 1202 | if (annotation_item == nullptr) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1206 | StackHandleScope<4> hs(Thread::Current()); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1207 | |
| 1208 | // Extract the parameters' names String[]. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1209 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1210 | Handle<mirror::Class> string_array_class = |
| 1211 | hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker)); |
| 1212 | DCHECK(string_array_class != nullptr); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1213 | |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1214 | ClassData data(method); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1215 | Handle<mirror::Object> names_obj = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1216 | hs.NewHandle(GetAnnotationValue(data, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1217 | annotation_item, |
| 1218 | "names", |
| 1219 | string_array_class, |
| 1220 | DexFile::kDexAnnotationArray)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1221 | if (names_obj == nullptr) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | // Extract the parameters' access flags int[]. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1226 | Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker))); |
| 1227 | DCHECK(int_array_class != nullptr); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1228 | Handle<mirror::Object> access_flags_obj = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1229 | hs.NewHandle(GetAnnotationValue(data, |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1230 | annotation_item, |
| 1231 | "accessFlags", |
| 1232 | int_array_class, |
| 1233 | DexFile::kDexAnnotationArray)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1234 | if (access_flags_obj == nullptr) { |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1235 | return false; |
| 1236 | } |
| 1237 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1238 | names->Assign(names_obj->AsObjectArray<mirror::String>()); |
| 1239 | access_flags->Assign(access_flags_obj->AsIntArray()); |
Neil Fuller | 79a21e7 | 2016-09-09 14:24:51 +0100 | [diff] [blame] | 1240 | return true; |
| 1241 | } |
| 1242 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1243 | ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1244 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1245 | if (annotation_set == nullptr) { |
| 1246 | return nullptr; |
| 1247 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1248 | return GetSignatureValue(ClassData(method), annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Roland Levillain | 35e42f0 | 2017-06-26 18:14:39 +0100 | [diff] [blame] | 1251 | bool IsMethodAnnotationPresent(ArtMethod* method, |
| 1252 | Handle<mirror::Class> annotation_class, |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1253 | uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1254 | const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1255 | if (annotation_set == nullptr) { |
| 1256 | return false; |
| 1257 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1258 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1259 | ClassData(method), annotation_set, visibility, annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1260 | return annotation_item != nullptr; |
| 1261 | } |
| 1262 | |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1263 | static void DCheckNativeAnnotation(const char* descriptor, jclass cls) { |
| 1264 | if (kIsDebugBuild) { |
| 1265 | ScopedObjectAccess soa(Thread::Current()); |
| 1266 | ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls); |
| 1267 | ClassLinker* linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1268 | // WellKnownClasses may not be initialized yet, so `klass` may be null. |
| 1269 | if (klass != nullptr) { |
| 1270 | // Lookup using the boot class path loader should yield the annotation class. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 1271 | CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr)); |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1272 | } |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | // Check whether a method from the `dex_file` with the given `annotation_set` |
| 1277 | // is annotated with `annotation_descriptor` with build visibility. |
| 1278 | static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1279 | const AnnotationSetItem& annotation_set, |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1280 | const char* annotation_descriptor, |
| 1281 | jclass annotation_class) { |
| 1282 | for (uint32_t i = 0; i < annotation_set.size_; ++i) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1283 | const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i); |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1284 | if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) { |
| 1285 | continue; |
| 1286 | } |
| 1287 | const uint8_t* annotation = annotation_item->annotation_; |
| 1288 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
| 1289 | const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index)); |
| 1290 | if (strcmp(descriptor, annotation_descriptor) == 0) { |
| 1291 | DCheckNativeAnnotation(descriptor, annotation_class); |
| 1292 | return true; |
| 1293 | } |
| 1294 | } |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1298 | uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1299 | const dex::ClassDef& class_def, |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1300 | uint32_t method_index) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1301 | const dex::AnnotationSetItem* annotation_set = |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1302 | FindAnnotationSetForMethod(dex_file, class_def, method_index); |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1303 | if (annotation_set == nullptr) { |
| 1304 | return 0u; |
| 1305 | } |
| 1306 | uint32_t access_flags = 0u; |
| 1307 | if (IsMethodBuildAnnotationPresent( |
| 1308 | dex_file, |
| 1309 | *annotation_set, |
| 1310 | "Ldalvik/annotation/optimization/FastNative;", |
| 1311 | WellKnownClasses::dalvik_annotation_optimization_FastNative)) { |
| 1312 | access_flags |= kAccFastNative; |
| 1313 | } |
| 1314 | if (IsMethodBuildAnnotationPresent( |
| 1315 | dex_file, |
| 1316 | *annotation_set, |
| 1317 | "Ldalvik/annotation/optimization/CriticalNative;", |
| 1318 | WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) { |
| 1319 | access_flags |= kAccCriticalNative; |
| 1320 | } |
| 1321 | CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative); |
| 1322 | return access_flags; |
Vladimir Marko | 0db16e0 | 2017-11-08 14:32:33 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Kevin Jeon | 5573756 | 2021-11-19 20:39:30 +0000 | [diff] [blame] | 1325 | bool MethodIsNeverCompile(const DexFile& dex_file, |
| 1326 | const dex::ClassDef& class_def, |
| 1327 | uint32_t method_index) { |
| 1328 | const dex::AnnotationSetItem* annotation_set = |
| 1329 | FindAnnotationSetForMethod(dex_file, class_def, method_index); |
| 1330 | if (annotation_set == nullptr) { |
| 1331 | return false; |
| 1332 | } |
| 1333 | return IsMethodBuildAnnotationPresent( |
| 1334 | dex_file, |
| 1335 | *annotation_set, |
| 1336 | "Ldalvik/annotation/optimization/NeverCompile;", |
| 1337 | WellKnownClasses::dalvik_annotation_optimization_NeverCompile); |
| 1338 | } |
| 1339 | |
Santiago Aboy Solanes | 2979d53 | 2022-09-29 18:03:13 +0100 | [diff] [blame] | 1340 | bool MethodIsNeverInline(const DexFile& dex_file, |
| 1341 | const dex::ClassDef& class_def, |
| 1342 | uint32_t method_index) { |
| 1343 | const dex::AnnotationSetItem* annotation_set = |
| 1344 | FindAnnotationSetForMethod(dex_file, class_def, method_index); |
| 1345 | if (annotation_set == nullptr) { |
| 1346 | return false; |
| 1347 | } |
| 1348 | return IsMethodBuildAnnotationPresent( |
| 1349 | dex_file, |
| 1350 | *annotation_set, |
| 1351 | "Ldalvik/annotation/optimization/NeverInline;", |
| 1352 | WellKnownClasses::dalvik_annotation_optimization_NeverInline); |
| 1353 | } |
Kevin Jeon | 5573756 | 2021-11-19 20:39:30 +0000 | [diff] [blame] | 1354 | |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1355 | bool FieldIsReachabilitySensitive(const DexFile& dex_file, |
| 1356 | const dex::ClassDef& class_def, |
| 1357 | uint32_t field_index) |
| 1358 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1359 | const AnnotationSetItem* annotation_set = |
| 1360 | FindAnnotationSetForField(dex_file, class_def, field_index); |
| 1361 | if (annotation_set == nullptr) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set, |
| 1365 | "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime); |
| 1366 | // TODO: We're missing the equivalent of DCheckNativeAnnotation (not a DCHECK). Does it matter? |
| 1367 | return annotation_item != nullptr; |
| 1368 | } |
| 1369 | |
| 1370 | bool MethodIsReachabilitySensitive(const DexFile& dex_file, |
| 1371 | const dex::ClassDef& class_def, |
| 1372 | uint32_t method_index) |
| 1373 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1374 | const AnnotationSetItem* annotation_set = |
| 1375 | FindAnnotationSetForMethod(dex_file, class_def, method_index); |
| 1376 | if (annotation_set == nullptr) { |
| 1377 | return false; |
| 1378 | } |
| 1379 | const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set, |
| 1380 | "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime); |
| 1381 | return annotation_item != nullptr; |
| 1382 | } |
| 1383 | |
| 1384 | static bool MethodIsReachabilitySensitive(const DexFile& dex_file, |
| 1385 | uint32_t method_index) |
| 1386 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1387 | DCHECK(method_index < dex_file.NumMethodIds()); |
| 1388 | const dex::MethodId& method_id = dex_file.GetMethodId(method_index); |
| 1389 | dex::TypeIndex class_index = method_id.class_idx_; |
| 1390 | const dex::ClassDef * class_def = dex_file.FindClassDef(class_index); |
| 1391 | return class_def != nullptr |
| 1392 | && MethodIsReachabilitySensitive(dex_file, *class_def, method_index); |
| 1393 | } |
| 1394 | |
| 1395 | bool MethodContainsRSensitiveAccess(const DexFile& dex_file, |
| 1396 | const dex::ClassDef& class_def, |
| 1397 | uint32_t method_index) |
| 1398 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1399 | // TODO: This is too slow to run very regularly. Currently this is only invoked in the |
| 1400 | // presence of @DeadReferenceSafe, which will be rare. In the long run, we need to quickly |
| 1401 | // check once whether a class has any @ReachabilitySensitive annotations. If not, we can |
| 1402 | // immediately return false here for any method in that class. |
| 1403 | uint32_t code_item_offset = dex_file.FindCodeItemOffset(class_def, method_index); |
| 1404 | const dex::CodeItem* code_item = dex_file.GetCodeItem(code_item_offset); |
| 1405 | CodeItemInstructionAccessor accessor(dex_file, code_item); |
| 1406 | if (!accessor.HasCodeItem()) { |
| 1407 | return false; |
| 1408 | } |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1409 | for (DexInstructionIterator iter = accessor.begin(); iter != accessor.end(); ++iter) { |
| 1410 | switch (iter->Opcode()) { |
| 1411 | case Instruction::IGET: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1412 | case Instruction::IGET_WIDE: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1413 | case Instruction::IGET_OBJECT: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1414 | case Instruction::IGET_BOOLEAN: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1415 | case Instruction::IGET_BYTE: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1416 | case Instruction::IGET_CHAR: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1417 | case Instruction::IGET_SHORT: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1418 | case Instruction::IPUT: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1419 | case Instruction::IPUT_WIDE: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1420 | case Instruction::IPUT_OBJECT: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1421 | case Instruction::IPUT_BOOLEAN: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1422 | case Instruction::IPUT_BYTE: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1423 | case Instruction::IPUT_CHAR: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1424 | case Instruction::IPUT_SHORT: |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1425 | { |
Nicolas Geoffray | 4924ea9 | 2021-03-23 08:25:31 +0000 | [diff] [blame] | 1426 | uint32_t field_index = iter->VRegC_22c(); |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1427 | DCHECK(field_index < dex_file.NumFieldIds()); |
| 1428 | // We only guarantee to pay attention to the annotation if it's in the same class, |
| 1429 | // or a containing class, but it's OK to do so in other cases. |
| 1430 | const dex::FieldId& field_id = dex_file.GetFieldId(field_index); |
| 1431 | dex::TypeIndex class_index = field_id.class_idx_; |
| 1432 | const dex::ClassDef * field_class_def = dex_file.FindClassDef(class_index); |
| 1433 | // We do not handle the case in which the field is declared in a superclass, and |
| 1434 | // don't claim to do so. The annotated field should normally be private. |
| 1435 | if (field_class_def != nullptr |
| 1436 | && FieldIsReachabilitySensitive(dex_file, *field_class_def, field_index)) { |
| 1437 | return true; |
| 1438 | } |
| 1439 | } |
| 1440 | break; |
| 1441 | case Instruction::INVOKE_SUPER: |
| 1442 | // Cannot call method in same class. TODO: Try an explicit superclass lookup for |
| 1443 | // better "best effort"? |
| 1444 | break; |
| 1445 | case Instruction::INVOKE_INTERFACE: |
| 1446 | // We handle an interface call just like a virtual call. We will find annotations |
| 1447 | // on interface methods/fields visible to us, but not of the annotation is in a |
| 1448 | // super-interface. Again, we could just ignore it. |
| 1449 | case Instruction::INVOKE_VIRTUAL: |
| 1450 | case Instruction::INVOKE_DIRECT: |
| 1451 | { |
| 1452 | uint32_t called_method_index = iter->VRegB_35c(); |
| 1453 | if (MethodIsReachabilitySensitive(dex_file, called_method_index)) { |
| 1454 | return true; |
| 1455 | } |
| 1456 | } |
| 1457 | break; |
| 1458 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 1459 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 1460 | case Instruction::INVOKE_DIRECT_RANGE: |
| 1461 | { |
| 1462 | uint32_t called_method_index = iter->VRegB_3rc(); |
| 1463 | if (MethodIsReachabilitySensitive(dex_file, called_method_index)) { |
| 1464 | return true; |
| 1465 | } |
| 1466 | } |
| 1467 | break; |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1468 | // We explicitly do not handle indirect ReachabilitySensitive accesses through VarHandles, |
| 1469 | // etc. Thus we ignore INVOKE_CUSTOM / INVOKE_CUSTOM_RANGE / INVOKE_POLYMORPHIC / |
| 1470 | // INVOKE_POLYMORPHIC_RANGE. |
| 1471 | default: |
| 1472 | // There is no way to add an annotation to array elements, and so far we've encountered no |
| 1473 | // need for that, so we ignore AGET and APUT. |
| 1474 | // It's impractical or impossible to garbage collect a class while one of its methods is |
| 1475 | // on the call stack. We allow ReachabilitySensitive annotations on static methods and |
| 1476 | // fields, but they can be safely ignored. |
| 1477 | break; |
| 1478 | } |
Hans Boehm | 206348c | 2018-12-05 11:11:33 -0800 | [diff] [blame] | 1479 | } |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file, |
| 1484 | const dex::ClassDef& class_def) |
| 1485 | // TODO: This should check outer classes as well. |
| 1486 | // It's conservatively correct not to do so. |
| 1487 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1488 | const AnnotationsDirectoryItem* annotations_dir = |
| 1489 | dex_file.GetAnnotationsDirectory(class_def); |
| 1490 | if (annotations_dir == nullptr) { |
| 1491 | return false; |
| 1492 | } |
| 1493 | const AnnotationSetItem* annotation_set = dex_file.GetClassAnnotationSet(annotations_dir); |
| 1494 | if (annotation_set == nullptr) { |
| 1495 | return false; |
| 1496 | } |
| 1497 | const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set, |
| 1498 | "Ldalvik/annotation/optimization/DeadReferenceSafe;", DexFile::kDexVisibilityRuntime); |
| 1499 | return annotation_item != nullptr; |
| 1500 | } |
| 1501 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1502 | ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass, |
| 1503 | Handle<mirror::Class> annotation_class) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1504 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1505 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1506 | if (annotation_set == nullptr) { |
| 1507 | return nullptr; |
| 1508 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1509 | return GetAnnotationObjectFromAnnotationSet(data, |
| 1510 | annotation_set, |
| 1511 | DexFile::kDexVisibilityRuntime, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1512 | annotation_class); |
| 1513 | } |
| 1514 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1515 | ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1516 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1517 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1518 | return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1521 | ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) { |
Sorin Basca | a0191bc | 2022-06-15 16:53:48 +0100 | [diff] [blame] | 1522 | return GetAnnotationArrayValue<mirror::Class>(klass, |
| 1523 | "Ldalvik/annotation/MemberClasses;", |
| 1524 | "value"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1527 | ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1528 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1529 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1530 | if (annotation_set == nullptr) { |
| 1531 | return nullptr; |
| 1532 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1533 | const AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1534 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1535 | DexFile::kDexVisibilitySystem); |
| 1536 | if (annotation_item == nullptr) { |
| 1537 | return nullptr; |
| 1538 | } |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1539 | ObjPtr<mirror::Object> obj = GetAnnotationValue(data, |
| 1540 | annotation_item, |
| 1541 | "value", |
| 1542 | ScopedNullHandle<mirror::Class>(), |
| 1543 | DexFile::kDexAnnotationType); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1544 | if (obj == nullptr) { |
| 1545 | return nullptr; |
| 1546 | } |
Nicolas Geoffray | f740be5e | 2021-09-16 14:07:37 +0100 | [diff] [blame] | 1547 | if (!obj->IsClass()) { |
| 1548 | // TypeNotPresentException, throw the NoClassDefFoundError. |
| 1549 | Thread::Current()->SetException(obj->AsThrowable()->GetCause()); |
| 1550 | return nullptr; |
| 1551 | } |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1552 | return obj->AsClass(); |
| 1553 | } |
| 1554 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1555 | ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) { |
| 1556 | ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass); |
Nicolas Geoffray | f740be5e | 2021-09-16 14:07:37 +0100 | [diff] [blame] | 1557 | if (declaring_class != nullptr || Thread::Current()->IsExceptionPending()) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1558 | return declaring_class; |
| 1559 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1560 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1561 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1562 | if (annotation_set == nullptr) { |
| 1563 | return nullptr; |
| 1564 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1565 | const AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1566 | SearchAnnotationSet(data.GetDexFile(), |
| 1567 | annotation_set, |
| 1568 | "Ldalvik/annotation/EnclosingMethod;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1569 | DexFile::kDexVisibilitySystem); |
| 1570 | if (annotation_item == nullptr) { |
| 1571 | return nullptr; |
| 1572 | } |
| 1573 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1574 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1575 | if (annotation == nullptr) { |
| 1576 | return nullptr; |
| 1577 | } |
| 1578 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1579 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1580 | &annotation, |
| 1581 | &annotation_value, |
| 1582 | ScopedNullHandle<mirror::Class>(), |
| 1583 | DexFile::kAllRaw)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1584 | return nullptr; |
| 1585 | } |
| 1586 | if (annotation_value.type_ != DexFile::kDexAnnotationMethod) { |
| 1587 | return nullptr; |
| 1588 | } |
| 1589 | StackHandleScope<2> hs(Thread::Current()); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1590 | ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1591 | annotation_value.value_.GetI(), |
| 1592 | hs.NewHandle(data.GetDexCache()), |
| 1593 | hs.NewHandle(data.GetClassLoader())); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1594 | if (method == nullptr) { |
| 1595 | return nullptr; |
| 1596 | } |
| 1597 | return method->GetDeclaringClass(); |
| 1598 | } |
| 1599 | |
Vladimir Marko | 2d3065e | 2018-05-22 13:56:09 +0100 | [diff] [blame] | 1600 | ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1601 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1602 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1603 | if (annotation_set == nullptr) { |
| 1604 | return nullptr; |
| 1605 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1606 | const AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1607 | SearchAnnotationSet(data.GetDexFile(), |
| 1608 | annotation_set, |
| 1609 | "Ldalvik/annotation/EnclosingMethod;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1610 | DexFile::kDexVisibilitySystem); |
| 1611 | if (annotation_item == nullptr) { |
| 1612 | return nullptr; |
| 1613 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1614 | return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(), |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1615 | DexFile::kDexAnnotationMethod); |
| 1616 | } |
| 1617 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1618 | bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1619 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1620 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1621 | if (annotation_set == nullptr) { |
| 1622 | return false; |
| 1623 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1624 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1625 | data.GetDexFile(), |
| 1626 | annotation_set, |
| 1627 | "Ldalvik/annotation/InnerClass;", |
| 1628 | DexFile::kDexVisibilitySystem); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1629 | if (annotation_item == nullptr) { |
| 1630 | return false; |
| 1631 | } |
| 1632 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1633 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1634 | if (annotation == nullptr) { |
| 1635 | return false; |
| 1636 | } |
| 1637 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1638 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1639 | &annotation, |
| 1640 | &annotation_value, |
| 1641 | ScopedNullHandle<mirror::Class>(), |
| 1642 | DexFile::kAllObjects)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1643 | return false; |
| 1644 | } |
| 1645 | if (annotation_value.type_ != DexFile::kDexAnnotationNull && |
| 1646 | annotation_value.type_ != DexFile::kDexAnnotationString) { |
| 1647 | return false; |
| 1648 | } |
| 1649 | *name = down_cast<mirror::String*>(annotation_value.value_.GetL()); |
| 1650 | return true; |
| 1651 | } |
| 1652 | |
| 1653 | bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1654 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1655 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1656 | if (annotation_set == nullptr) { |
| 1657 | return false; |
| 1658 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1659 | const AnnotationItem* annotation_item = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1660 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;", |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1661 | DexFile::kDexVisibilitySystem); |
| 1662 | if (annotation_item == nullptr) { |
| 1663 | return false; |
| 1664 | } |
| 1665 | const uint8_t* annotation = |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1666 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags"); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1667 | if (annotation == nullptr) { |
| 1668 | return false; |
| 1669 | } |
| 1670 | DexFile::AnnotationValue annotation_value; |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1671 | if (!ProcessAnnotationValue<false>(data, |
Andreas Gampe | 9486a16 | 2017-02-16 15:17:47 -0800 | [diff] [blame] | 1672 | &annotation, |
| 1673 | &annotation_value, |
| 1674 | ScopedNullHandle<mirror::Class>(), |
| 1675 | DexFile::kAllRaw)) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1676 | return false; |
| 1677 | } |
| 1678 | if (annotation_value.type_ != DexFile::kDexAnnotationInt) { |
| 1679 | return false; |
| 1680 | } |
| 1681 | *flags = annotation_value.value_.GetI(); |
| 1682 | return true; |
| 1683 | } |
| 1684 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1685 | ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass( |
| 1686 | Handle<mirror::Class> klass) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1687 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1688 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1689 | if (annotation_set == nullptr) { |
| 1690 | return nullptr; |
| 1691 | } |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1692 | return GetSignatureValue(data, annotation_set); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1695 | const char* GetSourceDebugExtension(Handle<mirror::Class> klass) { |
Orion Hodson | cf7127b | 2017-05-09 09:51:35 +0100 | [diff] [blame] | 1696 | // Before instantiating ClassData, check that klass has a DexCache |
| 1697 | // assigned. The ClassData constructor indirectly dereferences it |
| 1698 | // when calling klass->GetDexFile(). |
| 1699 | if (klass->GetDexCache() == nullptr) { |
| 1700 | DCHECK(klass->IsPrimitive() || klass->IsArrayClass()); |
| 1701 | return nullptr; |
| 1702 | } |
| 1703 | |
Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1704 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1705 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1706 | if (annotation_set == nullptr) { |
| 1707 | return nullptr; |
| 1708 | } |
Orion Hodson | cf7127b | 2017-05-09 09:51:35 +0100 | [diff] [blame] | 1709 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1710 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1711 | data.GetDexFile(), |
| 1712 | annotation_set, |
| 1713 | "Ldalvik/annotation/SourceDebugExtension;", |
| 1714 | DexFile::kDexVisibilitySystem); |
| 1715 | if (annotation_item == nullptr) { |
| 1716 | return nullptr; |
| 1717 | } |
Orion Hodson | cf7127b | 2017-05-09 09:51:35 +0100 | [diff] [blame] | 1718 | |
Orion Hodson | 77d8a1c | 2017-04-24 14:53:19 +0100 | [diff] [blame] | 1719 | const uint8_t* annotation = |
| 1720 | SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value"); |
| 1721 | if (annotation == nullptr) { |
| 1722 | return nullptr; |
| 1723 | } |
| 1724 | DexFile::AnnotationValue annotation_value; |
| 1725 | if (!ProcessAnnotationValue<false>(data, |
| 1726 | &annotation, |
| 1727 | &annotation_value, |
| 1728 | ScopedNullHandle<mirror::Class>(), |
| 1729 | DexFile::kAllRaw)) { |
| 1730 | return nullptr; |
| 1731 | } |
| 1732 | if (annotation_value.type_ != DexFile::kDexAnnotationString) { |
| 1733 | return nullptr; |
| 1734 | } |
| 1735 | dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI())); |
| 1736 | return data.GetDexFile().StringDataByIdx(index); |
| 1737 | } |
| 1738 | |
Sorin Basca | 08b3f87 | 2022-05-31 17:04:55 +0100 | [diff] [blame] | 1739 | ObjPtr<mirror::Class> GetNestHost(Handle<mirror::Class> klass) { |
| 1740 | ClassData data(klass); |
| 1741 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
| 1742 | if (annotation_set == nullptr) { |
| 1743 | return nullptr; |
| 1744 | } |
| 1745 | const AnnotationItem* annotation_item = |
| 1746 | SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/NestHost;", |
| 1747 | DexFile::kDexVisibilitySystem); |
| 1748 | if (annotation_item == nullptr) { |
| 1749 | return nullptr; |
| 1750 | } |
| 1751 | ObjPtr<mirror::Object> obj = GetAnnotationValue(data, |
| 1752 | annotation_item, |
| 1753 | "host", |
| 1754 | ScopedNullHandle<mirror::Class>(), |
| 1755 | DexFile::kDexAnnotationType); |
| 1756 | if (obj == nullptr) { |
| 1757 | return nullptr; |
| 1758 | } |
| 1759 | if (!obj->IsClass()) { |
| 1760 | // TypeNotPresentException, throw the NoClassDefFoundError. |
| 1761 | Thread::Current()->SetException(obj->AsThrowable()->GetCause()); |
| 1762 | return nullptr; |
| 1763 | } |
| 1764 | return obj->AsClass(); |
| 1765 | } |
| 1766 | |
| 1767 | ObjPtr<mirror::ObjectArray<mirror::Class>> GetNestMembers(Handle<mirror::Class> klass) { |
Sorin Basca | a0191bc | 2022-06-15 16:53:48 +0100 | [diff] [blame] | 1768 | return GetAnnotationArrayValue<mirror::Class>(klass, |
| 1769 | "Ldalvik/annotation/NestMembers;", |
| 1770 | "classes"); |
| 1771 | } |
| 1772 | |
| 1773 | ObjPtr<mirror::ObjectArray<mirror::Class>> GetPermittedSubclasses(Handle<mirror::Class> klass) { |
| 1774 | return GetAnnotationArrayValue<mirror::Class>(klass, |
| 1775 | "Ldalvik/annotation/PermittedSubclasses;", |
| 1776 | "value"); |
Sorin Basca | 08b3f87 | 2022-05-31 17:04:55 +0100 | [diff] [blame] | 1777 | } |
| 1778 | |
Victor Chang | d8e3d11 | 2023-03-21 18:40:45 +0000 | [diff] [blame] | 1779 | ObjPtr<mirror::Object> getRecordAnnotationElement(Handle<mirror::Class> klass, |
| 1780 | Handle<mirror::Class> array_class, |
| 1781 | const char* element_name) { |
| 1782 | ClassData data(klass); |
| 1783 | const DexFile& dex_file = klass->GetDexFile(); |
| 1784 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
| 1785 | if (annotation_set == nullptr) { |
| 1786 | return nullptr; |
| 1787 | } |
| 1788 | const AnnotationItem* annotation_item = SearchAnnotationSet( |
| 1789 | dex_file, annotation_set, "Ldalvik/annotation/Record;", DexFile::kDexVisibilitySystem); |
| 1790 | if (annotation_item == nullptr) { |
| 1791 | return nullptr; |
| 1792 | } |
| 1793 | const uint8_t* annotation = |
| 1794 | SearchEncodedAnnotation(dex_file, annotation_item->annotation_, element_name); |
| 1795 | if (annotation == nullptr) { |
| 1796 | return nullptr; |
| 1797 | } |
| 1798 | DexFile::AnnotationValue annotation_value; |
| 1799 | bool result = Runtime::Current()->IsActiveTransaction() |
| 1800 | ? ProcessAnnotationValue<true>(data, |
| 1801 | &annotation, |
| 1802 | &annotation_value, |
| 1803 | array_class, |
| 1804 | DexFile::kPrimitivesOrObjects) |
| 1805 | : ProcessAnnotationValue<false>(data, |
| 1806 | &annotation, |
| 1807 | &annotation_value, |
| 1808 | array_class, |
| 1809 | DexFile::kPrimitivesOrObjects); |
| 1810 | if (!result) { |
| 1811 | return nullptr; |
| 1812 | } |
| 1813 | return annotation_value.value_.GetL(); |
| 1814 | } |
| 1815 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1816 | bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) { |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1817 | ClassData data(klass); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1818 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1819 | if (annotation_set == nullptr) { |
| 1820 | return false; |
| 1821 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1822 | const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet( |
Alex Light | f2f1c9d | 2017-03-15 15:35:46 +0000 | [diff] [blame] | 1823 | data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1824 | return annotation_item != nullptr; |
| 1825 | } |
| 1826 | |
| 1827 | int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) { |
| 1828 | // For native method, lineno should be -2 to indicate it is native. Note that |
| 1829 | // "line number == -2" is how libcore tells from StackTraceElement. |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1830 | if (!method->HasCodeItem()) { |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1831 | return -2; |
| 1832 | } |
| 1833 | |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 1834 | CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo()); |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 1835 | DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation(); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1836 | |
| 1837 | // A method with no line number info should return -1 |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 1838 | uint32_t line_num = -1; |
| 1839 | accessor.GetLineNumForPc(rel_pc, &line_num); |
| 1840 | return line_num; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | template<bool kTransactionActive> |
| 1844 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const { |
| 1845 | DCHECK(dex_cache_ != nullptr); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1846 | switch (type_) { |
| 1847 | case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z); |
| 1848 | break; |
| 1849 | case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break; |
| 1850 | case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break; |
| 1851 | case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break; |
| 1852 | case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break; |
| 1853 | case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break; |
| 1854 | case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break; |
| 1855 | case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break; |
| 1856 | case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break; |
| 1857 | case kString: { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 1858 | ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i), |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 1859 | dex_cache_); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1860 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
| 1861 | break; |
| 1862 | } |
| 1863 | case kType: { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1864 | ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i), |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 1865 | dex_cache_, |
| 1866 | class_loader_); |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1867 | field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved); |
| 1868 | break; |
| 1869 | } |
| 1870 | default: UNIMPLEMENTED(FATAL) << ": type " << type_; |
| 1871 | } |
| 1872 | } |
| 1873 | template |
| 1874 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const; |
| 1875 | template |
| 1876 | void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const; |
| 1877 | |
Victor Chang | c8507c8 | 2023-04-22 09:24:57 +0100 | [diff] [blame] | 1878 | inline static VisitorStatus VisitElement(AnnotationVisitor* visitor, |
| 1879 | const char* element_name, |
| 1880 | uint8_t depth, |
| 1881 | uint32_t element_index, |
| 1882 | const DexFile::AnnotationValue& annotation_value) |
| 1883 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1884 | if (depth == 0) { |
| 1885 | return visitor->VisitAnnotationElement( |
| 1886 | element_name, annotation_value.type_, annotation_value.value_); |
| 1887 | } else { |
| 1888 | return visitor->VisitArrayElement( |
| 1889 | depth - 1, element_index, annotation_value.type_, annotation_value.value_); |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | static VisitorStatus VisitEncodedValue(const ClassData& klass, |
| 1894 | const DexFile& dex_file, |
| 1895 | const uint8_t** annotation_ptr, |
| 1896 | AnnotationVisitor* visitor, |
| 1897 | const char* element_name, |
| 1898 | uint8_t depth, |
| 1899 | uint32_t element_index) |
| 1900 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1901 | DexFile::AnnotationValue annotation_value; |
| 1902 | // kTransactionActive is safe because the result_style is kAllRaw. |
| 1903 | bool is_consumed = ProcessAnnotationValue<false>(klass, |
| 1904 | annotation_ptr, |
| 1905 | &annotation_value, |
| 1906 | ScopedNullHandle<mirror::Class>(), |
| 1907 | DexFile::kAllRaw); |
| 1908 | |
| 1909 | VisitorStatus status = |
| 1910 | VisitElement(visitor, element_name, depth, element_index, annotation_value); |
| 1911 | switch (annotation_value.type_) { |
| 1912 | case DexFile::kDexAnnotationArray: { |
| 1913 | DCHECK(!is_consumed) << " unexpected consumption of array-typed element '" << element_name |
| 1914 | << "' annotating the class " << klass.GetRealClass()->PrettyClass(); |
| 1915 | SkipEncodedValueHeaderByte(annotation_ptr); |
| 1916 | uint32_t array_size = DecodeUnsignedLeb128(annotation_ptr); |
| 1917 | uint8_t next_depth = depth + 1; |
| 1918 | VisitorStatus element_status = (status == VisitorStatus::kVisitInner) ? |
| 1919 | VisitorStatus::kVisitNext : |
| 1920 | VisitorStatus::kVisitBreak; |
| 1921 | uint32_t i = 0; |
| 1922 | for (; i < array_size && element_status != VisitorStatus::kVisitBreak; ++i) { |
| 1923 | element_status = VisitEncodedValue( |
| 1924 | klass, dex_file, annotation_ptr, visitor, element_name, next_depth, i); |
| 1925 | } |
| 1926 | for (; i < array_size; ++i) { |
| 1927 | SkipAnnotationValue(dex_file, annotation_ptr); |
| 1928 | } |
| 1929 | break; |
| 1930 | } |
| 1931 | case DexFile::kDexAnnotationAnnotation: { |
| 1932 | DCHECK(!is_consumed) << " unexpected consumption of annotation-typed element '" |
| 1933 | << element_name << "' annotating the class " |
| 1934 | << klass.GetRealClass()->PrettyClass(); |
| 1935 | SkipEncodedValueHeaderByte(annotation_ptr); |
| 1936 | DecodeUnsignedLeb128(annotation_ptr); // unused type_index |
| 1937 | uint32_t size = DecodeUnsignedLeb128(annotation_ptr); |
| 1938 | for (; size != 0u; --size) { |
| 1939 | DecodeUnsignedLeb128(annotation_ptr); // unused element_name_index |
| 1940 | SkipAnnotationValue(dex_file, annotation_ptr); |
| 1941 | } |
| 1942 | break; |
| 1943 | } |
| 1944 | default: { |
| 1945 | // kDexAnnotationArray and kDexAnnotationAnnotation are the only 2 known value_types causing |
| 1946 | // ProcessAnnotationValue return false. For other value_types, we shouldn't need to iterate |
| 1947 | // over annotation_ptr and skip the value here. |
| 1948 | DCHECK(is_consumed) << StringPrintf( |
| 1949 | "consumed annotation element type 0x%02x of %s for the class %s", |
| 1950 | annotation_value.type_, |
| 1951 | element_name, |
| 1952 | klass.GetRealClass()->PrettyClass().c_str()); |
| 1953 | if (UNLIKELY(!is_consumed)) { |
| 1954 | SkipAnnotationValue(dex_file, annotation_ptr); |
| 1955 | } |
| 1956 | break; |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | return status; |
| 1961 | } |
| 1962 | |
| 1963 | void VisitClassAnnotations(Handle<mirror::Class> klass, AnnotationVisitor* visitor) { |
| 1964 | ClassData data(klass); |
| 1965 | const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data); |
| 1966 | if (annotation_set == nullptr) { |
| 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | const DexFile& dex_file = data.GetDexFile(); |
| 1971 | for (uint32_t i = 0; i < annotation_set->size_; ++i) { |
| 1972 | const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i); |
| 1973 | uint8_t visibility = annotation_item->visibility_; |
| 1974 | const uint8_t* annotation = annotation_item->annotation_; |
| 1975 | uint32_t type_index = DecodeUnsignedLeb128(&annotation); |
| 1976 | const char* annotation_descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index)); |
| 1977 | VisitorStatus status = visitor->VisitAnnotation(annotation_descriptor, visibility); |
| 1978 | switch (status) { |
| 1979 | case VisitorStatus::kVisitBreak: |
| 1980 | return; |
| 1981 | case VisitorStatus::kVisitNext: |
| 1982 | continue; |
| 1983 | case VisitorStatus::kVisitInner: |
| 1984 | // Visit the annotation elements |
| 1985 | break; |
| 1986 | } |
| 1987 | |
| 1988 | uint32_t size = DecodeUnsignedLeb128(&annotation); |
| 1989 | while (size != 0) { |
| 1990 | uint32_t element_name_index = DecodeUnsignedLeb128(&annotation); |
| 1991 | const char* element_name = |
| 1992 | dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index))); |
| 1993 | |
| 1994 | status = VisitEncodedValue( |
| 1995 | data, dex_file, &annotation, visitor, element_name, /*depth=*/0, /*ignored*/ 0); |
| 1996 | if (status == VisitorStatus::kVisitBreak) { |
| 1997 | break; |
| 1998 | } |
| 1999 | size--; |
| 2000 | } |
| 2001 | } |
| 2002 | } |
| 2003 | |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 2004 | } // namespace annotations |
| 2005 | |
| 2006 | } // namespace art |