blob: 7c461fd26e36dd27580e4381f5d06f593dec2edb [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
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"
Hans Boehm206348c2018-12-05 11:11:33 -080040#include "quicken_info.h"
David Sehr9323e6e2016-09-13 08:58:35 -070041#include "reflection.h"
42#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070043#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070044
45namespace art {
46
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080049using dex::AnnotationItem;
50using dex::AnnotationSetItem;
51using dex::AnnotationSetRefItem;
52using dex::AnnotationSetRefList;
53using dex::AnnotationsDirectoryItem;
54using dex::FieldAnnotationsItem;
55using dex::MethodAnnotationsItem;
56using dex::ParameterAnnotationsItem;
57
David Sehr9323e6e2016-09-13 08:58:35 -070058struct DexFile::AnnotationValue {
59 JValue value_;
60 uint8_t type_;
61};
62
63namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000064
65// A helper class that contains all the data needed to do annotation lookup.
66class ClassData {
67 public:
68 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
69 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
70 method,
71 *method->GetDexFile(),
72 &method->GetClassDef()) {}
73
74 // Requires Scope to be able to create at least 1 handles.
75 template <typename Scope>
76 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
77 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
78
79 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
80 : ClassData(klass, // klass
81 nullptr, // method
82 klass->GetDexFile(),
83 klass->GetClassDef()) {}
84
85 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
86 return dex_file_;
87 }
88
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080089 const dex::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000090 return class_def_;
91 }
92
93 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
94 if (method_ != nullptr) {
95 return method_->GetDexCache();
96 } else {
97 return real_klass_->GetDexCache();
98 }
99 }
100
101 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
102 if (method_ != nullptr) {
103 return method_->GetDeclaringClass()->GetClassLoader();
104 } else {
105 return real_klass_->GetClassLoader();
106 }
107 }
108
109 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
110 if (method_ != nullptr) {
111 return method_->GetDeclaringClass();
112 } else {
113 return real_klass_.Get();
114 }
115 }
116
117 private:
118 ClassData(Handle<mirror::Class> klass,
119 ArtMethod* method,
120 const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800121 const dex::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000122 : real_klass_(klass),
123 method_(method),
124 dex_file_(dex_file),
125 class_def_(class_def) {
126 DCHECK((method_ == nullptr) || real_klass_.IsNull());
127 }
128
129 Handle<mirror::Class> real_klass_;
130 ArtMethod* method_;
131 const DexFile& dex_file_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800132 const dex::ClassDef* class_def_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000133
134 DISALLOW_COPY_AND_ASSIGN(ClassData);
135};
136
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100137ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
138 Handle<mirror::Class> annotation_class,
139 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
141
142bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
143 if (expected == DexFile::kDexVisibilityRuntime) {
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000144 if (IsSdkVersionSetAndAtMost(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kM)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700145 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
146 }
147 }
148 return actual == expected;
149}
150
Hans Boehm206348c2018-12-05 11:11:33 -0800151static const AnnotationSetItem* FindAnnotationSetForField(const DexFile& dex_file,
152 const dex::ClassDef& class_def,
153 uint32_t field_index)
David Sehr9323e6e2016-09-13 08:58:35 -0700154 REQUIRES_SHARED(Locks::mutator_lock_) {
Hans Boehm206348c2018-12-05 11:11:33 -0800155 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
156 if (annotations_dir == nullptr) {
157 return nullptr;
158 }
159 const FieldAnnotationsItem* field_annotations = dex_file.GetFieldAnnotations(annotations_dir);
160 if (field_annotations == nullptr) {
161 return nullptr;
162 }
163 uint32_t field_count = annotations_dir->fields_size_;
164 for (uint32_t i = 0; i < field_count; ++i) {
165 if (field_annotations[i].field_idx_ == field_index) {
166 return dex_file.GetFieldAnnotationSetItem(field_annotations[i]);
167 }
168 }
169 return nullptr;
170}
171
172static const AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
173 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700174 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800175 const dex::ClassDef* class_def = klass->GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700176 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700177 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700178 return nullptr;
179 }
Hans Boehm206348c2018-12-05 11:11:33 -0800180 return FindAnnotationSetForField(*field->GetDexFile(), *class_def, field->GetDexFieldIndex());
David Sehr9323e6e2016-09-13 08:58:35 -0700181}
182
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800183const AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
184 const AnnotationSetItem* annotation_set,
185 const char* descriptor,
186 uint32_t visibility)
David Sehr9323e6e2016-09-13 08:58:35 -0700187 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800188 const AnnotationItem* result = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700189 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800190 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700191 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
192 continue;
193 }
194 const uint8_t* annotation = annotation_item->annotation_;
195 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
196
Andreas Gampea5b09a62016-11-17 15:21:22 -0800197 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700198 result = annotation_item;
199 break;
200 }
201 }
202 return result;
203}
204
205bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
206 REQUIRES_SHARED(Locks::mutator_lock_) {
207 const uint8_t* annotation = *annotation_ptr;
208 uint8_t header_byte = *(annotation++);
209 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
210 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
211 int32_t width = value_arg + 1;
212
213 switch (value_type) {
214 case DexFile::kDexAnnotationByte:
215 case DexFile::kDexAnnotationShort:
216 case DexFile::kDexAnnotationChar:
217 case DexFile::kDexAnnotationInt:
218 case DexFile::kDexAnnotationLong:
219 case DexFile::kDexAnnotationFloat:
220 case DexFile::kDexAnnotationDouble:
221 case DexFile::kDexAnnotationString:
222 case DexFile::kDexAnnotationType:
223 case DexFile::kDexAnnotationMethod:
224 case DexFile::kDexAnnotationField:
225 case DexFile::kDexAnnotationEnum:
226 break;
227 case DexFile::kDexAnnotationArray:
228 {
229 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700230 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700231 if (!SkipAnnotationValue(dex_file, &annotation)) {
232 return false;
233 }
234 }
235 width = 0;
236 break;
237 }
238 case DexFile::kDexAnnotationAnnotation:
239 {
240 DecodeUnsignedLeb128(&annotation); // unused type_index
241 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700242 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700243 DecodeUnsignedLeb128(&annotation); // unused element_name_index
244 if (!SkipAnnotationValue(dex_file, &annotation)) {
245 return false;
246 }
247 }
248 width = 0;
249 break;
250 }
251 case DexFile::kDexAnnotationBoolean:
252 case DexFile::kDexAnnotationNull:
253 width = 0;
254 break;
255 default:
256 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
Elliott Hughesc1896c92018-11-29 11:33:18 -0800257 UNREACHABLE();
David Sehr9323e6e2016-09-13 08:58:35 -0700258 }
259
260 annotation += width;
261 *annotation_ptr = annotation;
262 return true;
263}
264
265const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
266 const uint8_t* annotation,
267 const char* name)
268 REQUIRES_SHARED(Locks::mutator_lock_) {
269 DecodeUnsignedLeb128(&annotation); // unused type_index
270 uint32_t size = DecodeUnsignedLeb128(&annotation);
271
272 while (size != 0) {
273 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800274 const char* element_name =
275 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700276 if (strcmp(name, element_name) == 0) {
277 return annotation;
278 }
279 SkipAnnotationValue(dex_file, &annotation);
280 size--;
281 }
282 return nullptr;
283}
284
Hans Boehm206348c2018-12-05 11:11:33 -0800285static const AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
286 const dex::ClassDef& class_def,
287 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800288 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700289 if (annotations_dir == nullptr) {
290 return nullptr;
291 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800292 const MethodAnnotationsItem* method_annotations = dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700293 if (method_annotations == nullptr) {
294 return nullptr;
295 }
David Sehr9323e6e2016-09-13 08:58:35 -0700296 uint32_t method_count = annotations_dir->methods_size_;
297 for (uint32_t i = 0; i < method_count; ++i) {
298 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000299 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700300 }
301 }
302 return nullptr;
303}
304
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800305inline const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
Vladimir Marko0db16e02017-11-08 14:32:33 +0000306 REQUIRES_SHARED(Locks::mutator_lock_) {
307 if (method->IsProxyMethod()) {
308 return nullptr;
309 }
310 return FindAnnotationSetForMethod(*method->GetDexFile(),
311 method->GetClassDef(),
312 method->GetDexMethodIndex());
313}
314
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800315const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -0700316 REQUIRES_SHARED(Locks::mutator_lock_) {
317 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800318 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000319 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700320 if (annotations_dir == nullptr) {
321 return nullptr;
322 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800323 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -0700324 dex_file->GetParameterAnnotations(annotations_dir);
325 if (parameter_annotations == nullptr) {
326 return nullptr;
327 }
328 uint32_t method_index = method->GetDexMethodIndex();
329 uint32_t parameter_count = annotations_dir->parameters_size_;
330 for (uint32_t i = 0; i < parameter_count; ++i) {
331 if (parameter_annotations[i].method_idx_ == method_index) {
332 return &parameter_annotations[i];
333 }
334 }
335 return nullptr;
336}
337
Hans Boehm206348c2018-12-05 11:11:33 -0800338static const AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700339 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000340 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800341 const dex::ClassDef* class_def = klass.GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700342 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700343 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700344 return nullptr;
345 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800346 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700347 if (annotations_dir == nullptr) {
348 return nullptr;
349 }
350 return dex_file.GetClassAnnotationSet(annotations_dir);
351}
352
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100353ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700354 REQUIRES_SHARED(Locks::mutator_lock_) {
355 uint32_t type_index = DecodeUnsignedLeb128(annotation);
356 uint32_t size = DecodeUnsignedLeb128(annotation);
357
358 Thread* self = Thread::Current();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000359 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700360 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
361 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000362 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000363 hs.NewHandle(klass.GetDexCache()),
364 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800365 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000366 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
367 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700368 DCHECK(Thread::Current()->IsExceptionPending());
369 Thread::Current()->ClearException();
370 return nullptr;
371 }
372
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100373 ObjPtr<mirror::Class> annotation_member_array_class =
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000374 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember__array);
David Sehr9323e6e2016-09-13 08:58:35 -0700375 if (annotation_member_array_class == nullptr) {
376 return nullptr;
377 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100378 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700379 if (size > 0) {
380 element_array =
381 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
382 if (element_array == nullptr) {
383 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
384 return nullptr;
385 }
386 }
387
388 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
389 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100390 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700391 if (new_member == nullptr) {
392 return nullptr;
393 }
394 h_element_array->SetWithoutChecks<false>(i, new_member);
395 }
396
David Sehr9323e6e2016-09-13 08:58:35 -0700397 ArtMethod* create_annotation_method =
Vladimir Markof776c172022-11-03 17:29:49 +0000398 WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
Vladimir Markof776c172022-11-03 17:29:49 +0000399 ObjPtr<mirror::Object> result = create_annotation_method->InvokeStatic<'L', 'L', 'L'>(
400 self, annotation_class.Get(), h_element_array.Get());
David Sehr9323e6e2016-09-13 08:58:35 -0700401 if (self->IsExceptionPending()) {
402 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
403 return nullptr;
404 }
405
Vladimir Markof776c172022-11-03 17:29:49 +0000406 return result;
David Sehr9323e6e2016-09-13 08:58:35 -0700407}
408
Andreas Gampe9486a162017-02-16 15:17:47 -0800409template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000410bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700411 const uint8_t** annotation_ptr,
412 DexFile::AnnotationValue* annotation_value,
413 Handle<mirror::Class> array_class,
414 DexFile::AnnotationResultStyle result_style)
415 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000416 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700417 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700418 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700419 bool set_object = false;
420 Primitive::Type primitive_type = Primitive::kPrimVoid;
421 const uint8_t* annotation = *annotation_ptr;
422 uint8_t header_byte = *(annotation++);
423 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
424 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
425 int32_t width = value_arg + 1;
426 annotation_value->type_ = value_type;
427
428 switch (value_type) {
429 case DexFile::kDexAnnotationByte:
430 annotation_value->value_.SetB(
431 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
432 primitive_type = Primitive::kPrimByte;
433 break;
434 case DexFile::kDexAnnotationShort:
435 annotation_value->value_.SetS(
436 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
437 primitive_type = Primitive::kPrimShort;
438 break;
439 case DexFile::kDexAnnotationChar:
440 annotation_value->value_.SetC(
441 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
442 primitive_type = Primitive::kPrimChar;
443 break;
444 case DexFile::kDexAnnotationInt:
445 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
446 primitive_type = Primitive::kPrimInt;
447 break;
448 case DexFile::kDexAnnotationLong:
449 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
450 primitive_type = Primitive::kPrimLong;
451 break;
452 case DexFile::kDexAnnotationFloat:
453 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
454 primitive_type = Primitive::kPrimFloat;
455 break;
456 case DexFile::kDexAnnotationDouble:
457 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
458 primitive_type = Primitive::kPrimDouble;
459 break;
460 case DexFile::kDexAnnotationBoolean:
461 annotation_value->value_.SetZ(value_arg != 0);
462 primitive_type = Primitive::kPrimBoolean;
463 width = 0;
464 break;
465 case DexFile::kDexAnnotationString: {
466 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
467 if (result_style == DexFile::kAllRaw) {
468 annotation_value->value_.SetI(index);
469 } else {
470 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700471 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000472 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700473 set_object = true;
474 if (element_object == nullptr) {
475 return false;
476 }
477 }
478 break;
479 }
480 case DexFile::kDexAnnotationType: {
481 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
482 if (result_style == DexFile::kAllRaw) {
483 annotation_value->value_.SetI(index);
484 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800485 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000486 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700487 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000488 type_index,
489 hs.NewHandle(klass.GetDexCache()),
490 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700491 set_object = true;
492 if (element_object == nullptr) {
493 CHECK(self->IsExceptionPending());
494 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800495 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700496 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
497 element_object = self->GetException();
498 self->ClearException();
499 } else {
500 return false;
501 }
502 }
503 }
504 break;
505 }
506 case DexFile::kDexAnnotationMethod: {
507 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
508 if (result_style == DexFile::kAllRaw) {
509 annotation_value->value_.SetI(index);
510 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000511 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000512 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700513 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000514 index,
515 hs.NewHandle(klass.GetDexCache()),
516 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700517 if (method == nullptr) {
518 return false;
519 }
520 PointerSize pointer_size = class_linker->GetImagePointerSize();
521 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700522 if (method->IsConstructor()) {
Vladimir Markob6f4c792020-05-04 15:37:29 +0100523 element_object = (pointer_size == PointerSize::k64)
524 ? mirror::Constructor::CreateFromArtMethod<PointerSize::k64>(self, method)
525 : mirror::Constructor::CreateFromArtMethod<PointerSize::k32>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700526 } else {
Vladimir Markob6f4c792020-05-04 15:37:29 +0100527 element_object = (pointer_size == PointerSize::k64)
528 ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, method)
529 : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700530 }
531 if (element_object == nullptr) {
532 return false;
533 }
534 }
535 break;
536 }
537 case DexFile::kDexAnnotationField: {
538 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
539 if (result_style == DexFile::kAllRaw) {
540 annotation_value->value_.SetI(index);
541 } else {
542 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700543 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000544 index,
545 hs.NewHandle(klass.GetDexCache()),
546 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700547 if (field == nullptr) {
548 return false;
549 }
550 set_object = true;
Vladimir Marko0a6063a2020-05-14 16:39:14 +0100551 element_object = mirror::Field::CreateFromArtField(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700552 if (element_object == nullptr) {
553 return false;
554 }
555 }
556 break;
557 }
558 case DexFile::kDexAnnotationEnum: {
559 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
560 if (result_style == DexFile::kAllRaw) {
561 annotation_value->value_.SetI(index);
562 } else {
563 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700564 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000565 index,
566 hs.NewHandle(klass.GetDexCache()),
567 hs.NewHandle(klass.GetClassLoader()),
568 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700569 if (enum_field == nullptr) {
570 return false;
571 } else {
572 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
573 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
574 element_object = enum_field->GetObject(field_class.Get());
575 set_object = true;
576 }
577 }
578 break;
579 }
580 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800581 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700582 return false;
583 } else {
584 ScopedObjectAccessUnchecked soa(self);
585 StackHandleScope<2> hs(self);
586 uint32_t size = DecodeUnsignedLeb128(&annotation);
587 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
Vladimir Marko9b81ac32019-05-16 16:47:08 +0100588 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc(
David Sehr9323e6e2016-09-13 08:58:35 -0700589 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
590 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800591 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700592 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
593 return false;
594 }
595 DexFile::AnnotationValue new_annotation_value;
596 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800597 if (!ProcessAnnotationValue<kTransactionActive>(klass,
598 &annotation,
599 &new_annotation_value,
600 component_type,
601 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700602 return false;
603 }
604 if (!component_type->IsPrimitive()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100605 ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800606 new_array->AsObjectArray<mirror::Object>()->
607 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700608 } else {
609 switch (new_annotation_value.type_) {
610 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800611 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700612 i, new_annotation_value.value_.GetB());
613 break;
614 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800615 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700616 i, new_annotation_value.value_.GetS());
617 break;
618 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800619 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700620 i, new_annotation_value.value_.GetC());
621 break;
622 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800623 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700624 i, new_annotation_value.value_.GetI());
625 break;
626 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800627 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700628 i, new_annotation_value.value_.GetJ());
629 break;
630 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800631 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700632 i, new_annotation_value.value_.GetF());
633 break;
634 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800635 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700636 i, new_annotation_value.value_.GetD());
637 break;
638 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800639 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700640 i, new_annotation_value.value_.GetZ());
641 break;
642 default:
643 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
644 return false;
645 }
646 }
647 }
648 element_object = new_array.Get();
649 set_object = true;
650 width = 0;
651 }
652 break;
653 case DexFile::kDexAnnotationAnnotation:
654 if (result_style == DexFile::kAllRaw) {
655 return false;
656 }
657 element_object = ProcessEncodedAnnotation(klass, &annotation);
658 if (element_object == nullptr) {
659 return false;
660 }
661 set_object = true;
662 width = 0;
663 break;
664 case DexFile::kDexAnnotationNull:
665 if (result_style == DexFile::kAllRaw) {
666 annotation_value->value_.SetI(0);
667 } else {
668 CHECK(element_object == nullptr);
669 set_object = true;
670 }
671 width = 0;
672 break;
673 default:
674 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
675 return false;
676 }
677
678 annotation += width;
679 *annotation_ptr = annotation;
680
681 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100682 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700683 set_object = true;
684 }
685
686 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100687 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700688 }
689
690 return true;
691}
692
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100693ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
694 Handle<mirror::Class> annotation_class,
695 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000696 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700697 Thread* self = Thread::Current();
698 ScopedObjectAccessUnchecked soa(self);
699 StackHandleScope<5> hs(self);
700 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800701 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700702
703 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
704 ArtMethod* annotation_method =
705 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
706 if (annotation_method == nullptr) {
707 return nullptr;
708 }
Vladimir Markob1baa732022-11-11 12:43:11 +0000709
710 Handle<mirror::String> string_name =
711 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name));
712 if (UNLIKELY(string_name == nullptr)) {
713 LOG(ERROR) << "Failed to allocate name for annotation member";
714 return nullptr;
715 }
716
717 Handle<mirror::Class> method_return = hs.NewHandle(annotation_method->ResolveReturnType());
718 if (UNLIKELY(method_return == nullptr)) {
719 LOG(ERROR) << "Failed to resolve method return type for annotation member";
720 return nullptr;
721 }
David Sehr9323e6e2016-09-13 08:58:35 -0700722
723 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800724 if (!ProcessAnnotationValue<false>(klass,
725 annotation,
726 &annotation_value,
727 method_return,
728 DexFile::kAllObjects)) {
Vladimir Markob1baa732022-11-11 12:43:11 +0000729 // TODO: Logging the error breaks run-test 005-annotations.
730 // LOG(ERROR) << "Failed to process annotation value for annotation member";
David Sehr9323e6e2016-09-13 08:58:35 -0700731 return nullptr;
732 }
Vladimir Markob1baa732022-11-11 12:43:11 +0000733 Handle<mirror::Object> value_object = hs.NewHandle(annotation_value.value_.GetL());
734
735 Handle<mirror::Method> method_object = hs.NewHandle((pointer_size == PointerSize::k64)
736 ? mirror::Method::CreateFromArtMethod<PointerSize::k64>(self, annotation_method)
737 : mirror::Method::CreateFromArtMethod<PointerSize::k32>(self, annotation_method));
738 if (UNLIKELY(method_object == nullptr)) {
739 LOG(ERROR) << "Failed to create method object for annotation member";
740 return nullptr;
741 }
David Sehr9323e6e2016-09-13 08:58:35 -0700742
Vladimir Markob1baa732022-11-11 12:43:11 +0000743 Handle<mirror::Object> new_member =
Vladimir Markob9c73f82022-11-22 15:18:43 +0000744 WellKnownClasses::libcore_reflect_AnnotationMember_init->NewObject<'L', 'L', 'L', 'L'>(
745 hs, self, string_name, value_object, method_return, method_object);
Vladimir Markob1baa732022-11-11 12:43:11 +0000746 if (new_member == nullptr) {
Vladimir Markob9c73f82022-11-22 15:18:43 +0000747 DCHECK(self->IsExceptionPending());
748 LOG(ERROR) << "Failed to create annotation member";
David Sehr9323e6e2016-09-13 08:58:35 -0700749 return nullptr;
750 }
751
752 return new_member.Get();
753}
754
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800755const AnnotationItem* GetAnnotationItemFromAnnotationSet(const ClassData& klass,
756 const AnnotationSetItem* annotation_set,
757 uint32_t visibility,
758 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700759 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000760 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700761 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800762 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700763 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
764 continue;
765 }
766 const uint8_t* annotation = annotation_item->annotation_;
767 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100768 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
769 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000770 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000771 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000772 dex::TypeIndex(type_index),
773 hs.NewHandle(klass.GetDexCache()),
774 hs.NewHandle(klass.GetClassLoader()));
775 if (resolved_class == nullptr) {
776 std::string temp;
777 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
778 klass.GetRealClass()->GetDescriptor(&temp), type_index);
779 CHECK(self->IsExceptionPending());
780 self->ClearException();
781 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700782 }
783 if (resolved_class == annotation_class.Get()) {
784 return annotation_item;
785 }
786 }
787
788 return nullptr;
789}
790
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800791ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(const ClassData& klass,
792 const AnnotationSetItem* annotation_set,
793 uint32_t visibility,
794 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700795 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800796 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000797 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700798 if (annotation_item == nullptr) {
799 return nullptr;
800 }
801 const uint8_t* annotation = annotation_item->annotation_;
802 return ProcessEncodedAnnotation(klass, &annotation);
803}
804
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100805ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800806 const AnnotationItem* annotation_item,
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100807 const char* annotation_name,
808 Handle<mirror::Class> array_class,
809 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700810 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000811 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700812 const uint8_t* annotation =
813 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
814 if (annotation == nullptr) {
815 return nullptr;
816 }
817 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800818 bool result = Runtime::Current()->IsActiveTransaction()
819 ? ProcessAnnotationValue<true>(klass,
820 &annotation,
821 &annotation_value,
822 array_class,
823 DexFile::kAllObjects)
824 : ProcessAnnotationValue<false>(klass,
825 &annotation,
826 &annotation_value,
827 array_class,
828 DexFile::kAllObjects);
829 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700830 return nullptr;
831 }
832 if (annotation_value.type_ != expected_type) {
833 return nullptr;
834 }
835 return annotation_value.value_.GetL();
836}
837
Sorin Bascaa0191bc2022-06-15 16:53:48 +0100838template<typename T>
839static inline ObjPtr<mirror::ObjectArray<T>> GetAnnotationArrayValue(
840 Handle<mirror::Class> klass,
841 const char* annotation_name,
842 const char* value_name)
843 REQUIRES_SHARED(Locks::mutator_lock_) {
844 ClassData data(klass);
845 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
846 if (annotation_set == nullptr) {
847 return nullptr;
848 }
849 const AnnotationItem* annotation_item =
850 SearchAnnotationSet(data.GetDexFile(), annotation_set, annotation_name,
851 DexFile::kDexVisibilitySystem);
852 if (annotation_item == nullptr) {
853 return nullptr;
854 }
855 StackHandleScope<1> hs(Thread::Current());
856 Handle<mirror::Class> class_array_class =
857 hs.NewHandle(GetClassRoot<mirror::ObjectArray<T>>());
858 DCHECK(class_array_class != nullptr);
859 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
860 annotation_item,
861 value_name,
862 class_array_class,
863 DexFile::kDexAnnotationArray);
864 if (obj == nullptr) {
865 return nullptr;
866 }
867 return obj->AsObjectArray<T>();
868}
869
Vladimir Markoacb906d2018-05-30 10:23:49 +0100870static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue(
871 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800872 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700873 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000874 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700875 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800876 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700877 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
878 DexFile::kDexVisibilitySystem);
879 if (annotation_item == nullptr) {
880 return nullptr;
881 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100882 Handle<mirror::Class> string_array_class =
883 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>());
884 DCHECK(string_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100885 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700886 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
887 DexFile::kDexAnnotationArray);
888 if (obj == nullptr) {
889 return nullptr;
890 }
891 return obj->AsObjectArray<mirror::String>();
892}
893
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800894ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(const ClassData& klass,
895 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700896 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000897 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800898 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700899 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
900 DexFile::kDexVisibilitySystem);
901 if (annotation_item == nullptr) {
902 return nullptr;
903 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100904 StackHandleScope<1> hs(Thread::Current());
905 Handle<mirror::Class> class_array_class =
906 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
907 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100908 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700909 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
910 DexFile::kDexAnnotationArray);
911 if (obj == nullptr) {
912 return nullptr;
913 }
914 return obj->AsObjectArray<mirror::Class>();
915}
916
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100917ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000918 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800919 const AnnotationSetItem* annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -0700920 uint32_t visibility)
921 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000922 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700923 Thread* self = Thread::Current();
David Sehr9323e6e2016-09-13 08:58:35 -0700924 StackHandleScope<2> hs(self);
925 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000926 WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700927 if (annotation_set == nullptr) {
928 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
929 }
930
931 uint32_t size = annotation_set->size_;
932 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
933 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800934 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700935 return nullptr;
936 }
937
938 uint32_t dest_index = 0;
939 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800940 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700941 // Note that we do not use IsVisibilityCompatible here because older code
942 // was correct for this case.
943 if (annotation_item->visibility_ != visibility) {
944 continue;
945 }
946 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100947 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700948 if (annotation_obj != nullptr) {
949 result->SetWithoutChecks<false>(dest_index, annotation_obj);
950 ++dest_index;
951 } else if (self->IsExceptionPending()) {
952 return nullptr;
953 }
954 }
955
956 if (dest_index == size) {
957 return result.Get();
958 }
959
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100960 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700961 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
962 if (trimmed_result == nullptr) {
963 return nullptr;
964 }
965
966 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100967 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700968 trimmed_result->SetWithoutChecks<false>(i, obj);
969 }
970
971 return trimmed_result;
972}
973
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100974ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000975 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800976 const AnnotationSetRefList* set_ref_list,
David Sehr9323e6e2016-09-13 08:58:35 -0700977 uint32_t size)
978 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000979 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700980 Thread* self = Thread::Current();
David Sehr9323e6e2016-09-13 08:58:35 -0700981 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700982 ObjPtr<mirror::Class> annotation_array_class =
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000983 WellKnownClasses::ToClass(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100984 ObjPtr<mirror::Class> annotation_array_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100985 Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700986 if (annotation_array_array_class == nullptr) {
987 return nullptr;
988 }
989 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
990 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800991 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700992 LOG(ERROR) << "Annotation set ref array allocation failed";
993 return nullptr;
994 }
995 for (uint32_t index = 0; index < size; ++index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800996 const AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
997 const AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100998 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
999 set_item,
1000 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001001 if (annotation_set == nullptr) {
1002 return nullptr;
1003 }
1004 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
1005 }
1006 return annotation_array_array.Get();
1007}
1008} // namespace
1009
1010namespace annotations {
1011
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001012ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1013 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001014 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001015 if (annotation_set == nullptr) {
1016 return nullptr;
1017 }
1018 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001019 const ClassData field_class(hs, field);
1020 return GetAnnotationObjectFromAnnotationSet(field_class,
1021 annotation_set,
1022 DexFile::kDexVisibilityRuntime,
1023 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001024}
1025
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001026ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001027 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001028 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001029 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001030 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1031}
1032
Vladimir Markoacb906d2018-05-30 10:23:49 +01001033ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001034 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001035 if (annotation_set == nullptr) {
1036 return nullptr;
1037 }
1038 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001039 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001040 return GetSignatureValue(field_class, annotation_set);
1041}
1042
1043bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001044 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001045 if (annotation_set == nullptr) {
1046 return false;
1047 }
1048 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001049 const ClassData field_class(hs, field);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001050 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
David Sehr9323e6e2016-09-13 08:58:35 -07001051 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1052 return annotation_item != nullptr;
1053}
1054
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001055ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001056 const ClassData klass(method);
1057 const DexFile* dex_file = &klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001058 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001059 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001060 if (annotations_dir == nullptr) {
1061 return nullptr;
1062 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001063 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001064 dex_file->GetClassAnnotationSet(annotations_dir);
1065 if (annotation_set == nullptr) {
1066 return nullptr;
1067 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001068 const AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001069 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1070 if (annotation_item == nullptr) {
1071 return nullptr;
1072 }
1073 const uint8_t* annotation =
1074 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1075 if (annotation == nullptr) {
1076 return nullptr;
1077 }
1078 uint8_t header_byte = *(annotation++);
1079 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1080 return nullptr;
1081 }
1082 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1083 if (annotation == nullptr) {
1084 return nullptr;
1085 }
1086 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001087 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001088 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001089 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001090 &annotation,
1091 &annotation_value,
1092 return_type,
1093 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001094 return nullptr;
1095 }
1096 return annotation_value.value_.GetL();
1097}
1098
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001099ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1100 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001101 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001102 if (annotation_set == nullptr) {
1103 return nullptr;
1104 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001105 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001106 DexFile::kDexVisibilityRuntime, annotation_class);
1107}
1108
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001109ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001110 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001111 return ProcessAnnotationSet(ClassData(method),
1112 annotation_set,
1113 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001114}
1115
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001116ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001117 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001118 if (annotation_set == nullptr) {
1119 return nullptr;
1120 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001121 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001122}
1123
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001124ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001125 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001126 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -07001127 FindAnnotationsItemForMethod(method);
1128 if (parameter_annotations == nullptr) {
1129 return nullptr;
1130 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001131 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001132 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1133 if (set_ref_list == nullptr) {
1134 return nullptr;
1135 }
1136 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001137 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001138}
1139
Orion Hodson58143d22018-02-20 08:44:20 +00001140uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1141 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001142 const ParameterAnnotationsItem* parameter_annotations =
Orion Hodson58143d22018-02-20 08:44:20 +00001143 FindAnnotationsItemForMethod(method);
1144 if (parameter_annotations == nullptr) {
1145 return 0u;
1146 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001147 const AnnotationSetRefList* set_ref_list =
Orion Hodson58143d22018-02-20 08:44:20 +00001148 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1149 if (set_ref_list == nullptr) {
1150 return 0u;
1151 }
1152 return set_ref_list->size_;
1153}
1154
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001155ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1156 uint32_t parameter_idx,
1157 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001158 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001159 const ParameterAnnotationsItem* parameter_annotations = FindAnnotationsItemForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001160 if (parameter_annotations == nullptr) {
1161 return nullptr;
1162 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001163 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001164 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1165 if (set_ref_list == nullptr) {
1166 return nullptr;
1167 }
1168 if (parameter_idx >= set_ref_list->size_) {
1169 return nullptr;
1170 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001171 const AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1172 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001173 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001174 if (annotation_set == nullptr) {
1175 return nullptr;
1176 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001177 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001178 annotation_set,
1179 DexFile::kDexVisibilityRuntime,
1180 annotation_class);
1181}
1182
Vladimir Markoacb906d2018-05-30 10:23:49 +01001183bool GetParametersMetadataForMethod(
1184 ArtMethod* method,
1185 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1186 /*out*/ MutableHandle<mirror::IntArray>* access_flags) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001187 const AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001188 FindAnnotationSetForMethod(method);
1189 if (annotation_set == nullptr) {
1190 return false;
1191 }
1192
1193 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001194 const AnnotationItem* annotation_item =
Neil Fuller79a21e72016-09-09 14:24:51 +01001195 SearchAnnotationSet(*dex_file,
1196 annotation_set,
1197 "Ldalvik/annotation/MethodParameters;",
1198 DexFile::kDexVisibilitySystem);
1199 if (annotation_item == nullptr) {
1200 return false;
1201 }
1202
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001203 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001204
1205 // Extract the parameters' names String[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001206 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1207 Handle<mirror::Class> string_array_class =
1208 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker));
1209 DCHECK(string_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001210
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001211 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001212 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001213 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001214 annotation_item,
1215 "names",
1216 string_array_class,
1217 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001218 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001219 return false;
1220 }
1221
1222 // Extract the parameters' access flags int[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001223 Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker)));
1224 DCHECK(int_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001225 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001226 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001227 annotation_item,
1228 "accessFlags",
1229 int_array_class,
1230 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001231 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001232 return false;
1233 }
1234
Vladimir Markoacb906d2018-05-30 10:23:49 +01001235 names->Assign(names_obj->AsObjectArray<mirror::String>());
1236 access_flags->Assign(access_flags_obj->AsIntArray());
Neil Fuller79a21e72016-09-09 14:24:51 +01001237 return true;
1238}
1239
Vladimir Markoacb906d2018-05-30 10:23:49 +01001240ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001241 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001242 if (annotation_set == nullptr) {
1243 return nullptr;
1244 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001245 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001246}
1247
Roland Levillain35e42f02017-06-26 18:14:39 +01001248bool IsMethodAnnotationPresent(ArtMethod* method,
1249 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001250 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001251 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001252 if (annotation_set == nullptr) {
1253 return false;
1254 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001255 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +00001256 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001257 return annotation_item != nullptr;
1258}
1259
Vladimir Marko0db16e02017-11-08 14:32:33 +00001260static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1261 if (kIsDebugBuild) {
1262 ScopedObjectAccess soa(Thread::Current());
1263 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1264 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001265 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1266 if (klass != nullptr) {
1267 // Lookup using the boot class path loader should yield the annotation class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001268 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr));
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001269 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001270 }
1271}
1272
1273// Check whether a method from the `dex_file` with the given `annotation_set`
1274// is annotated with `annotation_descriptor` with build visibility.
1275static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001276 const AnnotationSetItem& annotation_set,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001277 const char* annotation_descriptor,
1278 jclass annotation_class) {
1279 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001280 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
Vladimir Marko0db16e02017-11-08 14:32:33 +00001281 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1282 continue;
1283 }
1284 const uint8_t* annotation = annotation_item->annotation_;
1285 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1286 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1287 if (strcmp(descriptor, annotation_descriptor) == 0) {
1288 DCheckNativeAnnotation(descriptor, annotation_class);
1289 return true;
1290 }
1291 }
1292 return false;
1293}
1294
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001295uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001296 const dex::ClassDef& class_def,
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001297 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001298 const dex::AnnotationSetItem* annotation_set =
Vladimir Marko0db16e02017-11-08 14:32:33 +00001299 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001300 if (annotation_set == nullptr) {
1301 return 0u;
1302 }
1303 uint32_t access_flags = 0u;
1304 if (IsMethodBuildAnnotationPresent(
1305 dex_file,
1306 *annotation_set,
1307 "Ldalvik/annotation/optimization/FastNative;",
1308 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1309 access_flags |= kAccFastNative;
1310 }
1311 if (IsMethodBuildAnnotationPresent(
1312 dex_file,
1313 *annotation_set,
1314 "Ldalvik/annotation/optimization/CriticalNative;",
1315 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1316 access_flags |= kAccCriticalNative;
1317 }
1318 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1319 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001320}
1321
Kevin Jeon55737562021-11-19 20:39:30 +00001322bool MethodIsNeverCompile(const DexFile& dex_file,
1323 const dex::ClassDef& class_def,
1324 uint32_t method_index) {
1325 const dex::AnnotationSetItem* annotation_set =
1326 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1327 if (annotation_set == nullptr) {
1328 return false;
1329 }
1330 return IsMethodBuildAnnotationPresent(
1331 dex_file,
1332 *annotation_set,
1333 "Ldalvik/annotation/optimization/NeverCompile;",
1334 WellKnownClasses::dalvik_annotation_optimization_NeverCompile);
1335}
1336
Santiago Aboy Solanes2979d532022-09-29 18:03:13 +01001337bool MethodIsNeverInline(const DexFile& dex_file,
1338 const dex::ClassDef& class_def,
1339 uint32_t method_index) {
1340 const dex::AnnotationSetItem* annotation_set =
1341 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1342 if (annotation_set == nullptr) {
1343 return false;
1344 }
1345 return IsMethodBuildAnnotationPresent(
1346 dex_file,
1347 *annotation_set,
1348 "Ldalvik/annotation/optimization/NeverInline;",
1349 WellKnownClasses::dalvik_annotation_optimization_NeverInline);
1350}
Kevin Jeon55737562021-11-19 20:39:30 +00001351
Hans Boehm206348c2018-12-05 11:11:33 -08001352bool FieldIsReachabilitySensitive(const DexFile& dex_file,
1353 const dex::ClassDef& class_def,
1354 uint32_t field_index)
1355 REQUIRES_SHARED(Locks::mutator_lock_) {
1356 const AnnotationSetItem* annotation_set =
1357 FindAnnotationSetForField(dex_file, class_def, field_index);
1358 if (annotation_set == nullptr) {
1359 return false;
1360 }
1361 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1362 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1363 // TODO: We're missing the equivalent of DCheckNativeAnnotation (not a DCHECK). Does it matter?
1364 return annotation_item != nullptr;
1365}
1366
1367bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1368 const dex::ClassDef& class_def,
1369 uint32_t method_index)
1370 REQUIRES_SHARED(Locks::mutator_lock_) {
1371 const AnnotationSetItem* annotation_set =
1372 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1373 if (annotation_set == nullptr) {
1374 return false;
1375 }
1376 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1377 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1378 return annotation_item != nullptr;
1379}
1380
1381static bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1382 uint32_t method_index)
1383 REQUIRES_SHARED(Locks::mutator_lock_) {
1384 DCHECK(method_index < dex_file.NumMethodIds());
1385 const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
1386 dex::TypeIndex class_index = method_id.class_idx_;
1387 const dex::ClassDef * class_def = dex_file.FindClassDef(class_index);
1388 return class_def != nullptr
1389 && MethodIsReachabilitySensitive(dex_file, *class_def, method_index);
1390}
1391
1392bool MethodContainsRSensitiveAccess(const DexFile& dex_file,
1393 const dex::ClassDef& class_def,
1394 uint32_t method_index)
1395 REQUIRES_SHARED(Locks::mutator_lock_) {
1396 // TODO: This is too slow to run very regularly. Currently this is only invoked in the
1397 // presence of @DeadReferenceSafe, which will be rare. In the long run, we need to quickly
1398 // check once whether a class has any @ReachabilitySensitive annotations. If not, we can
1399 // immediately return false here for any method in that class.
1400 uint32_t code_item_offset = dex_file.FindCodeItemOffset(class_def, method_index);
1401 const dex::CodeItem* code_item = dex_file.GetCodeItem(code_item_offset);
1402 CodeItemInstructionAccessor accessor(dex_file, code_item);
1403 if (!accessor.HasCodeItem()) {
1404 return false;
1405 }
Hans Boehm206348c2018-12-05 11:11:33 -08001406 for (DexInstructionIterator iter = accessor.begin(); iter != accessor.end(); ++iter) {
1407 switch (iter->Opcode()) {
1408 case Instruction::IGET:
Hans Boehm206348c2018-12-05 11:11:33 -08001409 case Instruction::IGET_WIDE:
Hans Boehm206348c2018-12-05 11:11:33 -08001410 case Instruction::IGET_OBJECT:
Hans Boehm206348c2018-12-05 11:11:33 -08001411 case Instruction::IGET_BOOLEAN:
Hans Boehm206348c2018-12-05 11:11:33 -08001412 case Instruction::IGET_BYTE:
Hans Boehm206348c2018-12-05 11:11:33 -08001413 case Instruction::IGET_CHAR:
Hans Boehm206348c2018-12-05 11:11:33 -08001414 case Instruction::IGET_SHORT:
Hans Boehm206348c2018-12-05 11:11:33 -08001415 case Instruction::IPUT:
Hans Boehm206348c2018-12-05 11:11:33 -08001416 case Instruction::IPUT_WIDE:
Hans Boehm206348c2018-12-05 11:11:33 -08001417 case Instruction::IPUT_OBJECT:
Hans Boehm206348c2018-12-05 11:11:33 -08001418 case Instruction::IPUT_BOOLEAN:
Hans Boehm206348c2018-12-05 11:11:33 -08001419 case Instruction::IPUT_BYTE:
Hans Boehm206348c2018-12-05 11:11:33 -08001420 case Instruction::IPUT_CHAR:
Hans Boehm206348c2018-12-05 11:11:33 -08001421 case Instruction::IPUT_SHORT:
Hans Boehm206348c2018-12-05 11:11:33 -08001422 {
Nicolas Geoffray4924ea92021-03-23 08:25:31 +00001423 uint32_t field_index = iter->VRegC_22c();
Hans Boehm206348c2018-12-05 11:11:33 -08001424 DCHECK(field_index < dex_file.NumFieldIds());
1425 // We only guarantee to pay attention to the annotation if it's in the same class,
1426 // or a containing class, but it's OK to do so in other cases.
1427 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
1428 dex::TypeIndex class_index = field_id.class_idx_;
1429 const dex::ClassDef * field_class_def = dex_file.FindClassDef(class_index);
1430 // We do not handle the case in which the field is declared in a superclass, and
1431 // don't claim to do so. The annotated field should normally be private.
1432 if (field_class_def != nullptr
1433 && FieldIsReachabilitySensitive(dex_file, *field_class_def, field_index)) {
1434 return true;
1435 }
1436 }
1437 break;
1438 case Instruction::INVOKE_SUPER:
1439 // Cannot call method in same class. TODO: Try an explicit superclass lookup for
1440 // better "best effort"?
1441 break;
1442 case Instruction::INVOKE_INTERFACE:
1443 // We handle an interface call just like a virtual call. We will find annotations
1444 // on interface methods/fields visible to us, but not of the annotation is in a
1445 // super-interface. Again, we could just ignore it.
1446 case Instruction::INVOKE_VIRTUAL:
1447 case Instruction::INVOKE_DIRECT:
1448 {
1449 uint32_t called_method_index = iter->VRegB_35c();
1450 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1451 return true;
1452 }
1453 }
1454 break;
1455 case Instruction::INVOKE_INTERFACE_RANGE:
1456 case Instruction::INVOKE_VIRTUAL_RANGE:
1457 case Instruction::INVOKE_DIRECT_RANGE:
1458 {
1459 uint32_t called_method_index = iter->VRegB_3rc();
1460 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1461 return true;
1462 }
1463 }
1464 break;
Hans Boehm206348c2018-12-05 11:11:33 -08001465 // We explicitly do not handle indirect ReachabilitySensitive accesses through VarHandles,
1466 // etc. Thus we ignore INVOKE_CUSTOM / INVOKE_CUSTOM_RANGE / INVOKE_POLYMORPHIC /
1467 // INVOKE_POLYMORPHIC_RANGE.
1468 default:
1469 // There is no way to add an annotation to array elements, and so far we've encountered no
1470 // need for that, so we ignore AGET and APUT.
1471 // It's impractical or impossible to garbage collect a class while one of its methods is
1472 // on the call stack. We allow ReachabilitySensitive annotations on static methods and
1473 // fields, but they can be safely ignored.
1474 break;
1475 }
Hans Boehm206348c2018-12-05 11:11:33 -08001476 }
1477 return false;
1478}
1479
1480bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file,
1481 const dex::ClassDef& class_def)
1482 // TODO: This should check outer classes as well.
1483 // It's conservatively correct not to do so.
1484 REQUIRES_SHARED(Locks::mutator_lock_) {
1485 const AnnotationsDirectoryItem* annotations_dir =
1486 dex_file.GetAnnotationsDirectory(class_def);
1487 if (annotations_dir == nullptr) {
1488 return false;
1489 }
1490 const AnnotationSetItem* annotation_set = dex_file.GetClassAnnotationSet(annotations_dir);
1491 if (annotation_set == nullptr) {
1492 return false;
1493 }
1494 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1495 "Ldalvik/annotation/optimization/DeadReferenceSafe;", DexFile::kDexVisibilityRuntime);
1496 return annotation_item != nullptr;
1497}
1498
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001499ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1500 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001501 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001502 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001503 if (annotation_set == nullptr) {
1504 return nullptr;
1505 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001506 return GetAnnotationObjectFromAnnotationSet(data,
1507 annotation_set,
1508 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001509 annotation_class);
1510}
1511
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001512ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001513 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001514 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001515 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001516}
1517
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001518ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Sorin Bascaa0191bc2022-06-15 16:53:48 +01001519 return GetAnnotationArrayValue<mirror::Class>(klass,
1520 "Ldalvik/annotation/MemberClasses;",
1521 "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001522}
1523
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001524ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001525 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001526 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001527 if (annotation_set == nullptr) {
1528 return nullptr;
1529 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001530 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001531 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001532 DexFile::kDexVisibilitySystem);
1533 if (annotation_item == nullptr) {
1534 return nullptr;
1535 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001536 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1537 annotation_item,
1538 "value",
1539 ScopedNullHandle<mirror::Class>(),
1540 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001541 if (obj == nullptr) {
1542 return nullptr;
1543 }
Nicolas Geoffrayf740be5e2021-09-16 14:07:37 +01001544 if (!obj->IsClass()) {
1545 // TypeNotPresentException, throw the NoClassDefFoundError.
1546 Thread::Current()->SetException(obj->AsThrowable()->GetCause());
1547 return nullptr;
1548 }
David Sehr9323e6e2016-09-13 08:58:35 -07001549 return obj->AsClass();
1550}
1551
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001552ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1553 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
Nicolas Geoffrayf740be5e2021-09-16 14:07:37 +01001554 if (declaring_class != nullptr || Thread::Current()->IsExceptionPending()) {
David Sehr9323e6e2016-09-13 08:58:35 -07001555 return declaring_class;
1556 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001557 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001558 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001559 if (annotation_set == nullptr) {
1560 return nullptr;
1561 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001562 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001563 SearchAnnotationSet(data.GetDexFile(),
1564 annotation_set,
1565 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001566 DexFile::kDexVisibilitySystem);
1567 if (annotation_item == nullptr) {
1568 return nullptr;
1569 }
1570 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001571 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001572 if (annotation == nullptr) {
1573 return nullptr;
1574 }
1575 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001576 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001577 &annotation,
1578 &annotation_value,
1579 ScopedNullHandle<mirror::Class>(),
1580 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001581 return nullptr;
1582 }
1583 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1584 return nullptr;
1585 }
1586 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001587 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001588 annotation_value.value_.GetI(),
1589 hs.NewHandle(data.GetDexCache()),
1590 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001591 if (method == nullptr) {
1592 return nullptr;
1593 }
1594 return method->GetDeclaringClass();
1595}
1596
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001597ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001598 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001599 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001600 if (annotation_set == nullptr) {
1601 return nullptr;
1602 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001603 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001604 SearchAnnotationSet(data.GetDexFile(),
1605 annotation_set,
1606 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001607 DexFile::kDexVisibilitySystem);
1608 if (annotation_item == nullptr) {
1609 return nullptr;
1610 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001611 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001612 DexFile::kDexAnnotationMethod);
1613}
1614
Vladimir Markoacb906d2018-05-30 10:23:49 +01001615bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001616 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001617 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001618 if (annotation_set == nullptr) {
1619 return false;
1620 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001621 const AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001622 data.GetDexFile(),
1623 annotation_set,
1624 "Ldalvik/annotation/InnerClass;",
1625 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001626 if (annotation_item == nullptr) {
1627 return false;
1628 }
1629 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001630 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001631 if (annotation == nullptr) {
1632 return false;
1633 }
1634 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001635 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001636 &annotation,
1637 &annotation_value,
1638 ScopedNullHandle<mirror::Class>(),
1639 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001640 return false;
1641 }
1642 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1643 annotation_value.type_ != DexFile::kDexAnnotationString) {
1644 return false;
1645 }
1646 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1647 return true;
1648}
1649
1650bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001651 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001652 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001653 if (annotation_set == nullptr) {
1654 return false;
1655 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001656 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001657 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001658 DexFile::kDexVisibilitySystem);
1659 if (annotation_item == nullptr) {
1660 return false;
1661 }
1662 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001663 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001664 if (annotation == nullptr) {
1665 return false;
1666 }
1667 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001668 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001669 &annotation,
1670 &annotation_value,
1671 ScopedNullHandle<mirror::Class>(),
1672 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001673 return false;
1674 }
1675 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1676 return false;
1677 }
1678 *flags = annotation_value.value_.GetI();
1679 return true;
1680}
1681
Vladimir Markoacb906d2018-05-30 10:23:49 +01001682ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
1683 Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001684 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001685 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001686 if (annotation_set == nullptr) {
1687 return nullptr;
1688 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001689 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001690}
1691
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001692const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001693 // Before instantiating ClassData, check that klass has a DexCache
1694 // assigned. The ClassData constructor indirectly dereferences it
1695 // when calling klass->GetDexFile().
1696 if (klass->GetDexCache() == nullptr) {
1697 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1698 return nullptr;
1699 }
1700
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001701 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001702 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001703 if (annotation_set == nullptr) {
1704 return nullptr;
1705 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001706
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001707 const AnnotationItem* annotation_item = SearchAnnotationSet(
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001708 data.GetDexFile(),
1709 annotation_set,
1710 "Ldalvik/annotation/SourceDebugExtension;",
1711 DexFile::kDexVisibilitySystem);
1712 if (annotation_item == nullptr) {
1713 return nullptr;
1714 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001715
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001716 const uint8_t* annotation =
1717 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1718 if (annotation == nullptr) {
1719 return nullptr;
1720 }
1721 DexFile::AnnotationValue annotation_value;
1722 if (!ProcessAnnotationValue<false>(data,
1723 &annotation,
1724 &annotation_value,
1725 ScopedNullHandle<mirror::Class>(),
1726 DexFile::kAllRaw)) {
1727 return nullptr;
1728 }
1729 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1730 return nullptr;
1731 }
1732 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1733 return data.GetDexFile().StringDataByIdx(index);
1734}
1735
Sorin Basca08b3f872022-05-31 17:04:55 +01001736ObjPtr<mirror::Class> GetNestHost(Handle<mirror::Class> klass) {
1737 ClassData data(klass);
1738 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1739 if (annotation_set == nullptr) {
1740 return nullptr;
1741 }
1742 const AnnotationItem* annotation_item =
1743 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/NestHost;",
1744 DexFile::kDexVisibilitySystem);
1745 if (annotation_item == nullptr) {
1746 return nullptr;
1747 }
1748 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1749 annotation_item,
1750 "host",
1751 ScopedNullHandle<mirror::Class>(),
1752 DexFile::kDexAnnotationType);
1753 if (obj == nullptr) {
1754 return nullptr;
1755 }
1756 if (!obj->IsClass()) {
1757 // TypeNotPresentException, throw the NoClassDefFoundError.
1758 Thread::Current()->SetException(obj->AsThrowable()->GetCause());
1759 return nullptr;
1760 }
1761 return obj->AsClass();
1762}
1763
1764ObjPtr<mirror::ObjectArray<mirror::Class>> GetNestMembers(Handle<mirror::Class> klass) {
Sorin Bascaa0191bc2022-06-15 16:53:48 +01001765 return GetAnnotationArrayValue<mirror::Class>(klass,
1766 "Ldalvik/annotation/NestMembers;",
1767 "classes");
1768}
1769
1770ObjPtr<mirror::ObjectArray<mirror::Class>> GetPermittedSubclasses(Handle<mirror::Class> klass) {
1771 return GetAnnotationArrayValue<mirror::Class>(klass,
1772 "Ldalvik/annotation/PermittedSubclasses;",
1773 "value");
Sorin Basca08b3f872022-05-31 17:04:55 +01001774}
1775
David Sehr9323e6e2016-09-13 08:58:35 -07001776bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001777 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001778 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001779 if (annotation_set == nullptr) {
1780 return false;
1781 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001782 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001783 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001784 return annotation_item != nullptr;
1785}
1786
1787int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1788 // For native method, lineno should be -2 to indicate it is native. Note that
1789 // "line number == -2" is how libcore tells from StackTraceElement.
Nicolas Geoffray47171752020-08-31 15:03:20 +01001790 if (!method->HasCodeItem()) {
David Sehr9323e6e2016-09-13 08:58:35 -07001791 return -2;
1792 }
1793
David Sehr0225f8e2018-01-31 08:52:24 +00001794 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001795 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001796
1797 // A method with no line number info should return -1
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001798 uint32_t line_num = -1;
1799 accessor.GetLineNumForPc(rel_pc, &line_num);
1800 return line_num;
David Sehr9323e6e2016-09-13 08:58:35 -07001801}
1802
1803template<bool kTransactionActive>
1804void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1805 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001806 switch (type_) {
1807 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1808 break;
1809 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1810 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1811 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1812 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1813 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1814 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1815 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1816 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1817 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001818 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001819 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001820 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1821 break;
1822 }
1823 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001824 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001825 dex_cache_,
1826 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001827 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1828 break;
1829 }
1830 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1831 }
1832}
1833template
1834void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1835template
1836void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1837
1838} // namespace annotations
1839
1840} // namespace art