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