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