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