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