blob: 0765465db403a0f999a41b687c100c01940f0a11 [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
21#include "art_field-inl.h"
22#include "art_method-inl.h"
23#include "class_linker-inl.h"
24#include "dex_file-inl.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070025#include "jvalue-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070026#include "mirror/field.h"
27#include "mirror/method.h"
28#include "reflection.h"
29#include "thread.h"
30
31namespace art {
32
33struct DexFile::AnnotationValue {
34 JValue value_;
35 uint8_t type_;
36};
37
38namespace {
39mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
40 Handle<mirror::Class> annotation_class,
41 const uint8_t** annotation)
42 REQUIRES_SHARED(Locks::mutator_lock_);
43
44bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
45 if (expected == DexFile::kDexVisibilityRuntime) {
46 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
47 if (sdk_version > 0 && sdk_version <= 23) {
48 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
49 }
50 }
51 return actual == expected;
52}
53
54const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
55 REQUIRES_SHARED(Locks::mutator_lock_) {
56 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -070057 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
David Sehr9323e6e2016-09-13 08:58:35 -070058 const DexFile::AnnotationsDirectoryItem* annotations_dir =
59 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
60 if (annotations_dir == nullptr) {
61 return nullptr;
62 }
63 const DexFile::FieldAnnotationsItem* field_annotations =
64 dex_file->GetFieldAnnotations(annotations_dir);
65 if (field_annotations == nullptr) {
66 return nullptr;
67 }
68 uint32_t field_index = field->GetDexFieldIndex();
69 uint32_t field_count = annotations_dir->fields_size_;
70 for (uint32_t i = 0; i < field_count; ++i) {
71 if (field_annotations[i].field_idx_ == field_index) {
72 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
73 }
74 }
75 return nullptr;
76}
77
78const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
79 const DexFile::AnnotationSetItem* annotation_set,
80 const char* descriptor,
81 uint32_t visibility)
82 REQUIRES_SHARED(Locks::mutator_lock_) {
83 const DexFile::AnnotationItem* result = nullptr;
84 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
85 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
86 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
87 continue;
88 }
89 const uint8_t* annotation = annotation_item->annotation_;
90 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
91
92 if (strcmp(descriptor, dex_file.StringByTypeIdx(type_index)) == 0) {
93 result = annotation_item;
94 break;
95 }
96 }
97 return result;
98}
99
100bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
101 REQUIRES_SHARED(Locks::mutator_lock_) {
102 const uint8_t* annotation = *annotation_ptr;
103 uint8_t header_byte = *(annotation++);
104 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
105 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
106 int32_t width = value_arg + 1;
107
108 switch (value_type) {
109 case DexFile::kDexAnnotationByte:
110 case DexFile::kDexAnnotationShort:
111 case DexFile::kDexAnnotationChar:
112 case DexFile::kDexAnnotationInt:
113 case DexFile::kDexAnnotationLong:
114 case DexFile::kDexAnnotationFloat:
115 case DexFile::kDexAnnotationDouble:
116 case DexFile::kDexAnnotationString:
117 case DexFile::kDexAnnotationType:
118 case DexFile::kDexAnnotationMethod:
119 case DexFile::kDexAnnotationField:
120 case DexFile::kDexAnnotationEnum:
121 break;
122 case DexFile::kDexAnnotationArray:
123 {
124 uint32_t size = DecodeUnsignedLeb128(&annotation);
125 while (size--) {
126 if (!SkipAnnotationValue(dex_file, &annotation)) {
127 return false;
128 }
129 }
130 width = 0;
131 break;
132 }
133 case DexFile::kDexAnnotationAnnotation:
134 {
135 DecodeUnsignedLeb128(&annotation); // unused type_index
136 uint32_t size = DecodeUnsignedLeb128(&annotation);
137 while (size--) {
138 DecodeUnsignedLeb128(&annotation); // unused element_name_index
139 if (!SkipAnnotationValue(dex_file, &annotation)) {
140 return false;
141 }
142 }
143 width = 0;
144 break;
145 }
146 case DexFile::kDexAnnotationBoolean:
147 case DexFile::kDexAnnotationNull:
148 width = 0;
149 break;
150 default:
151 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
152 return false;
153 }
154
155 annotation += width;
156 *annotation_ptr = annotation;
157 return true;
158}
159
160const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
161 const uint8_t* annotation,
162 const char* name)
163 REQUIRES_SHARED(Locks::mutator_lock_) {
164 DecodeUnsignedLeb128(&annotation); // unused type_index
165 uint32_t size = DecodeUnsignedLeb128(&annotation);
166
167 while (size != 0) {
168 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
169 const char* element_name = dex_file.GetStringData(dex_file.GetStringId(element_name_index));
170 if (strcmp(name, element_name) == 0) {
171 return annotation;
172 }
173 SkipAnnotationValue(dex_file, &annotation);
174 size--;
175 }
176 return nullptr;
177}
178
179const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
180 REQUIRES_SHARED(Locks::mutator_lock_) {
181 const DexFile* dex_file = method->GetDexFile();
182 mirror::Class* klass = method->GetDeclaringClass();
183 const DexFile::AnnotationsDirectoryItem* annotations_dir =
184 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
185 if (annotations_dir == nullptr) {
186 return nullptr;
187 }
188 const DexFile::MethodAnnotationsItem* method_annotations =
189 dex_file->GetMethodAnnotations(annotations_dir);
190 if (method_annotations == nullptr) {
191 return nullptr;
192 }
193 uint32_t method_index = method->GetDexMethodIndex();
194 uint32_t method_count = annotations_dir->methods_size_;
195 for (uint32_t i = 0; i < method_count; ++i) {
196 if (method_annotations[i].method_idx_ == method_index) {
197 return dex_file->GetMethodAnnotationSetItem(method_annotations[i]);
198 }
199 }
200 return nullptr;
201}
202
203const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
204 REQUIRES_SHARED(Locks::mutator_lock_) {
205 const DexFile* dex_file = method->GetDexFile();
206 mirror::Class* klass = method->GetDeclaringClass();
207 const DexFile::AnnotationsDirectoryItem* annotations_dir =
208 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
209 if (annotations_dir == nullptr) {
210 return nullptr;
211 }
212 const DexFile::ParameterAnnotationsItem* parameter_annotations =
213 dex_file->GetParameterAnnotations(annotations_dir);
214 if (parameter_annotations == nullptr) {
215 return nullptr;
216 }
217 uint32_t method_index = method->GetDexMethodIndex();
218 uint32_t parameter_count = annotations_dir->parameters_size_;
219 for (uint32_t i = 0; i < parameter_count; ++i) {
220 if (parameter_annotations[i].method_idx_ == method_index) {
221 return &parameter_annotations[i];
222 }
223 }
224 return nullptr;
225}
226
227const DexFile::AnnotationSetItem* FindAnnotationSetForClass(Handle<mirror::Class> klass)
228 REQUIRES_SHARED(Locks::mutator_lock_) {
229 const DexFile& dex_file = klass->GetDexFile();
230 const DexFile::AnnotationsDirectoryItem* annotations_dir =
231 dex_file.GetAnnotationsDirectory(*klass->GetClassDef());
232 if (annotations_dir == nullptr) {
233 return nullptr;
234 }
235 return dex_file.GetClassAnnotationSet(annotations_dir);
236}
237
238mirror::Object* ProcessEncodedAnnotation(Handle<mirror::Class> klass, const uint8_t** annotation)
239 REQUIRES_SHARED(Locks::mutator_lock_) {
240 uint32_t type_index = DecodeUnsignedLeb128(annotation);
241 uint32_t size = DecodeUnsignedLeb128(annotation);
242
243 Thread* self = Thread::Current();
244 ScopedObjectAccessUnchecked soa(self);
245 StackHandleScope<2> hs(self);
246 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
247 Handle<mirror::Class> annotation_class(hs.NewHandle(
248 class_linker->ResolveType(klass->GetDexFile(), type_index, klass.Get())));
249 if (annotation_class.Get() == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700250 LOG(INFO) << "Unable to resolve " << klass->PrettyClass() << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700251 DCHECK(Thread::Current()->IsExceptionPending());
252 Thread::Current()->ClearException();
253 return nullptr;
254 }
255
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700256 ObjPtr<mirror::Class> annotation_member_class =
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700257 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700258 mirror::Class* annotation_member_array_class =
259 class_linker->FindArrayClass(self, &annotation_member_class);
260 if (annotation_member_array_class == nullptr) {
261 return nullptr;
262 }
263 mirror::ObjectArray<mirror::Object>* element_array = nullptr;
264 if (size > 0) {
265 element_array =
266 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
267 if (element_array == nullptr) {
268 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
269 return nullptr;
270 }
271 }
272
273 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
274 for (uint32_t i = 0; i < size; ++i) {
275 mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation);
276 if (new_member == nullptr) {
277 return nullptr;
278 }
279 h_element_array->SetWithoutChecks<false>(i, new_member);
280 }
281
282 JValue result;
283 ArtMethod* create_annotation_method =
284 soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
285 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
286 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
287 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
288 if (self->IsExceptionPending()) {
289 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
290 return nullptr;
291 }
292
293 return result.GetL();
294}
295
296bool ProcessAnnotationValue(Handle<mirror::Class> klass,
297 const uint8_t** annotation_ptr,
298 DexFile::AnnotationValue* annotation_value,
299 Handle<mirror::Class> array_class,
300 DexFile::AnnotationResultStyle result_style)
301 REQUIRES_SHARED(Locks::mutator_lock_) {
302 const DexFile& dex_file = klass->GetDexFile();
303 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700304 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700305 bool set_object = false;
306 Primitive::Type primitive_type = Primitive::kPrimVoid;
307 const uint8_t* annotation = *annotation_ptr;
308 uint8_t header_byte = *(annotation++);
309 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
310 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
311 int32_t width = value_arg + 1;
312 annotation_value->type_ = value_type;
313
314 switch (value_type) {
315 case DexFile::kDexAnnotationByte:
316 annotation_value->value_.SetB(
317 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
318 primitive_type = Primitive::kPrimByte;
319 break;
320 case DexFile::kDexAnnotationShort:
321 annotation_value->value_.SetS(
322 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
323 primitive_type = Primitive::kPrimShort;
324 break;
325 case DexFile::kDexAnnotationChar:
326 annotation_value->value_.SetC(
327 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
328 primitive_type = Primitive::kPrimChar;
329 break;
330 case DexFile::kDexAnnotationInt:
331 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
332 primitive_type = Primitive::kPrimInt;
333 break;
334 case DexFile::kDexAnnotationLong:
335 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
336 primitive_type = Primitive::kPrimLong;
337 break;
338 case DexFile::kDexAnnotationFloat:
339 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
340 primitive_type = Primitive::kPrimFloat;
341 break;
342 case DexFile::kDexAnnotationDouble:
343 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
344 primitive_type = Primitive::kPrimDouble;
345 break;
346 case DexFile::kDexAnnotationBoolean:
347 annotation_value->value_.SetZ(value_arg != 0);
348 primitive_type = Primitive::kPrimBoolean;
349 width = 0;
350 break;
351 case DexFile::kDexAnnotationString: {
352 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
353 if (result_style == DexFile::kAllRaw) {
354 annotation_value->value_.SetI(index);
355 } else {
356 StackHandleScope<1> hs(self);
357 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
358 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
359 klass->GetDexFile(), index, dex_cache);
360 set_object = true;
361 if (element_object == nullptr) {
362 return false;
363 }
364 }
365 break;
366 }
367 case DexFile::kDexAnnotationType: {
368 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
369 if (result_style == DexFile::kAllRaw) {
370 annotation_value->value_.SetI(index);
371 } else {
372 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
373 klass->GetDexFile(), index, klass.Get());
374 set_object = true;
375 if (element_object == nullptr) {
376 CHECK(self->IsExceptionPending());
377 if (result_style == DexFile::kAllObjects) {
378 const char* msg = dex_file.StringByTypeIdx(index);
379 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
380 element_object = self->GetException();
381 self->ClearException();
382 } else {
383 return false;
384 }
385 }
386 }
387 break;
388 }
389 case DexFile::kDexAnnotationMethod: {
390 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
391 if (result_style == DexFile::kAllRaw) {
392 annotation_value->value_.SetI(index);
393 } else {
394 StackHandleScope<2> hs(self);
395 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
396 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
397 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
398 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
399 klass->GetDexFile(), index, dex_cache, class_loader);
400 if (method == nullptr) {
401 return false;
402 }
403 PointerSize pointer_size = class_linker->GetImagePointerSize();
404 set_object = true;
405 DCHECK(!Runtime::Current()->IsActiveTransaction());
406 if (method->IsConstructor()) {
407 if (pointer_size == PointerSize::k64) {
408 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
409 false>(self, method);
410 } else {
411 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
412 false>(self, method);
413 }
414 } else {
415 if (pointer_size == PointerSize::k64) {
416 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
417 false>(self, method);
418 } else {
419 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
420 false>(self, method);
421 }
422 }
423 if (element_object == nullptr) {
424 return false;
425 }
426 }
427 break;
428 }
429 case DexFile::kDexAnnotationField: {
430 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
431 if (result_style == DexFile::kAllRaw) {
432 annotation_value->value_.SetI(index);
433 } else {
434 StackHandleScope<2> hs(self);
435 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
436 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
437 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
438 klass->GetDexFile(), index, dex_cache, class_loader);
439 if (field == nullptr) {
440 return false;
441 }
442 set_object = true;
443 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
444 if (pointer_size == PointerSize::k64) {
445 element_object = mirror::Field::CreateFromArtField<PointerSize::k64>(self, field, true);
446 } else {
447 element_object = mirror::Field::CreateFromArtField<PointerSize::k32>(self, field, true);
448 }
449 if (element_object == nullptr) {
450 return false;
451 }
452 }
453 break;
454 }
455 case DexFile::kDexAnnotationEnum: {
456 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
457 if (result_style == DexFile::kAllRaw) {
458 annotation_value->value_.SetI(index);
459 } else {
460 StackHandleScope<3> hs(self);
461 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
462 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
463 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
464 klass->GetDexFile(), index, dex_cache, class_loader, true);
465 if (enum_field == nullptr) {
466 return false;
467 } else {
468 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
469 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
470 element_object = enum_field->GetObject(field_class.Get());
471 set_object = true;
472 }
473 }
474 break;
475 }
476 case DexFile::kDexAnnotationArray:
477 if (result_style == DexFile::kAllRaw || array_class.Get() == nullptr) {
478 return false;
479 } else {
480 ScopedObjectAccessUnchecked soa(self);
481 StackHandleScope<2> hs(self);
482 uint32_t size = DecodeUnsignedLeb128(&annotation);
483 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
484 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
485 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
486 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
487 if (new_array.Get() == nullptr) {
488 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
489 return false;
490 }
491 DexFile::AnnotationValue new_annotation_value;
492 for (uint32_t i = 0; i < size; ++i) {
493 if (!ProcessAnnotationValue(klass, &annotation, &new_annotation_value,
494 component_type, DexFile::kPrimitivesOrObjects)) {
495 return false;
496 }
497 if (!component_type->IsPrimitive()) {
498 mirror::Object* obj = new_annotation_value.value_.GetL();
499 new_array->AsObjectArray<mirror::Object>()->SetWithoutChecks<false>(i, obj);
500 } else {
501 switch (new_annotation_value.type_) {
502 case DexFile::kDexAnnotationByte:
503 new_array->AsByteArray()->SetWithoutChecks<false>(
504 i, new_annotation_value.value_.GetB());
505 break;
506 case DexFile::kDexAnnotationShort:
507 new_array->AsShortArray()->SetWithoutChecks<false>(
508 i, new_annotation_value.value_.GetS());
509 break;
510 case DexFile::kDexAnnotationChar:
511 new_array->AsCharArray()->SetWithoutChecks<false>(
512 i, new_annotation_value.value_.GetC());
513 break;
514 case DexFile::kDexAnnotationInt:
515 new_array->AsIntArray()->SetWithoutChecks<false>(
516 i, new_annotation_value.value_.GetI());
517 break;
518 case DexFile::kDexAnnotationLong:
519 new_array->AsLongArray()->SetWithoutChecks<false>(
520 i, new_annotation_value.value_.GetJ());
521 break;
522 case DexFile::kDexAnnotationFloat:
523 new_array->AsFloatArray()->SetWithoutChecks<false>(
524 i, new_annotation_value.value_.GetF());
525 break;
526 case DexFile::kDexAnnotationDouble:
527 new_array->AsDoubleArray()->SetWithoutChecks<false>(
528 i, new_annotation_value.value_.GetD());
529 break;
530 case DexFile::kDexAnnotationBoolean:
531 new_array->AsBooleanArray()->SetWithoutChecks<false>(
532 i, new_annotation_value.value_.GetZ());
533 break;
534 default:
535 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
536 return false;
537 }
538 }
539 }
540 element_object = new_array.Get();
541 set_object = true;
542 width = 0;
543 }
544 break;
545 case DexFile::kDexAnnotationAnnotation:
546 if (result_style == DexFile::kAllRaw) {
547 return false;
548 }
549 element_object = ProcessEncodedAnnotation(klass, &annotation);
550 if (element_object == nullptr) {
551 return false;
552 }
553 set_object = true;
554 width = 0;
555 break;
556 case DexFile::kDexAnnotationNull:
557 if (result_style == DexFile::kAllRaw) {
558 annotation_value->value_.SetI(0);
559 } else {
560 CHECK(element_object == nullptr);
561 set_object = true;
562 }
563 width = 0;
564 break;
565 default:
566 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
567 return false;
568 }
569
570 annotation += width;
571 *annotation_ptr = annotation;
572
573 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700574 element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700575 set_object = true;
576 }
577
578 if (set_object) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700579 annotation_value->value_.SetL(element_object.Ptr());
David Sehr9323e6e2016-09-13 08:58:35 -0700580 }
581
582 return true;
583}
584
585mirror::Object* CreateAnnotationMember(Handle<mirror::Class> klass,
586 Handle<mirror::Class> annotation_class,
587 const uint8_t** annotation) {
588 const DexFile& dex_file = klass->GetDexFile();
589 Thread* self = Thread::Current();
590 ScopedObjectAccessUnchecked soa(self);
591 StackHandleScope<5> hs(self);
592 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
593 const char* name = dex_file.StringDataByIdx(element_name_index);
594 Handle<mirror::String> string_name(
595 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
596
597 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
598 ArtMethod* annotation_method =
599 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
600 if (annotation_method == nullptr) {
601 return nullptr;
602 }
603 Handle<mirror::Class> method_return(hs.NewHandle(
604 annotation_method->GetReturnType(true /* resolve */, pointer_size)));
605
606 DexFile::AnnotationValue annotation_value;
607 if (!ProcessAnnotationValue(klass, annotation, &annotation_value, method_return,
608 DexFile::kAllObjects)) {
609 return nullptr;
610 }
611 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
612
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700613 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700614 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
615 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
616 mirror::Method* method_obj_ptr;
617 DCHECK(!Runtime::Current()->IsActiveTransaction());
618 if (pointer_size == PointerSize::k64) {
619 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
620 self, annotation_method);
621 } else {
622 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
623 self, annotation_method);
624 }
625 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
626
627 if (new_member.Get() == nullptr || string_name.Get() == nullptr ||
628 method_object.Get() == nullptr || method_return.Get() == nullptr) {
629 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
630 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
631 return nullptr;
632 }
633
634 JValue result;
635 ArtMethod* annotation_member_init =
636 soa.DecodeMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
637 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
638 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
639 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
640 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
641 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
642 };
643 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
644 if (self->IsExceptionPending()) {
645 LOG(INFO) << "Exception in AnnotationMember.<init>";
646 return nullptr;
647 }
648
649 return new_member.Get();
650}
651
652const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
653 Handle<mirror::Class> klass,
654 const DexFile::AnnotationSetItem* annotation_set,
655 uint32_t visibility,
656 Handle<mirror::Class> annotation_class)
657 REQUIRES_SHARED(Locks::mutator_lock_) {
658 const DexFile& dex_file = klass->GetDexFile();
659 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
660 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
661 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
662 continue;
663 }
664 const uint8_t* annotation = annotation_item->annotation_;
665 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
666 mirror::Class* resolved_class = Runtime::Current()->GetClassLinker()->ResolveType(
667 klass->GetDexFile(), type_index, klass.Get());
668 if (resolved_class == nullptr) {
669 std::string temp;
670 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
671 klass->GetDescriptor(&temp), type_index);
672 CHECK(Thread::Current()->IsExceptionPending());
673 Thread::Current()->ClearException();
674 continue;
675 }
676 if (resolved_class == annotation_class.Get()) {
677 return annotation_item;
678 }
679 }
680
681 return nullptr;
682}
683
684mirror::Object* GetAnnotationObjectFromAnnotationSet(
685 Handle<mirror::Class> klass,
686 const DexFile::AnnotationSetItem* annotation_set,
687 uint32_t visibility,
688 Handle<mirror::Class> annotation_class)
689 REQUIRES_SHARED(Locks::mutator_lock_) {
690 const DexFile::AnnotationItem* annotation_item =
691 GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class);
692 if (annotation_item == nullptr) {
693 return nullptr;
694 }
695 const uint8_t* annotation = annotation_item->annotation_;
696 return ProcessEncodedAnnotation(klass, &annotation);
697}
698
699mirror::Object* GetAnnotationValue(Handle<mirror::Class> klass,
700 const DexFile::AnnotationItem* annotation_item,
701 const char* annotation_name,
702 Handle<mirror::Class> array_class,
703 uint32_t expected_type)
704 REQUIRES_SHARED(Locks::mutator_lock_) {
705 const DexFile& dex_file = klass->GetDexFile();
706 const uint8_t* annotation =
707 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
708 if (annotation == nullptr) {
709 return nullptr;
710 }
711 DexFile::AnnotationValue annotation_value;
712 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value, array_class,
713 DexFile::kAllObjects)) {
714 return nullptr;
715 }
716 if (annotation_value.type_ != expected_type) {
717 return nullptr;
718 }
719 return annotation_value.value_.GetL();
720}
721
722mirror::ObjectArray<mirror::String>* GetSignatureValue(Handle<mirror::Class> klass,
723 const DexFile::AnnotationSetItem* annotation_set)
724 REQUIRES_SHARED(Locks::mutator_lock_) {
725 const DexFile& dex_file = klass->GetDexFile();
726 StackHandleScope<1> hs(Thread::Current());
727 const DexFile::AnnotationItem* annotation_item =
728 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
729 DexFile::kDexVisibilitySystem);
730 if (annotation_item == nullptr) {
731 return nullptr;
732 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700733 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
David Sehr9323e6e2016-09-13 08:58:35 -0700734 Handle<mirror::Class> string_array_class(hs.NewHandle(
735 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
736 if (string_array_class.Get() == nullptr) {
737 return nullptr;
738 }
739 mirror::Object* obj =
740 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
741 DexFile::kDexAnnotationArray);
742 if (obj == nullptr) {
743 return nullptr;
744 }
745 return obj->AsObjectArray<mirror::String>();
746}
747
748mirror::ObjectArray<mirror::Class>* GetThrowsValue(Handle<mirror::Class> klass,
749 const DexFile::AnnotationSetItem* annotation_set)
750 REQUIRES_SHARED(Locks::mutator_lock_) {
751 const DexFile& dex_file = klass->GetDexFile();
752 StackHandleScope<1> hs(Thread::Current());
753 const DexFile::AnnotationItem* annotation_item =
754 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
755 DexFile::kDexVisibilitySystem);
756 if (annotation_item == nullptr) {
757 return nullptr;
758 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700759 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -0700760 Handle<mirror::Class> class_array_class(hs.NewHandle(
761 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
762 if (class_array_class.Get() == nullptr) {
763 return nullptr;
764 }
765 mirror::Object* obj =
766 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
767 DexFile::kDexAnnotationArray);
768 if (obj == nullptr) {
769 return nullptr;
770 }
771 return obj->AsObjectArray<mirror::Class>();
772}
773
774mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(
775 Handle<mirror::Class> klass,
776 const DexFile::AnnotationSetItem* annotation_set,
777 uint32_t visibility)
778 REQUIRES_SHARED(Locks::mutator_lock_) {
779 const DexFile& dex_file = klass->GetDexFile();
780 Thread* self = Thread::Current();
781 ScopedObjectAccessUnchecked soa(self);
782 StackHandleScope<2> hs(self);
783 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700784 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700785 if (annotation_set == nullptr) {
786 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
787 }
788
789 uint32_t size = annotation_set->size_;
790 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
791 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
792 if (result.Get() == nullptr) {
793 return nullptr;
794 }
795
796 uint32_t dest_index = 0;
797 for (uint32_t i = 0; i < size; ++i) {
798 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
799 // Note that we do not use IsVisibilityCompatible here because older code
800 // was correct for this case.
801 if (annotation_item->visibility_ != visibility) {
802 continue;
803 }
804 const uint8_t* annotation = annotation_item->annotation_;
805 mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
806 if (annotation_obj != nullptr) {
807 result->SetWithoutChecks<false>(dest_index, annotation_obj);
808 ++dest_index;
809 } else if (self->IsExceptionPending()) {
810 return nullptr;
811 }
812 }
813
814 if (dest_index == size) {
815 return result.Get();
816 }
817
818 mirror::ObjectArray<mirror::Object>* trimmed_result =
819 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
820 if (trimmed_result == nullptr) {
821 return nullptr;
822 }
823
824 for (uint32_t i = 0; i < dest_index; ++i) {
825 mirror::Object* obj = result->GetWithoutChecks(i);
826 trimmed_result->SetWithoutChecks<false>(i, obj);
827 }
828
829 return trimmed_result;
830}
831
832mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(
833 Handle<mirror::Class> klass,
834 const DexFile::AnnotationSetRefList* set_ref_list,
835 uint32_t size)
836 REQUIRES_SHARED(Locks::mutator_lock_) {
837 const DexFile& dex_file = klass->GetDexFile();
838 Thread* self = Thread::Current();
839 ScopedObjectAccessUnchecked soa(self);
840 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700841 ObjPtr<mirror::Class> annotation_array_class =
842 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
David Sehr9323e6e2016-09-13 08:58:35 -0700843 mirror::Class* annotation_array_array_class =
844 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
845 if (annotation_array_array_class == nullptr) {
846 return nullptr;
847 }
848 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
849 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
850 if (annotation_array_array.Get() == nullptr) {
851 LOG(ERROR) << "Annotation set ref array allocation failed";
852 return nullptr;
853 }
854 for (uint32_t index = 0; index < size; ++index) {
855 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
856 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
857 mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item,
858 DexFile::kDexVisibilityRuntime);
859 if (annotation_set == nullptr) {
860 return nullptr;
861 }
862 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
863 }
864 return annotation_array_array.Get();
865}
866} // namespace
867
868namespace annotations {
869
870mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) {
871 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
872 if (annotation_set == nullptr) {
873 return nullptr;
874 }
875 StackHandleScope<1> hs(Thread::Current());
876 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
877 return GetAnnotationObjectFromAnnotationSet(field_class, annotation_set,
878 DexFile::kDexVisibilityRuntime, annotation_class);
879}
880
881mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) {
882 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
883 StackHandleScope<1> hs(Thread::Current());
884 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
885 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
886}
887
888mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
889 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
890 if (annotation_set == nullptr) {
891 return nullptr;
892 }
893 StackHandleScope<1> hs(Thread::Current());
894 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
895 return GetSignatureValue(field_class, annotation_set);
896}
897
898bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
899 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
900 if (annotation_set == nullptr) {
901 return false;
902 }
903 StackHandleScope<1> hs(Thread::Current());
904 Handle<mirror::Class> field_class(hs.NewHandle(field->GetDeclaringClass()));
905 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
906 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
907 return annotation_item != nullptr;
908}
909
910mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) {
911 const DexFile* dex_file = method->GetDexFile();
912 mirror::Class* klass = method->GetDeclaringClass();
913 const DexFile::AnnotationsDirectoryItem* annotations_dir =
914 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
915 if (annotations_dir == nullptr) {
916 return nullptr;
917 }
918 const DexFile::AnnotationSetItem* annotation_set =
919 dex_file->GetClassAnnotationSet(annotations_dir);
920 if (annotation_set == nullptr) {
921 return nullptr;
922 }
923 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
924 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
925 if (annotation_item == nullptr) {
926 return nullptr;
927 }
928 const uint8_t* annotation =
929 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
930 if (annotation == nullptr) {
931 return nullptr;
932 }
933 uint8_t header_byte = *(annotation++);
934 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
935 return nullptr;
936 }
937 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
938 if (annotation == nullptr) {
939 return nullptr;
940 }
941 DexFile::AnnotationValue annotation_value;
942 StackHandleScope<2> hs(Thread::Current());
943 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
944 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
945 Handle<mirror::Class> return_type(hs.NewHandle(
946 method->GetReturnType(true /* resolve */, pointer_size)));
947 if (!ProcessAnnotationValue(h_klass, &annotation, &annotation_value, return_type,
948 DexFile::kAllObjects)) {
949 return nullptr;
950 }
951 return annotation_value.value_.GetL();
952}
953
954mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
955 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
956 if (annotation_set == nullptr) {
957 return nullptr;
958 }
959 StackHandleScope<1> hs(Thread::Current());
960 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
961 return GetAnnotationObjectFromAnnotationSet(method_class, annotation_set,
962 DexFile::kDexVisibilityRuntime, annotation_class);
963}
964
965mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
966 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
967 StackHandleScope<1> hs(Thread::Current());
968 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
969 return ProcessAnnotationSet(method_class, annotation_set, DexFile::kDexVisibilityRuntime);
970}
971
972mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
973 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
974 if (annotation_set == nullptr) {
975 return nullptr;
976 }
977 StackHandleScope<1> hs(Thread::Current());
978 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
979 return GetThrowsValue(method_class, annotation_set);
980}
981
982mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
983 const DexFile* dex_file = method->GetDexFile();
984 const DexFile::ParameterAnnotationsItem* parameter_annotations =
985 FindAnnotationsItemForMethod(method);
986 if (parameter_annotations == nullptr) {
987 return nullptr;
988 }
989 const DexFile::AnnotationSetRefList* set_ref_list =
990 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
991 if (set_ref_list == nullptr) {
992 return nullptr;
993 }
994 uint32_t size = set_ref_list->size_;
995 StackHandleScope<1> hs(Thread::Current());
996 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
997 return ProcessAnnotationSetRefList(method_class, set_ref_list, size);
998}
999
1000mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1001 uint32_t parameter_idx,
1002 Handle<mirror::Class> annotation_class) {
1003 const DexFile* dex_file = method->GetDexFile();
1004 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1005 FindAnnotationsItemForMethod(method);
1006 if (parameter_annotations == nullptr) {
1007 return nullptr;
1008 }
1009 const DexFile::AnnotationSetRefList* set_ref_list =
1010 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1011 if (set_ref_list == nullptr) {
1012 return nullptr;
1013 }
1014 if (parameter_idx >= set_ref_list->size_) {
1015 return nullptr;
1016 }
1017 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1018 const DexFile::AnnotationSetItem* annotation_set =
1019 dex_file->GetSetRefItemItem(annotation_set_ref);
1020
1021 StackHandleScope<1> hs(Thread::Current());
1022 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1023 return GetAnnotationObjectFromAnnotationSet(method_class,
1024 annotation_set,
1025 DexFile::kDexVisibilityRuntime,
1026 annotation_class);
1027}
1028
Neil Fuller79a21e72016-09-09 14:24:51 +01001029bool GetParametersMetadataForMethod(ArtMethod* method,
1030 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1031 MutableHandle<mirror::IntArray>* access_flags) {
1032 const DexFile::AnnotationSetItem::AnnotationSetItem* annotation_set =
1033 FindAnnotationSetForMethod(method);
1034 if (annotation_set == nullptr) {
1035 return false;
1036 }
1037
1038 const DexFile* dex_file = method->GetDexFile();
1039 const DexFile::AnnotationItem* annotation_item =
1040 SearchAnnotationSet(*dex_file,
1041 annotation_set,
1042 "Ldalvik/annotation/MethodParameters;",
1043 DexFile::kDexVisibilitySystem);
1044 if (annotation_item == nullptr) {
1045 return false;
1046 }
1047
1048 StackHandleScope<5> hs(Thread::Current());
1049
1050 // Extract the parameters' names String[].
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001051 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
Neil Fuller79a21e72016-09-09 14:24:51 +01001052 Handle<mirror::Class> string_array_class(hs.NewHandle(
1053 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
1054 if (UNLIKELY(string_array_class.Get() == nullptr)) {
1055 return false;
1056 }
1057
1058 Handle<mirror::Class> klass = hs.NewHandle(method->GetDeclaringClass());
1059 Handle<mirror::Object> names_obj =
1060 hs.NewHandle(GetAnnotationValue(klass,
1061 annotation_item,
1062 "names",
1063 string_array_class,
1064 DexFile::kDexAnnotationArray));
1065 if (names_obj.Get() == nullptr) {
1066 return false;
1067 }
1068
1069 // Extract the parameters' access flags int[].
1070 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
1071 if (UNLIKELY(int_array_class.Get() == nullptr)) {
1072 return false;
1073 }
1074 Handle<mirror::Object> access_flags_obj =
1075 hs.NewHandle(GetAnnotationValue(klass,
1076 annotation_item,
1077 "accessFlags",
1078 int_array_class,
1079 DexFile::kDexAnnotationArray));
1080 if (access_flags_obj.Get() == nullptr) {
1081 return false;
1082 }
1083
1084 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1085 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1086 return true;
1087}
1088
David Sehr9323e6e2016-09-13 08:58:35 -07001089mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1090 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1091 if (annotation_set == nullptr) {
1092 return nullptr;
1093 }
1094 StackHandleScope<1> hs(Thread::Current());
1095 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1096 return GetSignatureValue(method_class, annotation_set);
1097}
1098
1099bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class,
1100 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
1101 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1102 if (annotation_set == nullptr) {
1103 return false;
1104 }
1105 StackHandleScope<1> hs(Thread::Current());
1106 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1107 const DexFile::AnnotationItem* annotation_item =
1108 GetAnnotationItemFromAnnotationSet(method_class, annotation_set, visibility,
1109 annotation_class);
1110 return annotation_item != nullptr;
1111}
1112
1113mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1114 Handle<mirror::Class> annotation_class) {
1115 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1116 if (annotation_set == nullptr) {
1117 return nullptr;
1118 }
1119 return GetAnnotationObjectFromAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime,
1120 annotation_class);
1121}
1122
1123mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
1124 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1125 return ProcessAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime);
1126}
1127
1128mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
1129 const DexFile& dex_file = klass->GetDexFile();
1130 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1131 if (annotation_set == nullptr) {
1132 return nullptr;
1133 }
1134 const DexFile::AnnotationItem* annotation_item =
1135 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/MemberClasses;",
1136 DexFile::kDexVisibilitySystem);
1137 if (annotation_item == nullptr) {
1138 return nullptr;
1139 }
1140 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001141 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -07001142 Handle<mirror::Class> class_array_class(hs.NewHandle(
1143 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
1144 if (class_array_class.Get() == nullptr) {
1145 return nullptr;
1146 }
1147 mirror::Object* obj =
1148 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
1149 DexFile::kDexAnnotationArray);
1150 if (obj == nullptr) {
1151 return nullptr;
1152 }
1153 return obj->AsObjectArray<mirror::Class>();
1154}
1155
1156mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
1157 const DexFile& dex_file = klass->GetDexFile();
1158 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1159 if (annotation_set == nullptr) {
1160 return nullptr;
1161 }
1162 const DexFile::AnnotationItem* annotation_item =
1163 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingClass;",
1164 DexFile::kDexVisibilitySystem);
1165 if (annotation_item == nullptr) {
1166 return nullptr;
1167 }
1168 mirror::Object* obj = GetAnnotationValue(klass, annotation_item, "value",
1169 ScopedNullHandle<mirror::Class>(),
1170 DexFile::kDexAnnotationType);
1171 if (obj == nullptr) {
1172 return nullptr;
1173 }
1174 return obj->AsClass();
1175}
1176
1177mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
1178 const DexFile& dex_file = klass->GetDexFile();
1179 mirror::Class* declaring_class = GetDeclaringClass(klass);
1180 if (declaring_class != nullptr) {
1181 return declaring_class;
1182 }
1183 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1184 if (annotation_set == nullptr) {
1185 return nullptr;
1186 }
1187 const DexFile::AnnotationItem* annotation_item =
1188 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1189 DexFile::kDexVisibilitySystem);
1190 if (annotation_item == nullptr) {
1191 return nullptr;
1192 }
1193 const uint8_t* annotation =
1194 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "value");
1195 if (annotation == nullptr) {
1196 return nullptr;
1197 }
1198 DexFile::AnnotationValue annotation_value;
1199 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1200 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1201 return nullptr;
1202 }
1203 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1204 return nullptr;
1205 }
1206 StackHandleScope<2> hs(Thread::Current());
1207 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
1208 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
1209 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
1210 klass->GetDexFile(), annotation_value.value_.GetI(), dex_cache, class_loader);
1211 if (method == nullptr) {
1212 return nullptr;
1213 }
1214 return method->GetDeclaringClass();
1215}
1216
1217mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
1218 const DexFile& dex_file = klass->GetDexFile();
1219 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1220 if (annotation_set == nullptr) {
1221 return nullptr;
1222 }
1223 const DexFile::AnnotationItem* annotation_item =
1224 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1225 DexFile::kDexVisibilitySystem);
1226 if (annotation_item == nullptr) {
1227 return nullptr;
1228 }
1229 return GetAnnotationValue(klass, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
1230 DexFile::kDexAnnotationMethod);
1231}
1232
1233bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
1234 const DexFile& dex_file = klass->GetDexFile();
1235 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1236 if (annotation_set == nullptr) {
1237 return false;
1238 }
1239 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1240 dex_file, annotation_set, "Ldalvik/annotation/InnerClass;", DexFile::kDexVisibilitySystem);
1241 if (annotation_item == nullptr) {
1242 return false;
1243 }
1244 const uint8_t* annotation =
1245 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "name");
1246 if (annotation == nullptr) {
1247 return false;
1248 }
1249 DexFile::AnnotationValue annotation_value;
1250 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1251 ScopedNullHandle<mirror::Class>(),
1252 DexFile::kAllObjects)) {
1253 return false;
1254 }
1255 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1256 annotation_value.type_ != DexFile::kDexAnnotationString) {
1257 return false;
1258 }
1259 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1260 return true;
1261}
1262
1263bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
1264 const DexFile& dex_file = klass->GetDexFile();
1265 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1266 if (annotation_set == nullptr) {
1267 return false;
1268 }
1269 const DexFile::AnnotationItem* annotation_item =
1270 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/InnerClass;",
1271 DexFile::kDexVisibilitySystem);
1272 if (annotation_item == nullptr) {
1273 return false;
1274 }
1275 const uint8_t* annotation =
1276 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "accessFlags");
1277 if (annotation == nullptr) {
1278 return false;
1279 }
1280 DexFile::AnnotationValue annotation_value;
1281 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1282 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1283 return false;
1284 }
1285 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1286 return false;
1287 }
1288 *flags = annotation_value.value_.GetI();
1289 return true;
1290}
1291
1292mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
1293 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1294 if (annotation_set == nullptr) {
1295 return nullptr;
1296 }
1297 return GetSignatureValue(klass, annotation_set);
1298}
1299
1300bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
1301 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1302 if (annotation_set == nullptr) {
1303 return false;
1304 }
1305 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1306 klass, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1307 return annotation_item != nullptr;
1308}
1309
1310int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1311 // For native method, lineno should be -2 to indicate it is native. Note that
1312 // "line number == -2" is how libcore tells from StackTraceElement.
1313 if (method->GetCodeItemOffset() == 0) {
1314 return -2;
1315 }
1316
1317 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
David Sehr709b0702016-10-13 09:12:37 -07001318 DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001319
1320 // A method with no line number info should return -1
1321 DexFile::LineNumFromPcContext context(rel_pc, -1);
1322 dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context);
1323 return context.line_num_;
1324}
1325
1326template<bool kTransactionActive>
1327void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1328 DCHECK(dex_cache_ != nullptr);
1329 DCHECK(class_loader_ != nullptr);
1330 switch (type_) {
1331 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1332 break;
1333 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1334 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1335 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1336 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1337 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1338 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1339 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1340 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1341 case kString: {
1342 mirror::String* resolved = linker_->ResolveString(dex_file_, jval_.i, *dex_cache_);
1343 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1344 break;
1345 }
1346 case kType: {
1347 mirror::Class* resolved = linker_->ResolveType(dex_file_, jval_.i, *dex_cache_,
1348 *class_loader_);
1349 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1350 break;
1351 }
1352 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1353 }
1354}
1355template
1356void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1357template
1358void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1359
1360} // namespace annotations
1361
1362} // namespace art