blob: 827ced95459c0313d553cf13c44f9b2ee31f5244 [file] [log] [blame]
David Sehr9323e6e2016-09-13 08:58:35 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dex_file_annotations.h"
18
19#include <stdlib.h>
20
Victor Changc8507c82023-04-22 09:24:57 +010021#include "android-base/macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include "android-base/stringprintf.h"
David Sehr9323e6e2016-09-13 08:58:35 -070023#include "art_field-inl.h"
Vladimir Markob9c73f82022-11-22 15:18:43 +000024#include "art_method-alloc-inl.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000025#include "base/sdk_version.h"
David Sehr9323e6e2016-09-13 08:58:35 -070026#include "class_linker-inl.h"
Vladimir Marko5868ada2020-05-12 11:50:34 +010027#include "class_root-inl.h"
David Sehr334b9d72018-02-12 18:27:56 -080028#include "dex/dex_file-inl.h"
Hans Boehm206348c2018-12-05 11:11:33 -080029#include "dex/dex_instruction-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010030#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070031#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070033#include "mirror/class-alloc-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070034#include "mirror/field.h"
35#include "mirror/method.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070036#include "mirror/object_array-alloc-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070037#include "mirror/object_array-inl.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000038#include "oat_file.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010039#include "obj_ptr-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070040#include "reflection.h"
41#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070042#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070043
44namespace art {
45
Andreas Gampe46ee31b2016-12-14 10:11:49 -080046using android::base::StringPrintf;
47
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080048using dex::AnnotationItem;
49using dex::AnnotationSetItem;
50using dex::AnnotationSetRefItem;
51using dex::AnnotationSetRefList;
52using dex::AnnotationsDirectoryItem;
53using dex::FieldAnnotationsItem;
54using dex::MethodAnnotationsItem;
55using dex::ParameterAnnotationsItem;
56
David Sehr9323e6e2016-09-13 08:58:35 -070057struct DexFile::AnnotationValue {
58 JValue value_;
59 uint8_t type_;
60};
61
62namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000063
64// A helper class that contains all the data needed to do annotation lookup.
65class ClassData {
66 public:
67 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
68 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
69 method,
70 *method->GetDexFile(),
71 &method->GetClassDef()) {}
72
73 // Requires Scope to be able to create at least 1 handles.
74 template <typename Scope>
75 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
76 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
77
78 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
79 : ClassData(klass, // klass
80 nullptr, // method
81 klass->GetDexFile(),
82 klass->GetClassDef()) {}
83
84 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
85 return dex_file_;
86 }
87
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080088 const dex::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000089 return class_def_;
90 }
91
92 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
93 if (method_ != nullptr) {
94 return method_->GetDexCache();
95 } else {
96 return real_klass_->GetDexCache();
97 }
98 }
99
100 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
101 if (method_ != nullptr) {
102 return method_->GetDeclaringClass()->GetClassLoader();
103 } else {
104 return real_klass_->GetClassLoader();
105 }
106 }
107
108 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
109 if (method_ != nullptr) {
110 return method_->GetDeclaringClass();
111 } else {
112 return real_klass_.Get();
113 }
114 }
115
116 private:
117 ClassData(Handle<mirror::Class> klass,
118 ArtMethod* method,
119 const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800120 const dex::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000121 : real_klass_(klass),
122 method_(method),
123 dex_file_(dex_file),
124 class_def_(class_def) {
125 DCHECK((method_ == nullptr) || real_klass_.IsNull());
126 }
127
128 Handle<mirror::Class> real_klass_;
129 ArtMethod* method_;
130 const DexFile& dex_file_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800131 const dex::ClassDef* class_def_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000132
133 DISALLOW_COPY_AND_ASSIGN(ClassData);
134};
135
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100136ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
137 Handle<mirror::Class> annotation_class,
138 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700139 REQUIRES_SHARED(Locks::mutator_lock_);
140
141bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
142 if (expected == DexFile::kDexVisibilityRuntime) {
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000143 if (IsSdkVersionSetAndAtMost(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kM)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700144 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
145 }
146 }
147 return actual == expected;
148}
149
Hans Boehm206348c2018-12-05 11:11:33 -0800150static const AnnotationSetItem* FindAnnotationSetForField(const DexFile& dex_file,
151 const dex::ClassDef& class_def,
152 uint32_t field_index)
David Sehr9323e6e2016-09-13 08:58:35 -0700153 REQUIRES_SHARED(Locks::mutator_lock_) {
Hans Boehm206348c2018-12-05 11:11:33 -0800154 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
155 if (annotations_dir == nullptr) {
156 return nullptr;
157 }
158 const FieldAnnotationsItem* field_annotations = dex_file.GetFieldAnnotations(annotations_dir);
159 if (field_annotations == nullptr) {
160 return nullptr;
161 }
162 uint32_t field_count = annotations_dir->fields_size_;
163 for (uint32_t i = 0; i < field_count; ++i) {
164 if (field_annotations[i].field_idx_ == field_index) {
165 return dex_file.GetFieldAnnotationSetItem(field_annotations[i]);
166 }
167 }
168 return nullptr;
169}
170
171static const AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
172 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700173 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800174 const dex::ClassDef* class_def = klass->GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700175 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700176 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700177 return nullptr;
178 }
Hans Boehm206348c2018-12-05 11:11:33 -0800179 return FindAnnotationSetForField(*field->GetDexFile(), *class_def, field->GetDexFieldIndex());
David Sehr9323e6e2016-09-13 08:58:35 -0700180}
181
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800182const AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
183 const AnnotationSetItem* annotation_set,
184 const char* descriptor,
185 uint32_t visibility)
David Sehr9323e6e2016-09-13 08:58:35 -0700186 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800187 const AnnotationItem* result = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700188 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800189 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700190 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
191 continue;
192 }
193 const uint8_t* annotation = annotation_item->annotation_;
194 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
195
Andreas Gampea5b09a62016-11-17 15:21:22 -0800196 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700197 result = annotation_item;
198 break;
199 }
200 }
201 return result;
202}
203
Victor Changc8507c82023-04-22 09:24:57 +0100204inline static void SkipEncodedValueHeaderByte(const uint8_t** annotation_ptr) {
205 (*annotation_ptr)++;
206}
207
David Sehr9323e6e2016-09-13 08:58:35 -0700208bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
209 REQUIRES_SHARED(Locks::mutator_lock_) {
210 const uint8_t* annotation = *annotation_ptr;
211 uint8_t header_byte = *(annotation++);
212 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
213 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
214 int32_t width = value_arg + 1;
215
216 switch (value_type) {
217 case DexFile::kDexAnnotationByte:
218 case DexFile::kDexAnnotationShort:
219 case DexFile::kDexAnnotationChar:
220 case DexFile::kDexAnnotationInt:
221 case DexFile::kDexAnnotationLong:
222 case DexFile::kDexAnnotationFloat:
223 case DexFile::kDexAnnotationDouble:
224 case DexFile::kDexAnnotationString:
225 case DexFile::kDexAnnotationType:
226 case DexFile::kDexAnnotationMethod:
227 case DexFile::kDexAnnotationField:
228 case DexFile::kDexAnnotationEnum:
229 break;
230 case DexFile::kDexAnnotationArray:
231 {
232 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700233 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700234 if (!SkipAnnotationValue(dex_file, &annotation)) {
235 return false;
236 }
237 }
238 width = 0;
239 break;
240 }
241 case DexFile::kDexAnnotationAnnotation:
242 {
243 DecodeUnsignedLeb128(&annotation); // unused type_index
244 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700245 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700246 DecodeUnsignedLeb128(&annotation); // unused element_name_index
247 if (!SkipAnnotationValue(dex_file, &annotation)) {
248 return false;
249 }
250 }
251 width = 0;
252 break;
253 }
254 case DexFile::kDexAnnotationBoolean:
255 case DexFile::kDexAnnotationNull:
256 width = 0;
257 break;
258 default:
259 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
Elliott Hughesc1896c92018-11-29 11:33:18 -0800260 UNREACHABLE();
David Sehr9323e6e2016-09-13 08:58:35 -0700261 }
262
263 annotation += width;
264 *annotation_ptr = annotation;
265 return true;
266}
267
268const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
269 const uint8_t* annotation,
270 const char* name)
271 REQUIRES_SHARED(Locks::mutator_lock_) {
272 DecodeUnsignedLeb128(&annotation); // unused type_index
273 uint32_t size = DecodeUnsignedLeb128(&annotation);
274
275 while (size != 0) {
276 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800277 const char* element_name =
278 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700279 if (strcmp(name, element_name) == 0) {
280 return annotation;
281 }
282 SkipAnnotationValue(dex_file, &annotation);
283 size--;
284 }
285 return nullptr;
286}
287
Hans Boehm206348c2018-12-05 11:11:33 -0800288static const AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
289 const dex::ClassDef& class_def,
290 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800291 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700292 if (annotations_dir == nullptr) {
293 return nullptr;
294 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800295 const MethodAnnotationsItem* method_annotations = dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700296 if (method_annotations == nullptr) {
297 return nullptr;
298 }
David Sehr9323e6e2016-09-13 08:58:35 -0700299 uint32_t method_count = annotations_dir->methods_size_;
300 for (uint32_t i = 0; i < method_count; ++i) {
301 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000302 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700303 }
304 }
305 return nullptr;
306}
307
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800308inline const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
Vladimir Marko0db16e02017-11-08 14:32:33 +0000309 REQUIRES_SHARED(Locks::mutator_lock_) {
310 if (method->IsProxyMethod()) {
311 return nullptr;
312 }
313 return FindAnnotationSetForMethod(*method->GetDexFile(),
314 method->GetClassDef(),
315 method->GetDexMethodIndex());
316}
317
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800318const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -0700319 REQUIRES_SHARED(Locks::mutator_lock_) {
320 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800321 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000322 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700323 if (annotations_dir == nullptr) {
324 return nullptr;
325 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800326 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -0700327 dex_file->GetParameterAnnotations(annotations_dir);
328 if (parameter_annotations == nullptr) {
329 return nullptr;
330 }
331 uint32_t method_index = method->GetDexMethodIndex();
332 uint32_t parameter_count = annotations_dir->parameters_size_;
333 for (uint32_t i = 0; i < parameter_count; ++i) {
334 if (parameter_annotations[i].method_idx_ == method_index) {
335 return &parameter_annotations[i];
336 }
337 }
338 return nullptr;
339}
340
Hans Boehm206348c2018-12-05 11:11:33 -0800341static const AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700342 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000343 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800344 const dex::ClassDef* class_def = klass.GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700345 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700346 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700347 return nullptr;
348 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800349 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700350 if (annotations_dir == nullptr) {
351 return nullptr;
352 }
353 return dex_file.GetClassAnnotationSet(annotations_dir);
354}
355
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100356ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700357 REQUIRES_SHARED(Locks::mutator_lock_) {
358 uint32_t type_index = DecodeUnsignedLeb128(annotation);
359 uint32_t size = DecodeUnsignedLeb128(annotation);
360
361 Thread* self = Thread::Current();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000362 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700363 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
364 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000365 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000366 hs.NewHandle(klass.GetDexCache()),
367 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800368 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000369 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
370 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700371 DCHECK(Thread::Current()->IsExceptionPending());
372 Thread::Current()->ClearException();
373 return nullptr;
374 }
375
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100376 ObjPtr<mirror::Class> annotation_member_array_class =
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000377 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember__array);
David Sehr9323e6e2016-09-13 08:58:35 -0700378 if (annotation_member_array_class == nullptr) {
379 return nullptr;
380 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100381 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700382 if (size > 0) {
383 element_array =
384 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
385 if (element_array == nullptr) {
386 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
387 return nullptr;
388 }
389 }
390
391 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
392 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100393 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700394 if (new_member == nullptr) {
395 return nullptr;
396 }
397 h_element_array->SetWithoutChecks<false>(i, new_member);
398 }
399
David Sehr9323e6e2016-09-13 08:58:35 -0700400 ArtMethod* create_annotation_method =
Vladimir Markof776c172022-11-03 17:29:49 +0000401 WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
Vladimir Markof776c172022-11-03 17:29:49 +0000402 ObjPtr<mirror::Object> result = create_annotation_method->InvokeStatic<'L', 'L', 'L'>(
403 self, annotation_class.Get(), h_element_array.Get());
David Sehr9323e6e2016-09-13 08:58:35 -0700404 if (self->IsExceptionPending()) {
405 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
406 return nullptr;
407 }
408
Vladimir Markof776c172022-11-03 17:29:49 +0000409 return result;
David Sehr9323e6e2016-09-13 08:58:35 -0700410}
411
Andreas Gampe9486a162017-02-16 15:17:47 -0800412template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000413bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700414 const uint8_t** annotation_ptr,
415 DexFile::AnnotationValue* annotation_value,
416 Handle<mirror::Class> array_class,
417 DexFile::AnnotationResultStyle result_style)
418 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000419 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700420 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700421 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700422 bool set_object = false;
423 Primitive::Type primitive_type = Primitive::kPrimVoid;
424 const uint8_t* annotation = *annotation_ptr;
425 uint8_t header_byte = *(annotation++);
426 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
427 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
428 int32_t width = value_arg + 1;
429 annotation_value->type_ = value_type;
430
431 switch (value_type) {
432 case DexFile::kDexAnnotationByte:
433 annotation_value->value_.SetB(
434 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
435 primitive_type = Primitive::kPrimByte;
436 break;
437 case DexFile::kDexAnnotationShort:
438 annotation_value->value_.SetS(
439 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
440 primitive_type = Primitive::kPrimShort;
441 break;
442 case DexFile::kDexAnnotationChar:
443 annotation_value->value_.SetC(
444 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
445 primitive_type = Primitive::kPrimChar;
446 break;
447 case DexFile::kDexAnnotationInt:
448 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
449 primitive_type = Primitive::kPrimInt;
450 break;
451 case DexFile::kDexAnnotationLong:
452 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
453 primitive_type = Primitive::kPrimLong;
454 break;
455 case DexFile::kDexAnnotationFloat:
456 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
457 primitive_type = Primitive::kPrimFloat;
458 break;
459 case DexFile::kDexAnnotationDouble:
460 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
461 primitive_type = Primitive::kPrimDouble;
462 break;
463 case DexFile::kDexAnnotationBoolean:
464 annotation_value->value_.SetZ(value_arg != 0);
465 primitive_type = Primitive::kPrimBoolean;
466 width = 0;
467 break;
468 case DexFile::kDexAnnotationString: {
469 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
470 if (result_style == DexFile::kAllRaw) {
471 annotation_value->value_.SetI(index);
472 } else {
473 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700474 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000475 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700476 set_object = true;
477 if (element_object == nullptr) {
478 return false;
479 }
480 }
481 break;
482 }
483 case DexFile::kDexAnnotationType: {
484 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
485 if (result_style == DexFile::kAllRaw) {
486 annotation_value->value_.SetI(index);
487 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800488 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000489 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700490 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000491 type_index,
492 hs.NewHandle(klass.GetDexCache()),
493 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700494 set_object = true;
495 if (element_object == nullptr) {
496 CHECK(self->IsExceptionPending());
497 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800498 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700499 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
500 element_object = self->GetException();
501 self->ClearException();
502 } else {
503 return false;
504 }
505 }
506 }
507 break;
508 }
509 case DexFile::kDexAnnotationMethod: {
510 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
511 if (result_style == DexFile::kAllRaw) {
512 annotation_value->value_.SetI(index);
513 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000514 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000515 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700516 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000517 index,
518 hs.NewHandle(klass.GetDexCache()),
519 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700520 if (method == nullptr) {
521 return false;
522 }
523 PointerSize pointer_size = class_linker->GetImagePointerSize();
524 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700525 if (method->IsConstructor()) {
Vladimir Markob6f4c792020-05-04 15:37:29 +0100526 element_object = (pointer_size == PointerSize::k64)
527 ? mirror::Constructor::CreateFromArtMethod<PointerSize::k64>(self, method)
528 : mirror::Constructor::CreateFromArtMethod<PointerSize::k32>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700529 } else {
Vladimir Markob6f4c792020-05-04 15:37:29 +0100530 element_object = (pointer_size == PointerSize::k64)
531 ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, method)
532 : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700533 }
534 if (element_object == nullptr) {
535 return false;
536 }
537 }
538 break;
539 }
540 case DexFile::kDexAnnotationField: {
541 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
542 if (result_style == DexFile::kAllRaw) {
543 annotation_value->value_.SetI(index);
544 } else {
545 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700546 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000547 index,
548 hs.NewHandle(klass.GetDexCache()),
549 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700550 if (field == nullptr) {
551 return false;
552 }
553 set_object = true;
Vladimir Marko0a6063a2020-05-14 16:39:14 +0100554 element_object = mirror::Field::CreateFromArtField(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700555 if (element_object == nullptr) {
556 return false;
557 }
558 }
559 break;
560 }
561 case DexFile::kDexAnnotationEnum: {
562 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
563 if (result_style == DexFile::kAllRaw) {
564 annotation_value->value_.SetI(index);
565 } else {
566 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700567 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000568 index,
569 hs.NewHandle(klass.GetDexCache()),
570 hs.NewHandle(klass.GetClassLoader()),
571 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700572 if (enum_field == nullptr) {
573 return false;
574 } else {
575 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
576 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
577 element_object = enum_field->GetObject(field_class.Get());
578 set_object = true;
579 }
580 }
581 break;
582 }
583 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800584 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700585 return false;
586 } else {
587 ScopedObjectAccessUnchecked soa(self);
588 StackHandleScope<2> hs(self);
589 uint32_t size = DecodeUnsignedLeb128(&annotation);
590 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100591 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc(
David Sehr9323e6e2016-09-13 08:58:35 -0700592 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
593 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800594 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700595 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
596 return false;
597 }
598 DexFile::AnnotationValue new_annotation_value;
599 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800600 if (!ProcessAnnotationValue<kTransactionActive>(klass,
601 &annotation,
602 &new_annotation_value,
603 component_type,
604 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700605 return false;
606 }
607 if (!component_type->IsPrimitive()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100608 ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800609 new_array->AsObjectArray<mirror::Object>()->
610 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700611 } else {
612 switch (new_annotation_value.type_) {
613 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800614 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700615 i, new_annotation_value.value_.GetB());
616 break;
617 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800618 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700619 i, new_annotation_value.value_.GetS());
620 break;
621 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800622 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700623 i, new_annotation_value.value_.GetC());
624 break;
625 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800626 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700627 i, new_annotation_value.value_.GetI());
628 break;
629 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800630 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700631 i, new_annotation_value.value_.GetJ());
632 break;
633 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800634 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700635 i, new_annotation_value.value_.GetF());
636 break;
637 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800638 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700639 i, new_annotation_value.value_.GetD());
640 break;
641 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800642 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700643 i, new_annotation_value.value_.GetZ());
644 break;
645 default:
646 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
647 return false;
648 }
649 }
650 }
651 element_object = new_array.Get();
652 set_object = true;
653 width = 0;
654 }
655 break;
656 case DexFile::kDexAnnotationAnnotation:
657 if (result_style == DexFile::kAllRaw) {
658 return false;
659 }
660 element_object = ProcessEncodedAnnotation(klass, &annotation);
661 if (element_object == nullptr) {
662 return false;
663 }
664 set_object = true;
665 width = 0;
666 break;
667 case DexFile::kDexAnnotationNull:
668 if (result_style == DexFile::kAllRaw) {
669 annotation_value->value_.SetI(0);
670 } else {
671 CHECK(element_object == nullptr);
672 set_object = true;
673 }
674 width = 0;
675 break;
676 default:
677 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
678 return false;
679 }
680
681 annotation += width;
682 *annotation_ptr = annotation;
683
684 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100685 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700686 set_object = true;
687 }
688
689 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100690 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700691 }
692
693 return true;
694}
695
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100696ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
697 Handle<mirror::Class> annotation_class,
698 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000699 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700700 Thread* self = Thread::Current();
701 ScopedObjectAccessUnchecked soa(self);
702 StackHandleScope<5> hs(self);
703 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800704 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700705
706 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
707 ArtMethod* annotation_method =
708 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
709 if (annotation_method == nullptr) {
710 return nullptr;
711 }
Vladimir Markob1baa732022-11-11 12:43:11 +0000712
713 Handle<mirror::String> string_name =
714 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name));
715 if (UNLIKELY(string_name == nullptr)) {
716 LOG(ERROR) << "Failed to allocate name for annotation member";
717 return nullptr;
718 }
719
720 Handle<mirror::Class> method_return = hs.NewHandle(annotation_method->ResolveReturnType());
721 if (UNLIKELY(method_return == nullptr)) {
722 LOG(ERROR) << "Failed to resolve method return type for annotation member";
723 return nullptr;
724 }
David Sehr9323e6e2016-09-13 08:58:35 -0700725
726 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800727 if (!ProcessAnnotationValue<false>(klass,
728 annotation,
729 &annotation_value,
730 method_return,
731 DexFile::kAllObjects)) {
Vladimir Markob1baa732022-11-11 12:43:11 +0000732 // TODO: Logging the error breaks run-test 005-annotations.
733 // LOG(ERROR) << "Failed to process annotation value for annotation member";
David Sehr9323e6e2016-09-13 08:58:35 -0700734 return nullptr;
735 }
Vladimir Markob1baa732022-11-11 12:43:11 +0000736 Handle<mirror::Object> value_object = hs.NewHandle(annotation_value.value_.GetL());
737
738 Handle<mirror::Method> method_object = hs.NewHandle((pointer_size == PointerSize::k64)
739 ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, annotation_method)
740 : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, annotation_method));
741 if (UNLIKELY(method_object == nullptr)) {
742 LOG(ERROR) << "Failed to create method object for annotation member";
743 return nullptr;
744 }
David Sehr9323e6e2016-09-13 08:58:35 -0700745
Vladimir Markob1baa732022-11-11 12:43:11 +0000746 Handle<mirror::Object> new_member =
Vladimir Markob9c73f82022-11-22 15:18:43 +0000747 WellKnownClasses::libcore_reflect_AnnotationMember_init->NewObject<'L', 'L', 'L', 'L'>(
748 hs, self, string_name, value_object, method_return, method_object);
Vladimir Markob1baa732022-11-11 12:43:11 +0000749 if (new_member == nullptr) {
Vladimir Markob9c73f82022-11-22 15:18:43 +0000750 DCHECK(self->IsExceptionPending());
751 LOG(ERROR) << "Failed to create annotation member";
David Sehr9323e6e2016-09-13 08:58:35 -0700752 return nullptr;
753 }
754
755 return new_member.Get();
756}
757
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800758const AnnotationItem* GetAnnotationItemFromAnnotationSet(const ClassData& klass,
759 const AnnotationSetItem* annotation_set,
760 uint32_t visibility,
761 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700762 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000763 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700764 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800765 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700766 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
767 continue;
768 }
769 const uint8_t* annotation = annotation_item->annotation_;
770 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
772 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000773 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000774 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000775 dex::TypeIndex(type_index),
776 hs.NewHandle(klass.GetDexCache()),
777 hs.NewHandle(klass.GetClassLoader()));
778 if (resolved_class == nullptr) {
779 std::string temp;
780 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
781 klass.GetRealClass()->GetDescriptor(&temp), type_index);
782 CHECK(self->IsExceptionPending());
783 self->ClearException();
784 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700785 }
786 if (resolved_class == annotation_class.Get()) {
787 return annotation_item;
788 }
789 }
790
791 return nullptr;
792}
793
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800794ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(const ClassData& klass,
795 const AnnotationSetItem* annotation_set,
796 uint32_t visibility,
797 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700798 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800799 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000800 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700801 if (annotation_item == nullptr) {
802 return nullptr;
803 }
804 const uint8_t* annotation = annotation_item->annotation_;
805 return ProcessEncodedAnnotation(klass, &annotation);
806}
807
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100808ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800809 const AnnotationItem* annotation_item,
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100810 const char* annotation_name,
811 Handle<mirror::Class> array_class,
812 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700813 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000814 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700815 const uint8_t* annotation =
816 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
817 if (annotation == nullptr) {
818 return nullptr;
819 }
820 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800821 bool result = Runtime::Current()->IsActiveTransaction()
822 ? ProcessAnnotationValue<true>(klass,
823 &annotation,
824 &annotation_value,
825 array_class,
826 DexFile::kAllObjects)
827 : ProcessAnnotationValue<false>(klass,
828 &annotation,
829 &annotation_value,
830 array_class,
831 DexFile::kAllObjects);
832 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700833 return nullptr;
834 }
835 if (annotation_value.type_ != expected_type) {
836 return nullptr;
837 }
838 return annotation_value.value_.GetL();
839}
840
Sorin Bascaa0191bc2022-06-15 16:53:48 +0100841template<typename T>
842static inline ObjPtr<mirror::ObjectArray<T>> GetAnnotationArrayValue(
843 Handle<mirror::Class> klass,
844 const char* annotation_name,
845 const char* value_name)
846 REQUIRES_SHARED(Locks::mutator_lock_) {
847 ClassData data(klass);
848 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
849 if (annotation_set == nullptr) {
850 return nullptr;
851 }
852 const AnnotationItem* annotation_item =
853 SearchAnnotationSet(data.GetDexFile(), annotation_set, annotation_name,
854 DexFile::kDexVisibilitySystem);
855 if (annotation_item == nullptr) {
856 return nullptr;
857 }
858 StackHandleScope<1> hs(Thread::Current());
859 Handle<mirror::Class> class_array_class =
860 hs.NewHandle(GetClassRoot<mirror::ObjectArray<T>>());
861 DCHECK(class_array_class != nullptr);
862 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
863 annotation_item,
864 value_name,
865 class_array_class,
866 DexFile::kDexAnnotationArray);
867 if (obj == nullptr) {
868 return nullptr;
869 }
870 return obj->AsObjectArray<T>();
871}
872
Vladimir Markoacb906d2018-05-30 10:23:49 +0100873static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue(
874 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800875 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700876 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000877 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700878 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800879 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700880 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
881 DexFile::kDexVisibilitySystem);
882 if (annotation_item == nullptr) {
883 return nullptr;
884 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100885 Handle<mirror::Class> string_array_class =
886 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>());
887 DCHECK(string_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100888 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700889 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
890 DexFile::kDexAnnotationArray);
891 if (obj == nullptr) {
892 return nullptr;
893 }
894 return obj->AsObjectArray<mirror::String>();
895}
896
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800897ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(const ClassData& klass,
898 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700899 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000900 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800901 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700902 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
903 DexFile::kDexVisibilitySystem);
904 if (annotation_item == nullptr) {
905 return nullptr;
906 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100907 StackHandleScope<1> hs(Thread::Current());
908 Handle<mirror::Class> class_array_class =
909 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
910 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100911 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700912 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
913 DexFile::kDexAnnotationArray);
914 if (obj == nullptr) {
915 return nullptr;
916 }
917 return obj->AsObjectArray<mirror::Class>();
918}
919
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100920ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000921 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800922 const AnnotationSetItem* annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -0700923 uint32_t visibility)
924 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000925 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700926 Thread* self = Thread::Current();
David Sehr9323e6e2016-09-13 08:58:35 -0700927 StackHandleScope<2> hs(self);
928 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000929 WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700930 if (annotation_set == nullptr) {
931 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
932 }
933
934 uint32_t size = annotation_set->size_;
935 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
936 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800937 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700938 return nullptr;
939 }
940
941 uint32_t dest_index = 0;
942 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800943 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700944 // Note that we do not use IsVisibilityCompatible here because older code
945 // was correct for this case.
946 if (annotation_item->visibility_ != visibility) {
947 continue;
948 }
949 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100950 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700951 if (annotation_obj != nullptr) {
952 result->SetWithoutChecks<false>(dest_index, annotation_obj);
953 ++dest_index;
954 } else if (self->IsExceptionPending()) {
955 return nullptr;
956 }
957 }
958
959 if (dest_index == size) {
960 return result.Get();
961 }
962
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100963 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700964 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
965 if (trimmed_result == nullptr) {
966 return nullptr;
967 }
968
969 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100970 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700971 trimmed_result->SetWithoutChecks<false>(i, obj);
972 }
973
974 return trimmed_result;
975}
976
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100977ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000978 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800979 const AnnotationSetRefList* set_ref_list,
David Sehr9323e6e2016-09-13 08:58:35 -0700980 uint32_t size)
981 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000982 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700983 Thread* self = Thread::Current();
David Sehr9323e6e2016-09-13 08:58:35 -0700984 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700985 ObjPtr<mirror::Class> annotation_array_class =
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000986 WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100987 ObjPtr<mirror::Class> annotation_array_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100988 Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700989 if (annotation_array_array_class == nullptr) {
990 return nullptr;
991 }
992 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
993 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800994 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700995 LOG(ERROR) << "Annotation set ref array allocation failed";
996 return nullptr;
997 }
998 for (uint32_t index = 0; index < size; ++index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800999 const AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
1000 const AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001001 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
1002 set_item,
1003 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001004 if (annotation_set == nullptr) {
1005 return nullptr;
1006 }
1007 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
1008 }
1009 return annotation_array_array.Get();
1010}
1011} // namespace
1012
1013namespace annotations {
1014
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001015ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1016 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001017 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001018 if (annotation_set == nullptr) {
1019 return nullptr;
1020 }
1021 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001022 const ClassData field_class(hs, field);
1023 return GetAnnotationObjectFromAnnotationSet(field_class,
1024 annotation_set,
1025 DexFile::kDexVisibilityRuntime,
1026 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001027}
1028
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001029ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001030 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001031 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001032 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001033 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1034}
1035
Vladimir Markoacb906d2018-05-30 10:23:49 +01001036ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001037 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001038 if (annotation_set == nullptr) {
1039 return nullptr;
1040 }
1041 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001042 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001043 return GetSignatureValue(field_class, annotation_set);
1044}
1045
1046bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001047 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001048 if (annotation_set == nullptr) {
1049 return false;
1050 }
1051 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001052 const ClassData field_class(hs, field);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001053 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
David Sehr9323e6e2016-09-13 08:58:35 -07001054 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1055 return annotation_item != nullptr;
1056}
1057
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001058ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001059 const ClassData klass(method);
1060 const DexFile* dex_file = &klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001061 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001062 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001063 if (annotations_dir == nullptr) {
1064 return nullptr;
1065 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001066 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001067 dex_file->GetClassAnnotationSet(annotations_dir);
1068 if (annotation_set == nullptr) {
1069 return nullptr;
1070 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001071 const AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001072 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1073 if (annotation_item == nullptr) {
1074 return nullptr;
1075 }
1076 const uint8_t* annotation =
1077 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1078 if (annotation == nullptr) {
1079 return nullptr;
1080 }
1081 uint8_t header_byte = *(annotation++);
1082 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1083 return nullptr;
1084 }
1085 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1086 if (annotation == nullptr) {
1087 return nullptr;
1088 }
1089 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001090 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001091 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001092 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001093 &annotation,
1094 &annotation_value,
1095 return_type,
1096 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001097 return nullptr;
1098 }
1099 return annotation_value.value_.GetL();
1100}
1101
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001102ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1103 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001104 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001105 if (annotation_set == nullptr) {
1106 return nullptr;
1107 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001108 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001109 DexFile::kDexVisibilityRuntime, annotation_class);
1110}
1111
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001112ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001113 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001114 return ProcessAnnotationSet(ClassData(method),
1115 annotation_set,
1116 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001117}
1118
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001119ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001120 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001121 if (annotation_set == nullptr) {
1122 return nullptr;
1123 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001124 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001125}
1126
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001127ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001128 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001129 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -07001130 FindAnnotationsItemForMethod(method);
1131 if (parameter_annotations == nullptr) {
1132 return nullptr;
1133 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001134 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001135 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1136 if (set_ref_list == nullptr) {
1137 return nullptr;
1138 }
1139 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001140 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001141}
1142
Orion Hodson58143d22018-02-20 08:44:20 +00001143uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1144 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001145 const ParameterAnnotationsItem* parameter_annotations =
Orion Hodson58143d22018-02-20 08:44:20 +00001146 FindAnnotationsItemForMethod(method);
1147 if (parameter_annotations == nullptr) {
1148 return 0u;
1149 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001150 const AnnotationSetRefList* set_ref_list =
Orion Hodson58143d22018-02-20 08:44:20 +00001151 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1152 if (set_ref_list == nullptr) {
1153 return 0u;
1154 }
1155 return set_ref_list->size_;
1156}
1157
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001158ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1159 uint32_t parameter_idx,
1160 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001161 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001162 const ParameterAnnotationsItem* parameter_annotations = FindAnnotationsItemForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001163 if (parameter_annotations == nullptr) {
1164 return nullptr;
1165 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001166 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001167 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1168 if (set_ref_list == nullptr) {
1169 return nullptr;
1170 }
1171 if (parameter_idx >= set_ref_list->size_) {
1172 return nullptr;
1173 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001174 const AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1175 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001176 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001177 if (annotation_set == nullptr) {
1178 return nullptr;
1179 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001180 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001181 annotation_set,
1182 DexFile::kDexVisibilityRuntime,
1183 annotation_class);
1184}
1185
Vladimir Markoacb906d2018-05-30 10:23:49 +01001186bool GetParametersMetadataForMethod(
1187 ArtMethod* method,
1188 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1189 /*out*/ MutableHandle<mirror::IntArray>* access_flags) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001190 const AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001191 FindAnnotationSetForMethod(method);
1192 if (annotation_set == nullptr) {
1193 return false;
1194 }
1195
1196 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001197 const AnnotationItem* annotation_item =
Neil Fuller79a21e72016-09-09 14:24:51 +01001198 SearchAnnotationSet(*dex_file,
1199 annotation_set,
1200 "Ldalvik/annotation/MethodParameters;",
1201 DexFile::kDexVisibilitySystem);
1202 if (annotation_item == nullptr) {
1203 return false;
1204 }
1205
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001206 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001207
1208 // Extract the parameters' names String[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001209 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1210 Handle<mirror::Class> string_array_class =
1211 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker));
1212 DCHECK(string_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001213
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001214 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001215 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001216 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001217 annotation_item,
1218 "names",
1219 string_array_class,
1220 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001221 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001222 return false;
1223 }
1224
1225 // Extract the parameters' access flags int[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001226 Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker)));
1227 DCHECK(int_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001228 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001229 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001230 annotation_item,
1231 "accessFlags",
1232 int_array_class,
1233 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001234 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001235 return false;
1236 }
1237
Vladimir Markoacb906d2018-05-30 10:23:49 +01001238 names->Assign(names_obj->AsObjectArray<mirror::String>());
1239 access_flags->Assign(access_flags_obj->AsIntArray());
Neil Fuller79a21e72016-09-09 14:24:51 +01001240 return true;
1241}
1242
Vladimir Markoacb906d2018-05-30 10:23:49 +01001243ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001244 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001245 if (annotation_set == nullptr) {
1246 return nullptr;
1247 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001248 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001249}
1250
Roland Levillain35e42f02017-06-26 18:14:39 +01001251bool IsMethodAnnotationPresent(ArtMethod* method,
1252 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001253 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001254 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001255 if (annotation_set == nullptr) {
1256 return false;
1257 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001258 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +00001259 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001260 return annotation_item != nullptr;
1261}
1262
Vladimir Marko0db16e02017-11-08 14:32:33 +00001263static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1264 if (kIsDebugBuild) {
1265 ScopedObjectAccess soa(Thread::Current());
1266 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1267 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001268 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1269 if (klass != nullptr) {
1270 // Lookup using the boot class path loader should yield the annotation class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001271 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr));
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001272 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001273 }
1274}
1275
1276// Check whether a method from the `dex_file` with the given `annotation_set`
1277// is annotated with `annotation_descriptor` with build visibility.
1278static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001279 const AnnotationSetItem& annotation_set,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001280 const char* annotation_descriptor,
1281 jclass annotation_class) {
1282 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001283 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
Vladimir Marko0db16e02017-11-08 14:32:33 +00001284 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1285 continue;
1286 }
1287 const uint8_t* annotation = annotation_item->annotation_;
1288 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1289 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1290 if (strcmp(descriptor, annotation_descriptor) == 0) {
1291 DCheckNativeAnnotation(descriptor, annotation_class);
1292 return true;
1293 }
1294 }
1295 return false;
1296}
1297
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001298uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001299 const dex::ClassDef& class_def,
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001300 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001301 const dex::AnnotationSetItem* annotation_set =
Vladimir Marko0db16e02017-11-08 14:32:33 +00001302 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001303 if (annotation_set == nullptr) {
1304 return 0u;
1305 }
1306 uint32_t access_flags = 0u;
1307 if (IsMethodBuildAnnotationPresent(
1308 dex_file,
1309 *annotation_set,
1310 "Ldalvik/annotation/optimization/FastNative;",
1311 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1312 access_flags |= kAccFastNative;
1313 }
1314 if (IsMethodBuildAnnotationPresent(
1315 dex_file,
1316 *annotation_set,
1317 "Ldalvik/annotation/optimization/CriticalNative;",
1318 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1319 access_flags |= kAccCriticalNative;
1320 }
1321 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1322 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001323}
1324
Kevin Jeon55737562021-11-19 20:39:30 +00001325bool MethodIsNeverCompile(const DexFile& dex_file,
1326 const dex::ClassDef& class_def,
1327 uint32_t method_index) {
1328 const dex::AnnotationSetItem* annotation_set =
1329 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1330 if (annotation_set == nullptr) {
1331 return false;
1332 }
1333 return IsMethodBuildAnnotationPresent(
1334 dex_file,
1335 *annotation_set,
1336 "Ldalvik/annotation/optimization/NeverCompile;",
1337 WellKnownClasses::dalvik_annotation_optimization_NeverCompile);
1338}
1339
Santiago Aboy Solanes2979d532022-09-29 18:03:13 +01001340bool MethodIsNeverInline(const DexFile& dex_file,
1341 const dex::ClassDef& class_def,
1342 uint32_t method_index) {
1343 const dex::AnnotationSetItem* annotation_set =
1344 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1345 if (annotation_set == nullptr) {
1346 return false;
1347 }
1348 return IsMethodBuildAnnotationPresent(
1349 dex_file,
1350 *annotation_set,
1351 "Ldalvik/annotation/optimization/NeverInline;",
1352 WellKnownClasses::dalvik_annotation_optimization_NeverInline);
1353}
Kevin Jeon55737562021-11-19 20:39:30 +00001354
Hans Boehm206348c2018-12-05 11:11:33 -08001355bool FieldIsReachabilitySensitive(const DexFile& dex_file,
1356 const dex::ClassDef& class_def,
1357 uint32_t field_index)
1358 REQUIRES_SHARED(Locks::mutator_lock_) {
1359 const AnnotationSetItem* annotation_set =
1360 FindAnnotationSetForField(dex_file, class_def, field_index);
1361 if (annotation_set == nullptr) {
1362 return false;
1363 }
1364 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1365 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1366 // TODO: We're missing the equivalent of DCheckNativeAnnotation (not a DCHECK). Does it matter?
1367 return annotation_item != nullptr;
1368}
1369
1370bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1371 const dex::ClassDef& class_def,
1372 uint32_t method_index)
1373 REQUIRES_SHARED(Locks::mutator_lock_) {
1374 const AnnotationSetItem* annotation_set =
1375 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1376 if (annotation_set == nullptr) {
1377 return false;
1378 }
1379 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1380 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1381 return annotation_item != nullptr;
1382}
1383
1384static bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1385 uint32_t method_index)
1386 REQUIRES_SHARED(Locks::mutator_lock_) {
1387 DCHECK(method_index < dex_file.NumMethodIds());
1388 const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
1389 dex::TypeIndex class_index = method_id.class_idx_;
1390 const dex::ClassDef * class_def = dex_file.FindClassDef(class_index);
1391 return class_def != nullptr
1392 && MethodIsReachabilitySensitive(dex_file, *class_def, method_index);
1393}
1394
1395bool MethodContainsRSensitiveAccess(const DexFile& dex_file,
1396 const dex::ClassDef& class_def,
1397 uint32_t method_index)
1398 REQUIRES_SHARED(Locks::mutator_lock_) {
1399 // TODO: This is too slow to run very regularly. Currently this is only invoked in the
1400 // presence of @DeadReferenceSafe, which will be rare. In the long run, we need to quickly
1401 // check once whether a class has any @ReachabilitySensitive annotations. If not, we can
1402 // immediately return false here for any method in that class.
1403 uint32_t code_item_offset = dex_file.FindCodeItemOffset(class_def, method_index);
1404 const dex::CodeItem* code_item = dex_file.GetCodeItem(code_item_offset);
1405 CodeItemInstructionAccessor accessor(dex_file, code_item);
1406 if (!accessor.HasCodeItem()) {
1407 return false;
1408 }
Hans Boehm206348c2018-12-05 11:11:33 -08001409 for (DexInstructionIterator iter = accessor.begin(); iter != accessor.end(); ++iter) {
1410 switch (iter->Opcode()) {
1411 case Instruction::IGET:
Hans Boehm206348c2018-12-05 11:11:33 -08001412 case Instruction::IGET_WIDE:
Hans Boehm206348c2018-12-05 11:11:33 -08001413 case Instruction::IGET_OBJECT:
Hans Boehm206348c2018-12-05 11:11:33 -08001414 case Instruction::IGET_BOOLEAN:
Hans Boehm206348c2018-12-05 11:11:33 -08001415 case Instruction::IGET_BYTE:
Hans Boehm206348c2018-12-05 11:11:33 -08001416 case Instruction::IGET_CHAR:
Hans Boehm206348c2018-12-05 11:11:33 -08001417 case Instruction::IGET_SHORT:
Hans Boehm206348c2018-12-05 11:11:33 -08001418 case Instruction::IPUT:
Hans Boehm206348c2018-12-05 11:11:33 -08001419 case Instruction::IPUT_WIDE:
Hans Boehm206348c2018-12-05 11:11:33 -08001420 case Instruction::IPUT_OBJECT:
Hans Boehm206348c2018-12-05 11:11:33 -08001421 case Instruction::IPUT_BOOLEAN:
Hans Boehm206348c2018-12-05 11:11:33 -08001422 case Instruction::IPUT_BYTE:
Hans Boehm206348c2018-12-05 11:11:33 -08001423 case Instruction::IPUT_CHAR:
Hans Boehm206348c2018-12-05 11:11:33 -08001424 case Instruction::IPUT_SHORT:
Hans Boehm206348c2018-12-05 11:11:33 -08001425 {
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00001426 uint32_t field_index = iter->VRegC_22c();
Hans Boehm206348c2018-12-05 11:11:33 -08001427 DCHECK(field_index < dex_file.NumFieldIds());
1428 // We only guarantee to pay attention to the annotation if it's in the same class,
1429 // or a containing class, but it's OK to do so in other cases.
1430 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
1431 dex::TypeIndex class_index = field_id.class_idx_;
1432 const dex::ClassDef * field_class_def = dex_file.FindClassDef(class_index);
1433 // We do not handle the case in which the field is declared in a superclass, and
1434 // don't claim to do so. The annotated field should normally be private.
1435 if (field_class_def != nullptr
1436 && FieldIsReachabilitySensitive(dex_file, *field_class_def, field_index)) {
1437 return true;
1438 }
1439 }
1440 break;
1441 case Instruction::INVOKE_SUPER:
1442 // Cannot call method in same class. TODO: Try an explicit superclass lookup for
1443 // better "best effort"?
1444 break;
1445 case Instruction::INVOKE_INTERFACE:
1446 // We handle an interface call just like a virtual call. We will find annotations
1447 // on interface methods/fields visible to us, but not of the annotation is in a
1448 // super-interface. Again, we could just ignore it.
1449 case Instruction::INVOKE_VIRTUAL:
1450 case Instruction::INVOKE_DIRECT:
1451 {
1452 uint32_t called_method_index = iter->VRegB_35c();
1453 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1454 return true;
1455 }
1456 }
1457 break;
1458 case Instruction::INVOKE_INTERFACE_RANGE:
1459 case Instruction::INVOKE_VIRTUAL_RANGE:
1460 case Instruction::INVOKE_DIRECT_RANGE:
1461 {
1462 uint32_t called_method_index = iter->VRegB_3rc();
1463 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1464 return true;
1465 }
1466 }
1467 break;
Hans Boehm206348c2018-12-05 11:11:33 -08001468 // We explicitly do not handle indirect ReachabilitySensitive accesses through VarHandles,
1469 // etc. Thus we ignore INVOKE_CUSTOM / INVOKE_CUSTOM_RANGE / INVOKE_POLYMORPHIC /
1470 // INVOKE_POLYMORPHIC_RANGE.
1471 default:
1472 // There is no way to add an annotation to array elements, and so far we've encountered no
1473 // need for that, so we ignore AGET and APUT.
1474 // It's impractical or impossible to garbage collect a class while one of its methods is
1475 // on the call stack. We allow ReachabilitySensitive annotations on static methods and
1476 // fields, but they can be safely ignored.
1477 break;
1478 }
Hans Boehm206348c2018-12-05 11:11:33 -08001479 }
1480 return false;
1481}
1482
1483bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file,
1484 const dex::ClassDef& class_def)
1485 // TODO: This should check outer classes as well.
1486 // It's conservatively correct not to do so.
1487 REQUIRES_SHARED(Locks::mutator_lock_) {
1488 const AnnotationsDirectoryItem* annotations_dir =
1489 dex_file.GetAnnotationsDirectory(class_def);
1490 if (annotations_dir == nullptr) {
1491 return false;
1492 }
1493 const AnnotationSetItem* annotation_set = dex_file.GetClassAnnotationSet(annotations_dir);
1494 if (annotation_set == nullptr) {
1495 return false;
1496 }
1497 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1498 "Ldalvik/annotation/optimization/DeadReferenceSafe;", DexFile::kDexVisibilityRuntime);
1499 return annotation_item != nullptr;
1500}
1501
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001502ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1503 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001504 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001505 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001506 if (annotation_set == nullptr) {
1507 return nullptr;
1508 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001509 return GetAnnotationObjectFromAnnotationSet(data,
1510 annotation_set,
1511 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001512 annotation_class);
1513}
1514
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001515ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001516 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001517 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001518 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001519}
1520
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001521ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Sorin Bascaa0191bc2022-06-15 16:53:48 +01001522 return GetAnnotationArrayValue<mirror::Class>(klass,
1523 "Ldalvik/annotation/MemberClasses;",
1524 "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001525}
1526
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001527ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001528 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001529 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001530 if (annotation_set == nullptr) {
1531 return nullptr;
1532 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001533 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001534 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001535 DexFile::kDexVisibilitySystem);
1536 if (annotation_item == nullptr) {
1537 return nullptr;
1538 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001539 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1540 annotation_item,
1541 "value",
1542 ScopedNullHandle<mirror::Class>(),
1543 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001544 if (obj == nullptr) {
1545 return nullptr;
1546 }
Nicolas Geoffrayf740be5e2021-09-16 14:07:37 +01001547 if (!obj->IsClass()) {
1548 // TypeNotPresentException, throw the NoClassDefFoundError.
1549 Thread::Current()->SetException(obj->AsThrowable()->GetCause());
1550 return nullptr;
1551 }
David Sehr9323e6e2016-09-13 08:58:35 -07001552 return obj->AsClass();
1553}
1554
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001555ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1556 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
Nicolas Geoffrayf740be5e2021-09-16 14:07:37 +01001557 if (declaring_class != nullptr || Thread::Current()->IsExceptionPending()) {
David Sehr9323e6e2016-09-13 08:58:35 -07001558 return declaring_class;
1559 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001560 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001561 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001562 if (annotation_set == nullptr) {
1563 return nullptr;
1564 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001565 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001566 SearchAnnotationSet(data.GetDexFile(),
1567 annotation_set,
1568 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001569 DexFile::kDexVisibilitySystem);
1570 if (annotation_item == nullptr) {
1571 return nullptr;
1572 }
1573 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001574 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001575 if (annotation == nullptr) {
1576 return nullptr;
1577 }
1578 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001579 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001580 &annotation,
1581 &annotation_value,
1582 ScopedNullHandle<mirror::Class>(),
1583 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001584 return nullptr;
1585 }
1586 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1587 return nullptr;
1588 }
1589 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001590 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001591 annotation_value.value_.GetI(),
1592 hs.NewHandle(data.GetDexCache()),
1593 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001594 if (method == nullptr) {
1595 return nullptr;
1596 }
1597 return method->GetDeclaringClass();
1598}
1599
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001600ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001601 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001602 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001603 if (annotation_set == nullptr) {
1604 return nullptr;
1605 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001606 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001607 SearchAnnotationSet(data.GetDexFile(),
1608 annotation_set,
1609 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001610 DexFile::kDexVisibilitySystem);
1611 if (annotation_item == nullptr) {
1612 return nullptr;
1613 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001614 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001615 DexFile::kDexAnnotationMethod);
1616}
1617
Vladimir Markoacb906d2018-05-30 10:23:49 +01001618bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001619 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001620 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001621 if (annotation_set == nullptr) {
1622 return false;
1623 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001624 const AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001625 data.GetDexFile(),
1626 annotation_set,
1627 "Ldalvik/annotation/InnerClass;",
1628 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001629 if (annotation_item == nullptr) {
1630 return false;
1631 }
1632 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001633 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001634 if (annotation == nullptr) {
1635 return false;
1636 }
1637 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001638 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001639 &annotation,
1640 &annotation_value,
1641 ScopedNullHandle<mirror::Class>(),
1642 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001643 return false;
1644 }
1645 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1646 annotation_value.type_ != DexFile::kDexAnnotationString) {
1647 return false;
1648 }
1649 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1650 return true;
1651}
1652
1653bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001654 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001655 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001656 if (annotation_set == nullptr) {
1657 return false;
1658 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001659 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001660 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001661 DexFile::kDexVisibilitySystem);
1662 if (annotation_item == nullptr) {
1663 return false;
1664 }
1665 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001666 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001667 if (annotation == nullptr) {
1668 return false;
1669 }
1670 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001671 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001672 &annotation,
1673 &annotation_value,
1674 ScopedNullHandle<mirror::Class>(),
1675 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001676 return false;
1677 }
1678 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1679 return false;
1680 }
1681 *flags = annotation_value.value_.GetI();
1682 return true;
1683}
1684
Vladimir Markoacb906d2018-05-30 10:23:49 +01001685ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
1686 Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001687 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001688 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001689 if (annotation_set == nullptr) {
1690 return nullptr;
1691 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001692 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001693}
1694
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001695const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001696 // Before instantiating ClassData, check that klass has a DexCache
1697 // assigned. The ClassData constructor indirectly dereferences it
1698 // when calling klass->GetDexFile().
1699 if (klass->GetDexCache() == nullptr) {
1700 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1701 return nullptr;
1702 }
1703
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001704 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001705 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001706 if (annotation_set == nullptr) {
1707 return nullptr;
1708 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001709
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001710 const AnnotationItem* annotation_item = SearchAnnotationSet(
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001711 data.GetDexFile(),
1712 annotation_set,
1713 "Ldalvik/annotation/SourceDebugExtension;",
1714 DexFile::kDexVisibilitySystem);
1715 if (annotation_item == nullptr) {
1716 return nullptr;
1717 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001718
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001719 const uint8_t* annotation =
1720 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1721 if (annotation == nullptr) {
1722 return nullptr;
1723 }
1724 DexFile::AnnotationValue annotation_value;
1725 if (!ProcessAnnotationValue<false>(data,
1726 &annotation,
1727 &annotation_value,
1728 ScopedNullHandle<mirror::Class>(),
1729 DexFile::kAllRaw)) {
1730 return nullptr;
1731 }
1732 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1733 return nullptr;
1734 }
1735 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1736 return data.GetDexFile().StringDataByIdx(index);
1737}
1738
Sorin Basca08b3f872022-05-31 17:04:55 +01001739ObjPtr<mirror::Class> GetNestHost(Handle<mirror::Class> klass) {
1740 ClassData data(klass);
1741 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1742 if (annotation_set == nullptr) {
1743 return nullptr;
1744 }
1745 const AnnotationItem* annotation_item =
1746 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/NestHost;",
1747 DexFile::kDexVisibilitySystem);
1748 if (annotation_item == nullptr) {
1749 return nullptr;
1750 }
1751 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1752 annotation_item,
1753 "host",
1754 ScopedNullHandle<mirror::Class>(),
1755 DexFile::kDexAnnotationType);
1756 if (obj == nullptr) {
1757 return nullptr;
1758 }
1759 if (!obj->IsClass()) {
1760 // TypeNotPresentException, throw the NoClassDefFoundError.
1761 Thread::Current()->SetException(obj->AsThrowable()->GetCause());
1762 return nullptr;
1763 }
1764 return obj->AsClass();
1765}
1766
1767ObjPtr<mirror::ObjectArray<mirror::Class>> GetNestMembers(Handle<mirror::Class> klass) {
Sorin Bascaa0191bc2022-06-15 16:53:48 +01001768 return GetAnnotationArrayValue<mirror::Class>(klass,
1769 "Ldalvik/annotation/NestMembers;",
1770 "classes");
1771}
1772
1773ObjPtr<mirror::ObjectArray<mirror::Class>> GetPermittedSubclasses(Handle<mirror::Class> klass) {
1774 return GetAnnotationArrayValue<mirror::Class>(klass,
1775 "Ldalvik/annotation/PermittedSubclasses;",
1776 "value");
Sorin Basca08b3f872022-05-31 17:04:55 +01001777}
1778
Victor Changd8e3d112023-03-21 18:40:45 +00001779ObjPtr<mirror::Object> getRecordAnnotationElement(Handle<mirror::Class> klass,
1780 Handle<mirror::Class> array_class,
1781 const char* element_name) {
1782 ClassData data(klass);
1783 const DexFile& dex_file = klass->GetDexFile();
1784 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1785 if (annotation_set == nullptr) {
1786 return nullptr;
1787 }
1788 const AnnotationItem* annotation_item = SearchAnnotationSet(
1789 dex_file, annotation_set, "Ldalvik/annotation/Record;", DexFile::kDexVisibilitySystem);
1790 if (annotation_item == nullptr) {
1791 return nullptr;
1792 }
1793 const uint8_t* annotation =
1794 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, element_name);
1795 if (annotation == nullptr) {
1796 return nullptr;
1797 }
1798 DexFile::AnnotationValue annotation_value;
1799 bool result = Runtime::Current()->IsActiveTransaction()
1800 ? ProcessAnnotationValue<true>(data,
1801 &annotation,
1802 &annotation_value,
1803 array_class,
1804 DexFile::kPrimitivesOrObjects)
1805 : ProcessAnnotationValue<false>(data,
1806 &annotation,
1807 &annotation_value,
1808 array_class,
1809 DexFile::kPrimitivesOrObjects);
1810 if (!result) {
1811 return nullptr;
1812 }
1813 return annotation_value.value_.GetL();
1814}
1815
David Sehr9323e6e2016-09-13 08:58:35 -07001816bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001817 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001818 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001819 if (annotation_set == nullptr) {
1820 return false;
1821 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001822 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001823 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001824 return annotation_item != nullptr;
1825}
1826
1827int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1828 // For native method, lineno should be -2 to indicate it is native. Note that
1829 // "line number == -2" is how libcore tells from StackTraceElement.
Nicolas Geoffray47171752020-08-31 15:03:20 +01001830 if (!method->HasCodeItem()) {
David Sehr9323e6e2016-09-13 08:58:35 -07001831 return -2;
1832 }
1833
David Sehr0225f8e2018-01-31 08:52:24 +00001834 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001835 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001836
1837 // A method with no line number info should return -1
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001838 uint32_t line_num = -1;
1839 accessor.GetLineNumForPc(rel_pc, &line_num);
1840 return line_num;
David Sehr9323e6e2016-09-13 08:58:35 -07001841}
1842
1843template<bool kTransactionActive>
1844void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1845 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001846 switch (type_) {
1847 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1848 break;
1849 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1850 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1851 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1852 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1853 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1854 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1855 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1856 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1857 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001858 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001859 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001860 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1861 break;
1862 }
1863 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001864 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001865 dex_cache_,
1866 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001867 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1868 break;
1869 }
1870 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1871 }
1872}
1873template
1874void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1875template
1876void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1877
Victor Changc8507c82023-04-22 09:24:57 +01001878inline static VisitorStatus VisitElement(AnnotationVisitor* visitor,
1879 const char* element_name,
1880 uint8_t depth,
1881 uint32_t element_index,
1882 const DexFile::AnnotationValue& annotation_value)
1883 REQUIRES_SHARED(Locks::mutator_lock_) {
1884 if (depth == 0) {
1885 return visitor->VisitAnnotationElement(
1886 element_name, annotation_value.type_, annotation_value.value_);
1887 } else {
1888 return visitor->VisitArrayElement(
1889 depth - 1, element_index, annotation_value.type_, annotation_value.value_);
1890 }
1891}
1892
1893static VisitorStatus VisitEncodedValue(const ClassData& klass,
1894 const DexFile& dex_file,
1895 const uint8_t** annotation_ptr,
1896 AnnotationVisitor* visitor,
1897 const char* element_name,
1898 uint8_t depth,
1899 uint32_t element_index)
1900 REQUIRES_SHARED(Locks::mutator_lock_) {
1901 DexFile::AnnotationValue annotation_value;
1902 // kTransactionActive is safe because the result_style is kAllRaw.
1903 bool is_consumed = ProcessAnnotationValue<false>(klass,
1904 annotation_ptr,
1905 &annotation_value,
1906 ScopedNullHandle<mirror::Class>(),
1907 DexFile::kAllRaw);
1908
1909 VisitorStatus status =
1910 VisitElement(visitor, element_name, depth, element_index, annotation_value);
1911 switch (annotation_value.type_) {
1912 case DexFile::kDexAnnotationArray: {
1913 DCHECK(!is_consumed) << " unexpected consumption of array-typed element '" << element_name
1914 << "' annotating the class " << klass.GetRealClass()->PrettyClass();
1915 SkipEncodedValueHeaderByte(annotation_ptr);
1916 uint32_t array_size = DecodeUnsignedLeb128(annotation_ptr);
1917 uint8_t next_depth = depth + 1;
1918 VisitorStatus element_status = (status == VisitorStatus::kVisitInner) ?
1919 VisitorStatus::kVisitNext :
1920 VisitorStatus::kVisitBreak;
1921 uint32_t i = 0;
1922 for (; i < array_size && element_status != VisitorStatus::kVisitBreak; ++i) {
1923 element_status = VisitEncodedValue(
1924 klass, dex_file, annotation_ptr, visitor, element_name, next_depth, i);
1925 }
1926 for (; i < array_size; ++i) {
1927 SkipAnnotationValue(dex_file, annotation_ptr);
1928 }
1929 break;
1930 }
1931 case DexFile::kDexAnnotationAnnotation: {
1932 DCHECK(!is_consumed) << " unexpected consumption of annotation-typed element '"
1933 << element_name << "' annotating the class "
1934 << klass.GetRealClass()->PrettyClass();
1935 SkipEncodedValueHeaderByte(annotation_ptr);
1936 DecodeUnsignedLeb128(annotation_ptr); // unused type_index
1937 uint32_t size = DecodeUnsignedLeb128(annotation_ptr);
1938 for (; size != 0u; --size) {
1939 DecodeUnsignedLeb128(annotation_ptr); // unused element_name_index
1940 SkipAnnotationValue(dex_file, annotation_ptr);
1941 }
1942 break;
1943 }
1944 default: {
1945 // kDexAnnotationArray and kDexAnnotationAnnotation are the only 2 known value_types causing
1946 // ProcessAnnotationValue return false. For other value_types, we shouldn't need to iterate
1947 // over annotation_ptr and skip the value here.
1948 DCHECK(is_consumed) << StringPrintf(
1949 "consumed annotation element type 0x%02x of %s for the class %s",
1950 annotation_value.type_,
1951 element_name,
1952 klass.GetRealClass()->PrettyClass().c_str());
1953 if (UNLIKELY(!is_consumed)) {
1954 SkipAnnotationValue(dex_file, annotation_ptr);
1955 }
1956 break;
1957 }
1958 }
1959
1960 return status;
1961}
1962
1963void VisitClassAnnotations(Handle<mirror::Class> klass, AnnotationVisitor* visitor) {
1964 ClassData data(klass);
1965 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1966 if (annotation_set == nullptr) {
1967 return;
1968 }
1969
1970 const DexFile& dex_file = data.GetDexFile();
1971 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
1972 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
1973 uint8_t visibility = annotation_item->visibility_;
1974 const uint8_t* annotation = annotation_item->annotation_;
1975 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1976 const char* annotation_descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1977 VisitorStatus status = visitor->VisitAnnotation(annotation_descriptor, visibility);
1978 switch (status) {
1979 case VisitorStatus::kVisitBreak:
1980 return;
1981 case VisitorStatus::kVisitNext:
1982 continue;
1983 case VisitorStatus::kVisitInner:
1984 // Visit the annotation elements
1985 break;
1986 }
1987
1988 uint32_t size = DecodeUnsignedLeb128(&annotation);
1989 while (size != 0) {
1990 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
1991 const char* element_name =
1992 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
1993
1994 status = VisitEncodedValue(
1995 data, dex_file, &annotation, visitor, element_name, /*depth=*/0, /*ignored*/ 0);
1996 if (status == VisitorStatus::kVisitBreak) {
1997 break;
1998 }
1999 size--;
2000 }
2001 }
2002}
2003
David Sehr9323e6e2016-09-13 08:58:35 -07002004} // namespace annotations
2005
2006} // namespace art