blob: 95b42d27d28090b0d320bb93cc13d3bff1f80561 [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"
24#include "art_method-inl.h"
25#include "class_linker-inl.h"
David Sehr334b9d72018-02-12 18:27:56 -080026#include "dex/dex_file-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010027#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070028#include "jvalue-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070029#include "mirror/field.h"
30#include "mirror/method.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000031#include "oat_file.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010032#include "obj_ptr-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070033#include "reflection.h"
34#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070035#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070036
37namespace art {
38
Andreas Gampe46ee31b2016-12-14 10:11:49 -080039using android::base::StringPrintf;
40
David Sehr9323e6e2016-09-13 08:58:35 -070041struct DexFile::AnnotationValue {
42 JValue value_;
43 uint8_t type_;
44};
45
46namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000047
48// A helper class that contains all the data needed to do annotation lookup.
49class ClassData {
50 public:
51 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
52 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
53 method,
54 *method->GetDexFile(),
55 &method->GetClassDef()) {}
56
57 // Requires Scope to be able to create at least 1 handles.
58 template <typename Scope>
59 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
60 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
61
62 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
63 : ClassData(klass, // klass
64 nullptr, // method
65 klass->GetDexFile(),
66 klass->GetClassDef()) {}
67
68 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
69 return dex_file_;
70 }
71
72 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
73 return class_def_;
74 }
75
76 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
77 if (method_ != nullptr) {
78 return method_->GetDexCache();
79 } else {
80 return real_klass_->GetDexCache();
81 }
82 }
83
84 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
85 if (method_ != nullptr) {
86 return method_->GetDeclaringClass()->GetClassLoader();
87 } else {
88 return real_klass_->GetClassLoader();
89 }
90 }
91
92 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
93 if (method_ != nullptr) {
94 return method_->GetDeclaringClass();
95 } else {
96 return real_klass_.Get();
97 }
98 }
99
100 private:
101 ClassData(Handle<mirror::Class> klass,
102 ArtMethod* method,
103 const DexFile& dex_file,
104 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
105 : real_klass_(klass),
106 method_(method),
107 dex_file_(dex_file),
108 class_def_(class_def) {
109 DCHECK((method_ == nullptr) || real_klass_.IsNull());
110 }
111
112 Handle<mirror::Class> real_klass_;
113 ArtMethod* method_;
114 const DexFile& dex_file_;
115 const DexFile::ClassDef* class_def_;
116
117 DISALLOW_COPY_AND_ASSIGN(ClassData);
118};
119
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100120ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
121 Handle<mirror::Class> annotation_class,
122 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700123 REQUIRES_SHARED(Locks::mutator_lock_);
124
125bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
126 if (expected == DexFile::kDexVisibilityRuntime) {
127 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
128 if (sdk_version > 0 && sdk_version <= 23) {
129 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
130 }
131 }
132 return actual == expected;
133}
134
135const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
136 REQUIRES_SHARED(Locks::mutator_lock_) {
137 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700138 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Alex Light89f33b82017-10-23 16:37:03 -0700139 const DexFile::ClassDef* class_def = klass->GetClassDef();
140 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700141 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700142 return nullptr;
143 }
David Sehr9323e6e2016-09-13 08:58:35 -0700144 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700145 dex_file->GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700146 if (annotations_dir == nullptr) {
147 return nullptr;
148 }
149 const DexFile::FieldAnnotationsItem* field_annotations =
150 dex_file->GetFieldAnnotations(annotations_dir);
151 if (field_annotations == nullptr) {
152 return nullptr;
153 }
154 uint32_t field_index = field->GetDexFieldIndex();
155 uint32_t field_count = annotations_dir->fields_size_;
156 for (uint32_t i = 0; i < field_count; ++i) {
157 if (field_annotations[i].field_idx_ == field_index) {
158 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
159 }
160 }
161 return nullptr;
162}
163
164const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
165 const DexFile::AnnotationSetItem* annotation_set,
166 const char* descriptor,
167 uint32_t visibility)
168 REQUIRES_SHARED(Locks::mutator_lock_) {
169 const DexFile::AnnotationItem* result = nullptr;
170 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
171 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
172 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
173 continue;
174 }
175 const uint8_t* annotation = annotation_item->annotation_;
176 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
177
Andreas Gampea5b09a62016-11-17 15:21:22 -0800178 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700179 result = annotation_item;
180 break;
181 }
182 }
183 return result;
184}
185
186bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
187 REQUIRES_SHARED(Locks::mutator_lock_) {
188 const uint8_t* annotation = *annotation_ptr;
189 uint8_t header_byte = *(annotation++);
190 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
191 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
192 int32_t width = value_arg + 1;
193
194 switch (value_type) {
195 case DexFile::kDexAnnotationByte:
196 case DexFile::kDexAnnotationShort:
197 case DexFile::kDexAnnotationChar:
198 case DexFile::kDexAnnotationInt:
199 case DexFile::kDexAnnotationLong:
200 case DexFile::kDexAnnotationFloat:
201 case DexFile::kDexAnnotationDouble:
202 case DexFile::kDexAnnotationString:
203 case DexFile::kDexAnnotationType:
204 case DexFile::kDexAnnotationMethod:
205 case DexFile::kDexAnnotationField:
206 case DexFile::kDexAnnotationEnum:
207 break;
208 case DexFile::kDexAnnotationArray:
209 {
210 uint32_t size = DecodeUnsignedLeb128(&annotation);
211 while (size--) {
212 if (!SkipAnnotationValue(dex_file, &annotation)) {
213 return false;
214 }
215 }
216 width = 0;
217 break;
218 }
219 case DexFile::kDexAnnotationAnnotation:
220 {
221 DecodeUnsignedLeb128(&annotation); // unused type_index
222 uint32_t size = DecodeUnsignedLeb128(&annotation);
223 while (size--) {
224 DecodeUnsignedLeb128(&annotation); // unused element_name_index
225 if (!SkipAnnotationValue(dex_file, &annotation)) {
226 return false;
227 }
228 }
229 width = 0;
230 break;
231 }
232 case DexFile::kDexAnnotationBoolean:
233 case DexFile::kDexAnnotationNull:
234 width = 0;
235 break;
236 default:
237 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
238 return false;
239 }
240
241 annotation += width;
242 *annotation_ptr = annotation;
243 return true;
244}
245
246const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
247 const uint8_t* annotation,
248 const char* name)
249 REQUIRES_SHARED(Locks::mutator_lock_) {
250 DecodeUnsignedLeb128(&annotation); // unused type_index
251 uint32_t size = DecodeUnsignedLeb128(&annotation);
252
253 while (size != 0) {
254 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800255 const char* element_name =
256 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700257 if (strcmp(name, element_name) == 0) {
258 return annotation;
259 }
260 SkipAnnotationValue(dex_file, &annotation);
261 size--;
262 }
263 return nullptr;
264}
265
Vladimir Marko0db16e02017-11-08 14:32:33 +0000266const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
267 const DexFile::ClassDef& class_def,
268 uint32_t method_index) {
David Sehr9323e6e2016-09-13 08:58:35 -0700269 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000270 dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700271 if (annotations_dir == nullptr) {
272 return nullptr;
273 }
274 const DexFile::MethodAnnotationsItem* method_annotations =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000275 dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700276 if (method_annotations == nullptr) {
277 return nullptr;
278 }
David Sehr9323e6e2016-09-13 08:58:35 -0700279 uint32_t method_count = annotations_dir->methods_size_;
280 for (uint32_t i = 0; i < method_count; ++i) {
281 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000282 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700283 }
284 }
285 return nullptr;
286}
287
Vladimir Marko0db16e02017-11-08 14:32:33 +0000288inline const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
289 REQUIRES_SHARED(Locks::mutator_lock_) {
290 if (method->IsProxyMethod()) {
291 return nullptr;
292 }
293 return FindAnnotationSetForMethod(*method->GetDexFile(),
294 method->GetClassDef(),
295 method->GetDexMethodIndex());
296}
297
David Sehr9323e6e2016-09-13 08:58:35 -0700298const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
299 REQUIRES_SHARED(Locks::mutator_lock_) {
300 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700301 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000302 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700303 if (annotations_dir == nullptr) {
304 return nullptr;
305 }
306 const DexFile::ParameterAnnotationsItem* parameter_annotations =
307 dex_file->GetParameterAnnotations(annotations_dir);
308 if (parameter_annotations == nullptr) {
309 return nullptr;
310 }
311 uint32_t method_index = method->GetDexMethodIndex();
312 uint32_t parameter_count = annotations_dir->parameters_size_;
313 for (uint32_t i = 0; i < parameter_count; ++i) {
314 if (parameter_annotations[i].method_idx_ == method_index) {
315 return &parameter_annotations[i];
316 }
317 }
318 return nullptr;
319}
320
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000321const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700322 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000323 const DexFile& dex_file = klass.GetDexFile();
Alex Light89f33b82017-10-23 16:37:03 -0700324 const DexFile::ClassDef* class_def = klass.GetClassDef();
325 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700326 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700327 return nullptr;
328 }
David Sehr9323e6e2016-09-13 08:58:35 -0700329 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700330 dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700331 if (annotations_dir == nullptr) {
332 return nullptr;
333 }
334 return dex_file.GetClassAnnotationSet(annotations_dir);
335}
336
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100337ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700338 REQUIRES_SHARED(Locks::mutator_lock_) {
339 uint32_t type_index = DecodeUnsignedLeb128(annotation);
340 uint32_t size = DecodeUnsignedLeb128(annotation);
341
342 Thread* self = Thread::Current();
343 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000344 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700345 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
346 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000347 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000348 hs.NewHandle(klass.GetDexCache()),
349 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800350 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000351 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
352 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700353 DCHECK(Thread::Current()->IsExceptionPending());
354 Thread::Current()->ClearException();
355 return nullptr;
356 }
357
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700358 ObjPtr<mirror::Class> annotation_member_class =
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100359 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember);
360 ObjPtr<mirror::Class> annotation_member_array_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700361 class_linker->FindArrayClass(self, &annotation_member_class);
362 if (annotation_member_array_class == nullptr) {
363 return nullptr;
364 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100365 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700366 if (size > 0) {
367 element_array =
368 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
369 if (element_array == nullptr) {
370 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
371 return nullptr;
372 }
373 }
374
375 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
376 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100377 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700378 if (new_member == nullptr) {
379 return nullptr;
380 }
381 h_element_array->SetWithoutChecks<false>(i, new_member);
382 }
383
384 JValue result;
385 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800386 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700387 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
388 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
389 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
390 if (self->IsExceptionPending()) {
391 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
392 return nullptr;
393 }
394
395 return result.GetL();
396}
397
Andreas Gampe9486a162017-02-16 15:17:47 -0800398template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000399bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700400 const uint8_t** annotation_ptr,
401 DexFile::AnnotationValue* annotation_value,
402 Handle<mirror::Class> array_class,
403 DexFile::AnnotationResultStyle result_style)
404 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000405 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700406 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700407 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700408 bool set_object = false;
409 Primitive::Type primitive_type = Primitive::kPrimVoid;
410 const uint8_t* annotation = *annotation_ptr;
411 uint8_t header_byte = *(annotation++);
412 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
413 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
414 int32_t width = value_arg + 1;
415 annotation_value->type_ = value_type;
416
417 switch (value_type) {
418 case DexFile::kDexAnnotationByte:
419 annotation_value->value_.SetB(
420 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
421 primitive_type = Primitive::kPrimByte;
422 break;
423 case DexFile::kDexAnnotationShort:
424 annotation_value->value_.SetS(
425 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
426 primitive_type = Primitive::kPrimShort;
427 break;
428 case DexFile::kDexAnnotationChar:
429 annotation_value->value_.SetC(
430 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
431 primitive_type = Primitive::kPrimChar;
432 break;
433 case DexFile::kDexAnnotationInt:
434 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
435 primitive_type = Primitive::kPrimInt;
436 break;
437 case DexFile::kDexAnnotationLong:
438 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
439 primitive_type = Primitive::kPrimLong;
440 break;
441 case DexFile::kDexAnnotationFloat:
442 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
443 primitive_type = Primitive::kPrimFloat;
444 break;
445 case DexFile::kDexAnnotationDouble:
446 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
447 primitive_type = Primitive::kPrimDouble;
448 break;
449 case DexFile::kDexAnnotationBoolean:
450 annotation_value->value_.SetZ(value_arg != 0);
451 primitive_type = Primitive::kPrimBoolean;
452 width = 0;
453 break;
454 case DexFile::kDexAnnotationString: {
455 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
456 if (result_style == DexFile::kAllRaw) {
457 annotation_value->value_.SetI(index);
458 } else {
459 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700460 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000461 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700462 set_object = true;
463 if (element_object == nullptr) {
464 return false;
465 }
466 }
467 break;
468 }
469 case DexFile::kDexAnnotationType: {
470 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
471 if (result_style == DexFile::kAllRaw) {
472 annotation_value->value_.SetI(index);
473 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800474 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000475 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700476 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000477 type_index,
478 hs.NewHandle(klass.GetDexCache()),
479 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700480 set_object = true;
481 if (element_object == nullptr) {
482 CHECK(self->IsExceptionPending());
483 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800484 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700485 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
486 element_object = self->GetException();
487 self->ClearException();
488 } else {
489 return false;
490 }
491 }
492 }
493 break;
494 }
495 case DexFile::kDexAnnotationMethod: {
496 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
497 if (result_style == DexFile::kAllRaw) {
498 annotation_value->value_.SetI(index);
499 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000500 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000501 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700502 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000503 index,
504 hs.NewHandle(klass.GetDexCache()),
505 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700506 if (method == nullptr) {
507 return false;
508 }
509 PointerSize pointer_size = class_linker->GetImagePointerSize();
510 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700511 if (method->IsConstructor()) {
512 if (pointer_size == PointerSize::k64) {
513 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800514 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700515 } else {
516 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800517 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700518 }
519 } else {
520 if (pointer_size == PointerSize::k64) {
521 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800522 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700523 } else {
524 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800525 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700526 }
527 }
528 if (element_object == nullptr) {
529 return false;
530 }
531 }
532 break;
533 }
534 case DexFile::kDexAnnotationField: {
535 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
536 if (result_style == DexFile::kAllRaw) {
537 annotation_value->value_.SetI(index);
538 } else {
539 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700540 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000541 index,
542 hs.NewHandle(klass.GetDexCache()),
543 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700544 if (field == nullptr) {
545 return false;
546 }
547 set_object = true;
548 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
549 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800550 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
551 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700552 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800553 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
554 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700555 }
556 if (element_object == nullptr) {
557 return false;
558 }
559 }
560 break;
561 }
562 case DexFile::kDexAnnotationEnum: {
563 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
564 if (result_style == DexFile::kAllRaw) {
565 annotation_value->value_.SetI(index);
566 } else {
567 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700568 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000569 index,
570 hs.NewHandle(klass.GetDexCache()),
571 hs.NewHandle(klass.GetClassLoader()),
572 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700573 if (enum_field == nullptr) {
574 return false;
575 } else {
576 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
577 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
578 element_object = enum_field->GetObject(field_class.Get());
579 set_object = true;
580 }
581 }
582 break;
583 }
584 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800585 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700586 return false;
587 } else {
588 ScopedObjectAccessUnchecked soa(self);
589 StackHandleScope<2> hs(self);
590 uint32_t size = DecodeUnsignedLeb128(&annotation);
591 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
592 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
593 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
594 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800595 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700596 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
597 return false;
598 }
599 DexFile::AnnotationValue new_annotation_value;
600 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800601 if (!ProcessAnnotationValue<kTransactionActive>(klass,
602 &annotation,
603 &new_annotation_value,
604 component_type,
605 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700606 return false;
607 }
608 if (!component_type->IsPrimitive()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100609 ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800610 new_array->AsObjectArray<mirror::Object>()->
611 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700612 } else {
613 switch (new_annotation_value.type_) {
614 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800615 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700616 i, new_annotation_value.value_.GetB());
617 break;
618 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800619 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700620 i, new_annotation_value.value_.GetS());
621 break;
622 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800623 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700624 i, new_annotation_value.value_.GetC());
625 break;
626 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800627 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700628 i, new_annotation_value.value_.GetI());
629 break;
630 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800631 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700632 i, new_annotation_value.value_.GetJ());
633 break;
634 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800635 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700636 i, new_annotation_value.value_.GetF());
637 break;
638 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800639 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700640 i, new_annotation_value.value_.GetD());
641 break;
642 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800643 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700644 i, new_annotation_value.value_.GetZ());
645 break;
646 default:
647 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
648 return false;
649 }
650 }
651 }
652 element_object = new_array.Get();
653 set_object = true;
654 width = 0;
655 }
656 break;
657 case DexFile::kDexAnnotationAnnotation:
658 if (result_style == DexFile::kAllRaw) {
659 return false;
660 }
661 element_object = ProcessEncodedAnnotation(klass, &annotation);
662 if (element_object == nullptr) {
663 return false;
664 }
665 set_object = true;
666 width = 0;
667 break;
668 case DexFile::kDexAnnotationNull:
669 if (result_style == DexFile::kAllRaw) {
670 annotation_value->value_.SetI(0);
671 } else {
672 CHECK(element_object == nullptr);
673 set_object = true;
674 }
675 width = 0;
676 break;
677 default:
678 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
679 return false;
680 }
681
682 annotation += width;
683 *annotation_ptr = annotation;
684
685 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100686 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700687 set_object = true;
688 }
689
690 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100691 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700692 }
693
694 return true;
695}
696
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100697ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
698 Handle<mirror::Class> annotation_class,
699 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000700 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700701 Thread* self = Thread::Current();
702 ScopedObjectAccessUnchecked soa(self);
703 StackHandleScope<5> hs(self);
704 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800705 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700706 Handle<mirror::String> string_name(
707 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
708
709 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
710 ArtMethod* annotation_method =
711 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
712 if (annotation_method == nullptr) {
713 return nullptr;
714 }
Vladimir Markob45528c2017-07-27 14:14:28 +0100715 Handle<mirror::Class> method_return(hs.NewHandle(annotation_method->ResolveReturnType()));
David Sehr9323e6e2016-09-13 08:58:35 -0700716
717 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800718 if (!ProcessAnnotationValue<false>(klass,
719 annotation,
720 &annotation_value,
721 method_return,
722 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700723 return nullptr;
724 }
725 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
726
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700727 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700728 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
729 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
730 mirror::Method* method_obj_ptr;
731 DCHECK(!Runtime::Current()->IsActiveTransaction());
732 if (pointer_size == PointerSize::k64) {
733 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
734 self, annotation_method);
735 } else {
736 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
737 self, annotation_method);
738 }
739 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
740
Andreas Gampefa4333d2017-02-14 11:10:34 -0800741 if (new_member == nullptr || string_name == nullptr ||
742 method_object == nullptr || method_return == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700743 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
744 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
745 return nullptr;
746 }
747
748 JValue result;
749 ArtMethod* annotation_member_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800750 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
David Sehr9323e6e2016-09-13 08:58:35 -0700751 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
752 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
753 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
754 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
755 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
756 };
757 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
758 if (self->IsExceptionPending()) {
759 LOG(INFO) << "Exception in AnnotationMember.<init>";
760 return nullptr;
761 }
762
763 return new_member.Get();
764}
765
766const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000767 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700768 const DexFile::AnnotationSetItem* annotation_set,
769 uint32_t visibility,
Vladimir Marko0db16e02017-11-08 14:32:33 +0000770 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700771 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000772 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700773 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
774 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
775 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
776 continue;
777 }
778 const uint8_t* annotation = annotation_item->annotation_;
779 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100780 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
781 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000782 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000783 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000784 dex::TypeIndex(type_index),
785 hs.NewHandle(klass.GetDexCache()),
786 hs.NewHandle(klass.GetClassLoader()));
787 if (resolved_class == nullptr) {
788 std::string temp;
789 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
790 klass.GetRealClass()->GetDescriptor(&temp), type_index);
791 CHECK(self->IsExceptionPending());
792 self->ClearException();
793 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700794 }
795 if (resolved_class == annotation_class.Get()) {
796 return annotation_item;
797 }
798 }
799
800 return nullptr;
801}
802
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100803ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000804 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700805 const DexFile::AnnotationSetItem* annotation_set,
806 uint32_t visibility,
807 Handle<mirror::Class> annotation_class)
808 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000809 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
810 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700811 if (annotation_item == nullptr) {
812 return nullptr;
813 }
814 const uint8_t* annotation = annotation_item->annotation_;
815 return ProcessEncodedAnnotation(klass, &annotation);
816}
817
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100818ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
819 const DexFile::AnnotationItem* annotation_item,
820 const char* annotation_name,
821 Handle<mirror::Class> array_class,
822 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700823 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000824 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700825 const uint8_t* annotation =
826 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
827 if (annotation == nullptr) {
828 return nullptr;
829 }
830 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800831 bool result = Runtime::Current()->IsActiveTransaction()
832 ? ProcessAnnotationValue<true>(klass,
833 &annotation,
834 &annotation_value,
835 array_class,
836 DexFile::kAllObjects)
837 : ProcessAnnotationValue<false>(klass,
838 &annotation,
839 &annotation_value,
840 array_class,
841 DexFile::kAllObjects);
842 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700843 return nullptr;
844 }
845 if (annotation_value.type_ != expected_type) {
846 return nullptr;
847 }
848 return annotation_value.value_.GetL();
849}
850
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000851mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700852 const DexFile::AnnotationSetItem* annotation_set)
853 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000854 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700855 StackHandleScope<1> hs(Thread::Current());
856 const DexFile::AnnotationItem* annotation_item =
857 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
858 DexFile::kDexVisibilitySystem);
859 if (annotation_item == nullptr) {
860 return nullptr;
861 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700862 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
David Sehr9323e6e2016-09-13 08:58:35 -0700863 Handle<mirror::Class> string_array_class(hs.NewHandle(
864 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800865 if (string_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700866 return nullptr;
867 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100868 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700869 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
870 DexFile::kDexAnnotationArray);
871 if (obj == nullptr) {
872 return nullptr;
873 }
874 return obj->AsObjectArray<mirror::String>();
875}
876
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100877ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(
878 const ClassData& klass,
879 const DexFile::AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700880 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000881 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700882 StackHandleScope<1> hs(Thread::Current());
883 const DexFile::AnnotationItem* annotation_item =
884 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
885 DexFile::kDexVisibilitySystem);
886 if (annotation_item == nullptr) {
887 return nullptr;
888 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700889 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -0700890 Handle<mirror::Class> class_array_class(hs.NewHandle(
891 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800892 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700893 return nullptr;
894 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100895 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700896 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
897 DexFile::kDexAnnotationArray);
898 if (obj == nullptr) {
899 return nullptr;
900 }
901 return obj->AsObjectArray<mirror::Class>();
902}
903
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100904ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000905 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700906 const DexFile::AnnotationSetItem* annotation_set,
907 uint32_t visibility)
908 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000909 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700910 Thread* self = Thread::Current();
911 ScopedObjectAccessUnchecked soa(self);
912 StackHandleScope<2> hs(self);
913 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700914 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700915 if (annotation_set == nullptr) {
916 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
917 }
918
919 uint32_t size = annotation_set->size_;
920 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
921 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800922 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700923 return nullptr;
924 }
925
926 uint32_t dest_index = 0;
927 for (uint32_t i = 0; i < size; ++i) {
928 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
929 // Note that we do not use IsVisibilityCompatible here because older code
930 // was correct for this case.
931 if (annotation_item->visibility_ != visibility) {
932 continue;
933 }
934 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100935 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700936 if (annotation_obj != nullptr) {
937 result->SetWithoutChecks<false>(dest_index, annotation_obj);
938 ++dest_index;
939 } else if (self->IsExceptionPending()) {
940 return nullptr;
941 }
942 }
943
944 if (dest_index == size) {
945 return result.Get();
946 }
947
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100948 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700949 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
950 if (trimmed_result == nullptr) {
951 return nullptr;
952 }
953
954 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100955 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700956 trimmed_result->SetWithoutChecks<false>(i, obj);
957 }
958
959 return trimmed_result;
960}
961
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100962ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000963 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700964 const DexFile::AnnotationSetRefList* set_ref_list,
965 uint32_t size)
966 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000967 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700968 Thread* self = Thread::Current();
969 ScopedObjectAccessUnchecked soa(self);
970 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700971 ObjPtr<mirror::Class> annotation_array_class =
972 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100973 ObjPtr<mirror::Class> annotation_array_array_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700974 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
975 if (annotation_array_array_class == nullptr) {
976 return nullptr;
977 }
978 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
979 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800980 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700981 LOG(ERROR) << "Annotation set ref array allocation failed";
982 return nullptr;
983 }
984 for (uint32_t index = 0; index < size; ++index) {
985 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
986 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100987 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
988 set_item,
989 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -0700990 if (annotation_set == nullptr) {
991 return nullptr;
992 }
993 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
994 }
995 return annotation_array_array.Get();
996}
997} // namespace
998
999namespace annotations {
1000
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001001ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1002 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001003 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1004 if (annotation_set == nullptr) {
1005 return nullptr;
1006 }
1007 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001008 const ClassData field_class(hs, field);
1009 return GetAnnotationObjectFromAnnotationSet(field_class,
1010 annotation_set,
1011 DexFile::kDexVisibilityRuntime,
1012 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001013}
1014
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001015ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
David Sehr9323e6e2016-09-13 08:58:35 -07001016 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1017 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001018 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001019 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1020}
1021
1022mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
1023 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1024 if (annotation_set == nullptr) {
1025 return nullptr;
1026 }
1027 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001028 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001029 return GetSignatureValue(field_class, annotation_set);
1030}
1031
1032bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1033 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1034 if (annotation_set == nullptr) {
1035 return false;
1036 }
1037 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001038 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001039 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1040 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1041 return annotation_item != nullptr;
1042}
1043
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001044ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001045 const ClassData klass(method);
1046 const DexFile* dex_file = &klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -07001047 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001048 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001049 if (annotations_dir == nullptr) {
1050 return nullptr;
1051 }
1052 const DexFile::AnnotationSetItem* annotation_set =
1053 dex_file->GetClassAnnotationSet(annotations_dir);
1054 if (annotation_set == nullptr) {
1055 return nullptr;
1056 }
1057 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1058 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1059 if (annotation_item == nullptr) {
1060 return nullptr;
1061 }
1062 const uint8_t* annotation =
1063 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1064 if (annotation == nullptr) {
1065 return nullptr;
1066 }
1067 uint8_t header_byte = *(annotation++);
1068 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1069 return nullptr;
1070 }
1071 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1072 if (annotation == nullptr) {
1073 return nullptr;
1074 }
1075 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001076 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001077 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001078 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001079 &annotation,
1080 &annotation_value,
1081 return_type,
1082 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001083 return nullptr;
1084 }
1085 return annotation_value.value_.GetL();
1086}
1087
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001088ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1089 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001090 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1091 if (annotation_set == nullptr) {
1092 return nullptr;
1093 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001094 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001095 DexFile::kDexVisibilityRuntime, annotation_class);
1096}
1097
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001098ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001099 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001100 return ProcessAnnotationSet(ClassData(method),
1101 annotation_set,
1102 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001103}
1104
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001105ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001106 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1107 if (annotation_set == nullptr) {
1108 return nullptr;
1109 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001110 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001111}
1112
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001113ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001114 const DexFile* dex_file = method->GetDexFile();
1115 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1116 FindAnnotationsItemForMethod(method);
1117 if (parameter_annotations == nullptr) {
1118 return nullptr;
1119 }
1120 const DexFile::AnnotationSetRefList* set_ref_list =
1121 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1122 if (set_ref_list == nullptr) {
1123 return nullptr;
1124 }
1125 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001126 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001127}
1128
Orion Hodson58143d22018-02-20 08:44:20 +00001129uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1130 const DexFile* dex_file = method->GetDexFile();
1131 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1132 FindAnnotationsItemForMethod(method);
1133 if (parameter_annotations == nullptr) {
1134 return 0u;
1135 }
1136 const DexFile::AnnotationSetRefList* set_ref_list =
1137 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1138 if (set_ref_list == nullptr) {
1139 return 0u;
1140 }
1141 return set_ref_list->size_;
1142}
1143
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001144ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1145 uint32_t parameter_idx,
1146 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001147 const DexFile* dex_file = method->GetDexFile();
1148 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1149 FindAnnotationsItemForMethod(method);
1150 if (parameter_annotations == nullptr) {
1151 return nullptr;
1152 }
1153 const DexFile::AnnotationSetRefList* set_ref_list =
1154 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1155 if (set_ref_list == nullptr) {
1156 return nullptr;
1157 }
1158 if (parameter_idx >= set_ref_list->size_) {
1159 return nullptr;
1160 }
1161 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1162 const DexFile::AnnotationSetItem* annotation_set =
1163 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001164 if (annotation_set == nullptr) {
1165 return nullptr;
1166 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001167 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001168 annotation_set,
1169 DexFile::kDexVisibilityRuntime,
1170 annotation_class);
1171}
1172
Neil Fuller79a21e72016-09-09 14:24:51 +01001173bool GetParametersMetadataForMethod(ArtMethod* method,
1174 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1175 MutableHandle<mirror::IntArray>* access_flags) {
Yi Kong88307ed2017-04-25 22:33:06 -07001176 const DexFile::AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001177 FindAnnotationSetForMethod(method);
1178 if (annotation_set == nullptr) {
1179 return false;
1180 }
1181
1182 const DexFile* dex_file = method->GetDexFile();
1183 const DexFile::AnnotationItem* annotation_item =
1184 SearchAnnotationSet(*dex_file,
1185 annotation_set,
1186 "Ldalvik/annotation/MethodParameters;",
1187 DexFile::kDexVisibilitySystem);
1188 if (annotation_item == nullptr) {
1189 return false;
1190 }
1191
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001192 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001193
1194 // Extract the parameters' names String[].
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001195 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
Neil Fuller79a21e72016-09-09 14:24:51 +01001196 Handle<mirror::Class> string_array_class(hs.NewHandle(
1197 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001198 if (UNLIKELY(string_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001199 return false;
1200 }
1201
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001202 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001203 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001204 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001205 annotation_item,
1206 "names",
1207 string_array_class,
1208 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001209 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001210 return false;
1211 }
1212
1213 // Extract the parameters' access flags int[].
1214 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001215 if (UNLIKELY(int_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001216 return false;
1217 }
1218 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001219 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001220 annotation_item,
1221 "accessFlags",
1222 int_array_class,
1223 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001224 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001225 return false;
1226 }
1227
1228 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1229 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1230 return true;
1231}
1232
David Sehr9323e6e2016-09-13 08:58:35 -07001233mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1234 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1235 if (annotation_set == nullptr) {
1236 return nullptr;
1237 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001238 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001239}
1240
Roland Levillain35e42f02017-06-26 18:14:39 +01001241bool IsMethodAnnotationPresent(ArtMethod* method,
1242 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001243 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
David Sehr9323e6e2016-09-13 08:58:35 -07001244 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1245 if (annotation_set == nullptr) {
1246 return false;
1247 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001248 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1249 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001250 return annotation_item != nullptr;
1251}
1252
Vladimir Marko0db16e02017-11-08 14:32:33 +00001253static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1254 if (kIsDebugBuild) {
1255 ScopedObjectAccess soa(Thread::Current());
1256 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1257 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001258 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1259 if (klass != nullptr) {
1260 // Lookup using the boot class path loader should yield the annotation class.
1261 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader */ nullptr));
1262 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001263 }
1264}
1265
1266// Check whether a method from the `dex_file` with the given `annotation_set`
1267// is annotated with `annotation_descriptor` with build visibility.
1268static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
1269 const DexFile::AnnotationSetItem& annotation_set,
1270 const char* annotation_descriptor,
1271 jclass annotation_class) {
1272 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
1273 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
1274 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1275 continue;
1276 }
1277 const uint8_t* annotation = annotation_item->annotation_;
1278 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1279 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1280 if (strcmp(descriptor, annotation_descriptor) == 0) {
1281 DCheckNativeAnnotation(descriptor, annotation_class);
1282 return true;
1283 }
1284 }
1285 return false;
1286}
1287
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001288uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
1289 const DexFile::ClassDef& class_def,
1290 uint32_t method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +00001291 const DexFile::AnnotationSetItem* annotation_set =
1292 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001293 if (annotation_set == nullptr) {
1294 return 0u;
1295 }
1296 uint32_t access_flags = 0u;
1297 if (IsMethodBuildAnnotationPresent(
1298 dex_file,
1299 *annotation_set,
1300 "Ldalvik/annotation/optimization/FastNative;",
1301 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1302 access_flags |= kAccFastNative;
1303 }
1304 if (IsMethodBuildAnnotationPresent(
1305 dex_file,
1306 *annotation_set,
1307 "Ldalvik/annotation/optimization/CriticalNative;",
1308 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1309 access_flags |= kAccCriticalNative;
1310 }
1311 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1312 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001313}
1314
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001315ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1316 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001317 ClassData data(klass);
1318 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001319 if (annotation_set == nullptr) {
1320 return nullptr;
1321 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001322 return GetAnnotationObjectFromAnnotationSet(data,
1323 annotation_set,
1324 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001325 annotation_class);
1326}
1327
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001328ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001329 ClassData data(klass);
1330 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1331 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001332}
1333
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001334ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001335 ClassData data(klass);
1336 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001337 if (annotation_set == nullptr) {
1338 return nullptr;
1339 }
1340 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001341 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001342 DexFile::kDexVisibilitySystem);
1343 if (annotation_item == nullptr) {
1344 return nullptr;
1345 }
1346 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001347 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -07001348 Handle<mirror::Class> class_array_class(hs.NewHandle(
1349 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001350 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -07001351 return nullptr;
1352 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001353 ObjPtr<mirror::Object> obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001354 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001355 DexFile::kDexAnnotationArray);
1356 if (obj == nullptr) {
1357 return nullptr;
1358 }
1359 return obj->AsObjectArray<mirror::Class>();
1360}
1361
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001362ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001363 ClassData data(klass);
1364 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001365 if (annotation_set == nullptr) {
1366 return nullptr;
1367 }
1368 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001369 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001370 DexFile::kDexVisibilitySystem);
1371 if (annotation_item == nullptr) {
1372 return nullptr;
1373 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001374 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1375 annotation_item,
1376 "value",
1377 ScopedNullHandle<mirror::Class>(),
1378 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001379 if (obj == nullptr) {
1380 return nullptr;
1381 }
1382 return obj->AsClass();
1383}
1384
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001385ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1386 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
David Sehr9323e6e2016-09-13 08:58:35 -07001387 if (declaring_class != nullptr) {
1388 return declaring_class;
1389 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001390 ClassData data(klass);
1391 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001392 if (annotation_set == nullptr) {
1393 return nullptr;
1394 }
1395 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001396 SearchAnnotationSet(data.GetDexFile(),
1397 annotation_set,
1398 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001399 DexFile::kDexVisibilitySystem);
1400 if (annotation_item == nullptr) {
1401 return nullptr;
1402 }
1403 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001404 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001405 if (annotation == nullptr) {
1406 return nullptr;
1407 }
1408 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001409 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001410 &annotation,
1411 &annotation_value,
1412 ScopedNullHandle<mirror::Class>(),
1413 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001414 return nullptr;
1415 }
1416 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1417 return nullptr;
1418 }
1419 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001420 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001421 annotation_value.value_.GetI(),
1422 hs.NewHandle(data.GetDexCache()),
1423 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001424 if (method == nullptr) {
1425 return nullptr;
1426 }
1427 return method->GetDeclaringClass();
1428}
1429
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001430ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001431 ClassData data(klass);
1432 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001433 if (annotation_set == nullptr) {
1434 return nullptr;
1435 }
1436 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001437 SearchAnnotationSet(data.GetDexFile(),
1438 annotation_set,
1439 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001440 DexFile::kDexVisibilitySystem);
1441 if (annotation_item == nullptr) {
1442 return nullptr;
1443 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001444 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001445 DexFile::kDexAnnotationMethod);
1446}
1447
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001448bool GetInnerClass(Handle<mirror::Class> klass, ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001449 ClassData data(klass);
1450 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001451 if (annotation_set == nullptr) {
1452 return false;
1453 }
1454 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001455 data.GetDexFile(),
1456 annotation_set,
1457 "Ldalvik/annotation/InnerClass;",
1458 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001459 if (annotation_item == nullptr) {
1460 return false;
1461 }
1462 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001463 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001464 if (annotation == nullptr) {
1465 return false;
1466 }
1467 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001468 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001469 &annotation,
1470 &annotation_value,
1471 ScopedNullHandle<mirror::Class>(),
1472 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001473 return false;
1474 }
1475 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1476 annotation_value.type_ != DexFile::kDexAnnotationString) {
1477 return false;
1478 }
1479 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1480 return true;
1481}
1482
1483bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001484 ClassData data(klass);
1485 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001486 if (annotation_set == nullptr) {
1487 return false;
1488 }
1489 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001490 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001491 DexFile::kDexVisibilitySystem);
1492 if (annotation_item == nullptr) {
1493 return false;
1494 }
1495 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001496 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001497 if (annotation == nullptr) {
1498 return false;
1499 }
1500 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001501 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001502 &annotation,
1503 &annotation_value,
1504 ScopedNullHandle<mirror::Class>(),
1505 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001506 return false;
1507 }
1508 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1509 return false;
1510 }
1511 *flags = annotation_value.value_.GetI();
1512 return true;
1513}
1514
1515mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001516 ClassData data(klass);
1517 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001518 if (annotation_set == nullptr) {
1519 return nullptr;
1520 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001521 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001522}
1523
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001524const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001525 // Before instantiating ClassData, check that klass has a DexCache
1526 // assigned. The ClassData constructor indirectly dereferences it
1527 // when calling klass->GetDexFile().
1528 if (klass->GetDexCache() == nullptr) {
1529 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1530 return nullptr;
1531 }
1532
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001533 ClassData data(klass);
1534 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1535 if (annotation_set == nullptr) {
1536 return nullptr;
1537 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001538
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001539 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1540 data.GetDexFile(),
1541 annotation_set,
1542 "Ldalvik/annotation/SourceDebugExtension;",
1543 DexFile::kDexVisibilitySystem);
1544 if (annotation_item == nullptr) {
1545 return nullptr;
1546 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001547
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001548 const uint8_t* annotation =
1549 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1550 if (annotation == nullptr) {
1551 return nullptr;
1552 }
1553 DexFile::AnnotationValue annotation_value;
1554 if (!ProcessAnnotationValue<false>(data,
1555 &annotation,
1556 &annotation_value,
1557 ScopedNullHandle<mirror::Class>(),
1558 DexFile::kAllRaw)) {
1559 return nullptr;
1560 }
1561 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1562 return nullptr;
1563 }
1564 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1565 return data.GetDexFile().StringDataByIdx(index);
1566}
1567
David Sehr9323e6e2016-09-13 08:58:35 -07001568bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001569 ClassData data(klass);
1570 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001571 if (annotation_set == nullptr) {
1572 return false;
1573 }
1574 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001575 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001576 return annotation_item != nullptr;
1577}
1578
1579int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1580 // For native method, lineno should be -2 to indicate it is native. Note that
1581 // "line number == -2" is how libcore tells from StackTraceElement.
1582 if (method->GetCodeItemOffset() == 0) {
1583 return -2;
1584 }
1585
David Sehr0225f8e2018-01-31 08:52:24 +00001586 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001587 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001588
1589 // A method with no line number info should return -1
1590 DexFile::LineNumFromPcContext context(rel_pc, -1);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001591 dex_file->DecodeDebugPositionInfo(accessor.DebugInfoOffset(), DexFile::LineNumForPcCb, &context);
David Sehr9323e6e2016-09-13 08:58:35 -07001592 return context.line_num_;
1593}
1594
1595template<bool kTransactionActive>
1596void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1597 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001598 switch (type_) {
1599 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1600 break;
1601 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1602 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1603 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1604 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1605 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1606 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1607 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1608 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1609 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001610 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001611 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001612 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1613 break;
1614 }
1615 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001616 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001617 dex_cache_,
1618 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001619 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1620 break;
1621 }
1622 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1623 }
1624}
1625template
1626void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1627template
1628void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1629
1630} // namespace annotations
1631
1632} // namespace art