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