blob: 16a447b0a6e248f1fd483a8b520753495c9c4447 [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(
Vladimir Marko942fd312017-01-16 20:52:19 +0000611 annotation_method->GetReturnType(true /* resolve */)));
David Sehr9323e6e2016-09-13 08:58:35 -0700612
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));
Vladimir Marko942fd312017-01-16 20:52:19 +0000951 Handle<mirror::Class> return_type(hs.NewHandle(method->GetReturnType(true /* resolve */)));
David Sehr9323e6e2016-09-13 08:58:35 -0700952 if (!ProcessAnnotationValue(h_klass, &annotation, &annotation_value, return_type,
953 DexFile::kAllObjects)) {
954 return nullptr;
955 }
956 return annotation_value.value_.GetL();
957}
958
959mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
960 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
961 if (annotation_set == nullptr) {
962 return nullptr;
963 }
964 StackHandleScope<1> hs(Thread::Current());
965 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
966 return GetAnnotationObjectFromAnnotationSet(method_class, annotation_set,
967 DexFile::kDexVisibilityRuntime, annotation_class);
968}
969
970mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
971 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
972 StackHandleScope<1> hs(Thread::Current());
973 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
974 return ProcessAnnotationSet(method_class, annotation_set, DexFile::kDexVisibilityRuntime);
975}
976
977mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
978 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
979 if (annotation_set == nullptr) {
980 return nullptr;
981 }
982 StackHandleScope<1> hs(Thread::Current());
983 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
984 return GetThrowsValue(method_class, annotation_set);
985}
986
987mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
988 const DexFile* dex_file = method->GetDexFile();
989 const DexFile::ParameterAnnotationsItem* parameter_annotations =
990 FindAnnotationsItemForMethod(method);
991 if (parameter_annotations == nullptr) {
992 return nullptr;
993 }
994 const DexFile::AnnotationSetRefList* set_ref_list =
995 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
996 if (set_ref_list == nullptr) {
997 return nullptr;
998 }
999 uint32_t size = set_ref_list->size_;
1000 StackHandleScope<1> hs(Thread::Current());
1001 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1002 return ProcessAnnotationSetRefList(method_class, set_ref_list, size);
1003}
1004
1005mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1006 uint32_t parameter_idx,
1007 Handle<mirror::Class> annotation_class) {
1008 const DexFile* dex_file = method->GetDexFile();
1009 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1010 FindAnnotationsItemForMethod(method);
1011 if (parameter_annotations == nullptr) {
1012 return nullptr;
1013 }
1014 const DexFile::AnnotationSetRefList* set_ref_list =
1015 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1016 if (set_ref_list == nullptr) {
1017 return nullptr;
1018 }
1019 if (parameter_idx >= set_ref_list->size_) {
1020 return nullptr;
1021 }
1022 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1023 const DexFile::AnnotationSetItem* annotation_set =
1024 dex_file->GetSetRefItemItem(annotation_set_ref);
1025
1026 StackHandleScope<1> hs(Thread::Current());
1027 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1028 return GetAnnotationObjectFromAnnotationSet(method_class,
1029 annotation_set,
1030 DexFile::kDexVisibilityRuntime,
1031 annotation_class);
1032}
1033
Neil Fuller79a21e72016-09-09 14:24:51 +01001034bool GetParametersMetadataForMethod(ArtMethod* method,
1035 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1036 MutableHandle<mirror::IntArray>* access_flags) {
1037 const DexFile::AnnotationSetItem::AnnotationSetItem* annotation_set =
1038 FindAnnotationSetForMethod(method);
1039 if (annotation_set == nullptr) {
1040 return false;
1041 }
1042
1043 const DexFile* dex_file = method->GetDexFile();
1044 const DexFile::AnnotationItem* annotation_item =
1045 SearchAnnotationSet(*dex_file,
1046 annotation_set,
1047 "Ldalvik/annotation/MethodParameters;",
1048 DexFile::kDexVisibilitySystem);
1049 if (annotation_item == nullptr) {
1050 return false;
1051 }
1052
1053 StackHandleScope<5> hs(Thread::Current());
1054
1055 // Extract the parameters' names String[].
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001056 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
Neil Fuller79a21e72016-09-09 14:24:51 +01001057 Handle<mirror::Class> string_array_class(hs.NewHandle(
1058 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
1059 if (UNLIKELY(string_array_class.Get() == nullptr)) {
1060 return false;
1061 }
1062
1063 Handle<mirror::Class> klass = hs.NewHandle(method->GetDeclaringClass());
1064 Handle<mirror::Object> names_obj =
1065 hs.NewHandle(GetAnnotationValue(klass,
1066 annotation_item,
1067 "names",
1068 string_array_class,
1069 DexFile::kDexAnnotationArray));
1070 if (names_obj.Get() == nullptr) {
1071 return false;
1072 }
1073
1074 // Extract the parameters' access flags int[].
1075 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
1076 if (UNLIKELY(int_array_class.Get() == nullptr)) {
1077 return false;
1078 }
1079 Handle<mirror::Object> access_flags_obj =
1080 hs.NewHandle(GetAnnotationValue(klass,
1081 annotation_item,
1082 "accessFlags",
1083 int_array_class,
1084 DexFile::kDexAnnotationArray));
1085 if (access_flags_obj.Get() == nullptr) {
1086 return false;
1087 }
1088
1089 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1090 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1091 return true;
1092}
1093
David Sehr9323e6e2016-09-13 08:58:35 -07001094mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1095 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1096 if (annotation_set == nullptr) {
1097 return nullptr;
1098 }
1099 StackHandleScope<1> hs(Thread::Current());
1100 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1101 return GetSignatureValue(method_class, annotation_set);
1102}
1103
1104bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class,
1105 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
1106 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1107 if (annotation_set == nullptr) {
1108 return false;
1109 }
1110 StackHandleScope<1> hs(Thread::Current());
1111 Handle<mirror::Class> method_class(hs.NewHandle(method->GetDeclaringClass()));
1112 const DexFile::AnnotationItem* annotation_item =
1113 GetAnnotationItemFromAnnotationSet(method_class, annotation_set, visibility,
1114 annotation_class);
1115 return annotation_item != nullptr;
1116}
1117
1118mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1119 Handle<mirror::Class> annotation_class) {
1120 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1121 if (annotation_set == nullptr) {
1122 return nullptr;
1123 }
1124 return GetAnnotationObjectFromAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime,
1125 annotation_class);
1126}
1127
1128mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
1129 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1130 return ProcessAnnotationSet(klass, annotation_set, DexFile::kDexVisibilityRuntime);
1131}
1132
1133mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
1134 const DexFile& dex_file = klass->GetDexFile();
1135 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1136 if (annotation_set == nullptr) {
1137 return nullptr;
1138 }
1139 const DexFile::AnnotationItem* annotation_item =
1140 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/MemberClasses;",
1141 DexFile::kDexVisibilitySystem);
1142 if (annotation_item == nullptr) {
1143 return nullptr;
1144 }
1145 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001146 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -07001147 Handle<mirror::Class> class_array_class(hs.NewHandle(
1148 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
1149 if (class_array_class.Get() == nullptr) {
1150 return nullptr;
1151 }
1152 mirror::Object* obj =
1153 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
1154 DexFile::kDexAnnotationArray);
1155 if (obj == nullptr) {
1156 return nullptr;
1157 }
1158 return obj->AsObjectArray<mirror::Class>();
1159}
1160
1161mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
1162 const DexFile& dex_file = klass->GetDexFile();
1163 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1164 if (annotation_set == nullptr) {
1165 return nullptr;
1166 }
1167 const DexFile::AnnotationItem* annotation_item =
1168 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingClass;",
1169 DexFile::kDexVisibilitySystem);
1170 if (annotation_item == nullptr) {
1171 return nullptr;
1172 }
1173 mirror::Object* obj = GetAnnotationValue(klass, annotation_item, "value",
1174 ScopedNullHandle<mirror::Class>(),
1175 DexFile::kDexAnnotationType);
1176 if (obj == nullptr) {
1177 return nullptr;
1178 }
1179 return obj->AsClass();
1180}
1181
1182mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
1183 const DexFile& dex_file = klass->GetDexFile();
1184 mirror::Class* declaring_class = GetDeclaringClass(klass);
1185 if (declaring_class != nullptr) {
1186 return declaring_class;
1187 }
1188 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1189 if (annotation_set == nullptr) {
1190 return nullptr;
1191 }
1192 const DexFile::AnnotationItem* annotation_item =
1193 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1194 DexFile::kDexVisibilitySystem);
1195 if (annotation_item == nullptr) {
1196 return nullptr;
1197 }
1198 const uint8_t* annotation =
1199 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "value");
1200 if (annotation == nullptr) {
1201 return nullptr;
1202 }
1203 DexFile::AnnotationValue annotation_value;
1204 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1205 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1206 return nullptr;
1207 }
1208 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1209 return nullptr;
1210 }
1211 StackHandleScope<2> hs(Thread::Current());
1212 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
1213 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
1214 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
1215 klass->GetDexFile(), annotation_value.value_.GetI(), dex_cache, class_loader);
1216 if (method == nullptr) {
1217 return nullptr;
1218 }
1219 return method->GetDeclaringClass();
1220}
1221
1222mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
1223 const DexFile& dex_file = klass->GetDexFile();
1224 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1225 if (annotation_set == nullptr) {
1226 return nullptr;
1227 }
1228 const DexFile::AnnotationItem* annotation_item =
1229 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/EnclosingMethod;",
1230 DexFile::kDexVisibilitySystem);
1231 if (annotation_item == nullptr) {
1232 return nullptr;
1233 }
1234 return GetAnnotationValue(klass, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
1235 DexFile::kDexAnnotationMethod);
1236}
1237
1238bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
1239 const DexFile& dex_file = klass->GetDexFile();
1240 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1241 if (annotation_set == nullptr) {
1242 return false;
1243 }
1244 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1245 dex_file, annotation_set, "Ldalvik/annotation/InnerClass;", DexFile::kDexVisibilitySystem);
1246 if (annotation_item == nullptr) {
1247 return false;
1248 }
1249 const uint8_t* annotation =
1250 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "name");
1251 if (annotation == nullptr) {
1252 return false;
1253 }
1254 DexFile::AnnotationValue annotation_value;
1255 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1256 ScopedNullHandle<mirror::Class>(),
1257 DexFile::kAllObjects)) {
1258 return false;
1259 }
1260 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1261 annotation_value.type_ != DexFile::kDexAnnotationString) {
1262 return false;
1263 }
1264 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1265 return true;
1266}
1267
1268bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
1269 const DexFile& dex_file = klass->GetDexFile();
1270 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1271 if (annotation_set == nullptr) {
1272 return false;
1273 }
1274 const DexFile::AnnotationItem* annotation_item =
1275 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/InnerClass;",
1276 DexFile::kDexVisibilitySystem);
1277 if (annotation_item == nullptr) {
1278 return false;
1279 }
1280 const uint8_t* annotation =
1281 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, "accessFlags");
1282 if (annotation == nullptr) {
1283 return false;
1284 }
1285 DexFile::AnnotationValue annotation_value;
1286 if (!ProcessAnnotationValue(klass, &annotation, &annotation_value,
1287 ScopedNullHandle<mirror::Class>(), DexFile::kAllRaw)) {
1288 return false;
1289 }
1290 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1291 return false;
1292 }
1293 *flags = annotation_value.value_.GetI();
1294 return true;
1295}
1296
1297mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
1298 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1299 if (annotation_set == nullptr) {
1300 return nullptr;
1301 }
1302 return GetSignatureValue(klass, annotation_set);
1303}
1304
1305bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
1306 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(klass);
1307 if (annotation_set == nullptr) {
1308 return false;
1309 }
1310 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1311 klass, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1312 return annotation_item != nullptr;
1313}
1314
1315int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1316 // For native method, lineno should be -2 to indicate it is native. Note that
1317 // "line number == -2" is how libcore tells from StackTraceElement.
1318 if (method->GetCodeItemOffset() == 0) {
1319 return -2;
1320 }
1321
1322 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
David Sehr709b0702016-10-13 09:12:37 -07001323 DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001324
1325 // A method with no line number info should return -1
1326 DexFile::LineNumFromPcContext context(rel_pc, -1);
1327 dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context);
1328 return context.line_num_;
1329}
1330
1331template<bool kTransactionActive>
1332void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1333 DCHECK(dex_cache_ != nullptr);
1334 DCHECK(class_loader_ != nullptr);
1335 switch (type_) {
1336 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1337 break;
1338 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1339 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1340 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1341 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1342 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1343 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1344 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1345 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1346 case kString: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001347 mirror::String* resolved = linker_->ResolveString(dex_file_,
1348 dex::StringIndex(jval_.i),
1349 *dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001350 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1351 break;
1352 }
1353 case kType: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001354 mirror::Class* resolved = linker_->ResolveType(dex_file_,
1355 dex::TypeIndex(jval_.i),
1356 *dex_cache_,
David Sehr9323e6e2016-09-13 08:58:35 -07001357 *class_loader_);
1358 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1359 break;
1360 }
1361 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1362 }
1363}
1364template
1365void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1366template
1367void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1368
1369} // namespace annotations
1370
1371} // namespace art