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