blob: 6b9654dc49b9e4f6fd8c4de2a2bdb53cf9ed1251 [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 {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000044
45// A helper class that contains all the data needed to do annotation lookup.
46class ClassData {
47 public:
48 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
49 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
50 method,
51 *method->GetDexFile(),
52 &method->GetClassDef()) {}
53
54 // Requires Scope to be able to create at least 1 handles.
55 template <typename Scope>
56 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
57 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
58
59 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
60 : ClassData(klass, // klass
61 nullptr, // method
62 klass->GetDexFile(),
63 klass->GetClassDef()) {}
64
65 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
66 return dex_file_;
67 }
68
69 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
70 return class_def_;
71 }
72
73 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
74 if (method_ != nullptr) {
75 return method_->GetDexCache();
76 } else {
77 return real_klass_->GetDexCache();
78 }
79 }
80
81 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
82 if (method_ != nullptr) {
83 return method_->GetDeclaringClass()->GetClassLoader();
84 } else {
85 return real_klass_->GetClassLoader();
86 }
87 }
88
89 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
90 if (method_ != nullptr) {
91 return method_->GetDeclaringClass();
92 } else {
93 return real_klass_.Get();
94 }
95 }
96
97 private:
98 ClassData(Handle<mirror::Class> klass,
99 ArtMethod* method,
100 const DexFile& dex_file,
101 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
102 : real_klass_(klass),
103 method_(method),
104 dex_file_(dex_file),
105 class_def_(class_def) {
106 DCHECK((method_ == nullptr) || real_klass_.IsNull());
107 }
108
109 Handle<mirror::Class> real_klass_;
110 ArtMethod* method_;
111 const DexFile& dex_file_;
112 const DexFile::ClassDef* class_def_;
113
114 DISALLOW_COPY_AND_ASSIGN(ClassData);
115};
116
117mirror::Object* CreateAnnotationMember(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700118 Handle<mirror::Class> annotation_class,
119 const uint8_t** annotation)
120 REQUIRES_SHARED(Locks::mutator_lock_);
121
122bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
123 if (expected == DexFile::kDexVisibilityRuntime) {
124 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
125 if (sdk_version > 0 && sdk_version <= 23) {
126 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
127 }
128 }
129 return actual == expected;
130}
131
132const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
133 REQUIRES_SHARED(Locks::mutator_lock_) {
134 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700135 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
David Sehr9323e6e2016-09-13 08:58:35 -0700136 const DexFile::AnnotationsDirectoryItem* annotations_dir =
137 dex_file->GetAnnotationsDirectory(*klass->GetClassDef());
138 if (annotations_dir == nullptr) {
139 return nullptr;
140 }
141 const DexFile::FieldAnnotationsItem* field_annotations =
142 dex_file->GetFieldAnnotations(annotations_dir);
143 if (field_annotations == nullptr) {
144 return nullptr;
145 }
146 uint32_t field_index = field->GetDexFieldIndex();
147 uint32_t field_count = annotations_dir->fields_size_;
148 for (uint32_t i = 0; i < field_count; ++i) {
149 if (field_annotations[i].field_idx_ == field_index) {
150 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
151 }
152 }
153 return nullptr;
154}
155
156const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
157 const DexFile::AnnotationSetItem* annotation_set,
158 const char* descriptor,
159 uint32_t visibility)
160 REQUIRES_SHARED(Locks::mutator_lock_) {
161 const DexFile::AnnotationItem* result = nullptr;
162 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
163 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
164 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
165 continue;
166 }
167 const uint8_t* annotation = annotation_item->annotation_;
168 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
169
Andreas Gampea5b09a62016-11-17 15:21:22 -0800170 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700171 result = annotation_item;
172 break;
173 }
174 }
175 return result;
176}
177
178bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
179 REQUIRES_SHARED(Locks::mutator_lock_) {
180 const uint8_t* annotation = *annotation_ptr;
181 uint8_t header_byte = *(annotation++);
182 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
183 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
184 int32_t width = value_arg + 1;
185
186 switch (value_type) {
187 case DexFile::kDexAnnotationByte:
188 case DexFile::kDexAnnotationShort:
189 case DexFile::kDexAnnotationChar:
190 case DexFile::kDexAnnotationInt:
191 case DexFile::kDexAnnotationLong:
192 case DexFile::kDexAnnotationFloat:
193 case DexFile::kDexAnnotationDouble:
194 case DexFile::kDexAnnotationString:
195 case DexFile::kDexAnnotationType:
196 case DexFile::kDexAnnotationMethod:
197 case DexFile::kDexAnnotationField:
198 case DexFile::kDexAnnotationEnum:
199 break;
200 case DexFile::kDexAnnotationArray:
201 {
202 uint32_t size = DecodeUnsignedLeb128(&annotation);
203 while (size--) {
204 if (!SkipAnnotationValue(dex_file, &annotation)) {
205 return false;
206 }
207 }
208 width = 0;
209 break;
210 }
211 case DexFile::kDexAnnotationAnnotation:
212 {
213 DecodeUnsignedLeb128(&annotation); // unused type_index
214 uint32_t size = DecodeUnsignedLeb128(&annotation);
215 while (size--) {
216 DecodeUnsignedLeb128(&annotation); // unused element_name_index
217 if (!SkipAnnotationValue(dex_file, &annotation)) {
218 return false;
219 }
220 }
221 width = 0;
222 break;
223 }
224 case DexFile::kDexAnnotationBoolean:
225 case DexFile::kDexAnnotationNull:
226 width = 0;
227 break;
228 default:
229 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
230 return false;
231 }
232
233 annotation += width;
234 *annotation_ptr = annotation;
235 return true;
236}
237
238const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
239 const uint8_t* annotation,
240 const char* name)
241 REQUIRES_SHARED(Locks::mutator_lock_) {
242 DecodeUnsignedLeb128(&annotation); // unused type_index
243 uint32_t size = DecodeUnsignedLeb128(&annotation);
244
245 while (size != 0) {
246 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800247 const char* element_name =
248 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700249 if (strcmp(name, element_name) == 0) {
250 return annotation;
251 }
252 SkipAnnotationValue(dex_file, &annotation);
253 size--;
254 }
255 return nullptr;
256}
257
258const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
259 REQUIRES_SHARED(Locks::mutator_lock_) {
260 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700261 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000262 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700263 if (annotations_dir == nullptr) {
264 return nullptr;
265 }
266 const DexFile::MethodAnnotationsItem* method_annotations =
267 dex_file->GetMethodAnnotations(annotations_dir);
268 if (method_annotations == nullptr) {
269 return nullptr;
270 }
271 uint32_t method_index = method->GetDexMethodIndex();
272 uint32_t method_count = annotations_dir->methods_size_;
273 for (uint32_t i = 0; i < method_count; ++i) {
274 if (method_annotations[i].method_idx_ == method_index) {
275 return dex_file->GetMethodAnnotationSetItem(method_annotations[i]);
276 }
277 }
278 return nullptr;
279}
280
281const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
282 REQUIRES_SHARED(Locks::mutator_lock_) {
283 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700284 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000285 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700286 if (annotations_dir == nullptr) {
287 return nullptr;
288 }
289 const DexFile::ParameterAnnotationsItem* parameter_annotations =
290 dex_file->GetParameterAnnotations(annotations_dir);
291 if (parameter_annotations == nullptr) {
292 return nullptr;
293 }
294 uint32_t method_index = method->GetDexMethodIndex();
295 uint32_t parameter_count = annotations_dir->parameters_size_;
296 for (uint32_t i = 0; i < parameter_count; ++i) {
297 if (parameter_annotations[i].method_idx_ == method_index) {
298 return &parameter_annotations[i];
299 }
300 }
301 return nullptr;
302}
303
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000304const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700305 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000306 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700307 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000308 dex_file.GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700309 if (annotations_dir == nullptr) {
310 return nullptr;
311 }
312 return dex_file.GetClassAnnotationSet(annotations_dir);
313}
314
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000315mirror::Object* ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700316 REQUIRES_SHARED(Locks::mutator_lock_) {
317 uint32_t type_index = DecodeUnsignedLeb128(annotation);
318 uint32_t size = DecodeUnsignedLeb128(annotation);
319
320 Thread* self = Thread::Current();
321 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000322 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700323 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
324 Handle<mirror::Class> annotation_class(hs.NewHandle(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000325 class_linker->ResolveType(klass.GetDexFile(),
326 dex::TypeIndex(type_index),
327 hs.NewHandle(klass.GetDexCache()),
328 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800329 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000330 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
331 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700332 DCHECK(Thread::Current()->IsExceptionPending());
333 Thread::Current()->ClearException();
334 return nullptr;
335 }
336
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700337 ObjPtr<mirror::Class> annotation_member_class =
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700338 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700339 mirror::Class* annotation_member_array_class =
340 class_linker->FindArrayClass(self, &annotation_member_class);
341 if (annotation_member_array_class == nullptr) {
342 return nullptr;
343 }
344 mirror::ObjectArray<mirror::Object>* element_array = nullptr;
345 if (size > 0) {
346 element_array =
347 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
348 if (element_array == nullptr) {
349 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
350 return nullptr;
351 }
352 }
353
354 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
355 for (uint32_t i = 0; i < size; ++i) {
356 mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation);
357 if (new_member == nullptr) {
358 return nullptr;
359 }
360 h_element_array->SetWithoutChecks<false>(i, new_member);
361 }
362
363 JValue result;
364 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800365 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700366 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
367 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
368 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
369 if (self->IsExceptionPending()) {
370 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
371 return nullptr;
372 }
373
374 return result.GetL();
375}
376
Andreas Gampe9486a162017-02-16 15:17:47 -0800377template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000378bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700379 const uint8_t** annotation_ptr,
380 DexFile::AnnotationValue* annotation_value,
381 Handle<mirror::Class> array_class,
382 DexFile::AnnotationResultStyle result_style)
383 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000384 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700385 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700386 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700387 bool set_object = false;
388 Primitive::Type primitive_type = Primitive::kPrimVoid;
389 const uint8_t* annotation = *annotation_ptr;
390 uint8_t header_byte = *(annotation++);
391 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
392 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
393 int32_t width = value_arg + 1;
394 annotation_value->type_ = value_type;
395
396 switch (value_type) {
397 case DexFile::kDexAnnotationByte:
398 annotation_value->value_.SetB(
399 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
400 primitive_type = Primitive::kPrimByte;
401 break;
402 case DexFile::kDexAnnotationShort:
403 annotation_value->value_.SetS(
404 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
405 primitive_type = Primitive::kPrimShort;
406 break;
407 case DexFile::kDexAnnotationChar:
408 annotation_value->value_.SetC(
409 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
410 primitive_type = Primitive::kPrimChar;
411 break;
412 case DexFile::kDexAnnotationInt:
413 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
414 primitive_type = Primitive::kPrimInt;
415 break;
416 case DexFile::kDexAnnotationLong:
417 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
418 primitive_type = Primitive::kPrimLong;
419 break;
420 case DexFile::kDexAnnotationFloat:
421 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
422 primitive_type = Primitive::kPrimFloat;
423 break;
424 case DexFile::kDexAnnotationDouble:
425 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
426 primitive_type = Primitive::kPrimDouble;
427 break;
428 case DexFile::kDexAnnotationBoolean:
429 annotation_value->value_.SetZ(value_arg != 0);
430 primitive_type = Primitive::kPrimBoolean;
431 width = 0;
432 break;
433 case DexFile::kDexAnnotationString: {
434 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
435 if (result_style == DexFile::kAllRaw) {
436 annotation_value->value_.SetI(index);
437 } else {
438 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700439 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000440 klass.GetDexFile(), dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700441 set_object = true;
442 if (element_object == nullptr) {
443 return false;
444 }
445 }
446 break;
447 }
448 case DexFile::kDexAnnotationType: {
449 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
450 if (result_style == DexFile::kAllRaw) {
451 annotation_value->value_.SetI(index);
452 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800453 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000454 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700455 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000456 klass.GetDexFile(),
457 type_index,
458 hs.NewHandle(klass.GetDexCache()),
459 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700460 set_object = true;
461 if (element_object == nullptr) {
462 CHECK(self->IsExceptionPending());
463 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800464 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700465 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
466 element_object = self->GetException();
467 self->ClearException();
468 } else {
469 return false;
470 }
471 }
472 }
473 break;
474 }
475 case DexFile::kDexAnnotationMethod: {
476 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
477 if (result_style == DexFile::kAllRaw) {
478 annotation_value->value_.SetI(index);
479 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000480 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000481 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700482 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000483 klass.GetDexFile(),
484 index,
485 hs.NewHandle(klass.GetDexCache()),
486 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700487 if (method == nullptr) {
488 return false;
489 }
490 PointerSize pointer_size = class_linker->GetImagePointerSize();
491 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700492 if (method->IsConstructor()) {
493 if (pointer_size == PointerSize::k64) {
494 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800495 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700496 } else {
497 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800498 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700499 }
500 } else {
501 if (pointer_size == PointerSize::k64) {
502 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800503 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700504 } else {
505 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800506 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700507 }
508 }
509 if (element_object == nullptr) {
510 return false;
511 }
512 }
513 break;
514 }
515 case DexFile::kDexAnnotationField: {
516 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
517 if (result_style == DexFile::kAllRaw) {
518 annotation_value->value_.SetI(index);
519 } else {
520 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700521 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000522 klass.GetDexFile(),
523 index,
524 hs.NewHandle(klass.GetDexCache()),
525 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700526 if (field == nullptr) {
527 return false;
528 }
529 set_object = true;
530 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
531 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800532 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
533 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700534 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800535 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
536 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700537 }
538 if (element_object == nullptr) {
539 return false;
540 }
541 }
542 break;
543 }
544 case DexFile::kDexAnnotationEnum: {
545 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
546 if (result_style == DexFile::kAllRaw) {
547 annotation_value->value_.SetI(index);
548 } else {
549 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700550 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000551 klass.GetDexFile(),
552 index,
553 hs.NewHandle(klass.GetDexCache()),
554 hs.NewHandle(klass.GetClassLoader()),
555 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700556 if (enum_field == nullptr) {
557 return false;
558 } else {
559 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
560 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
561 element_object = enum_field->GetObject(field_class.Get());
562 set_object = true;
563 }
564 }
565 break;
566 }
567 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800568 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700569 return false;
570 } else {
571 ScopedObjectAccessUnchecked soa(self);
572 StackHandleScope<2> hs(self);
573 uint32_t size = DecodeUnsignedLeb128(&annotation);
574 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
575 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
576 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
577 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800578 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700579 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
580 return false;
581 }
582 DexFile::AnnotationValue new_annotation_value;
583 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800584 if (!ProcessAnnotationValue<kTransactionActive>(klass,
585 &annotation,
586 &new_annotation_value,
587 component_type,
588 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700589 return false;
590 }
591 if (!component_type->IsPrimitive()) {
592 mirror::Object* obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800593 new_array->AsObjectArray<mirror::Object>()->
594 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700595 } else {
596 switch (new_annotation_value.type_) {
597 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800598 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700599 i, new_annotation_value.value_.GetB());
600 break;
601 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800602 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700603 i, new_annotation_value.value_.GetS());
604 break;
605 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800606 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700607 i, new_annotation_value.value_.GetC());
608 break;
609 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800610 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700611 i, new_annotation_value.value_.GetI());
612 break;
613 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800614 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700615 i, new_annotation_value.value_.GetJ());
616 break;
617 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800618 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700619 i, new_annotation_value.value_.GetF());
620 break;
621 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800622 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700623 i, new_annotation_value.value_.GetD());
624 break;
625 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800626 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700627 i, new_annotation_value.value_.GetZ());
628 break;
629 default:
630 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
631 return false;
632 }
633 }
634 }
635 element_object = new_array.Get();
636 set_object = true;
637 width = 0;
638 }
639 break;
640 case DexFile::kDexAnnotationAnnotation:
641 if (result_style == DexFile::kAllRaw) {
642 return false;
643 }
644 element_object = ProcessEncodedAnnotation(klass, &annotation);
645 if (element_object == nullptr) {
646 return false;
647 }
648 set_object = true;
649 width = 0;
650 break;
651 case DexFile::kDexAnnotationNull:
652 if (result_style == DexFile::kAllRaw) {
653 annotation_value->value_.SetI(0);
654 } else {
655 CHECK(element_object == nullptr);
656 set_object = true;
657 }
658 width = 0;
659 break;
660 default:
661 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
662 return false;
663 }
664
665 annotation += width;
666 *annotation_ptr = annotation;
667
668 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700669 element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700670 set_object = true;
671 }
672
673 if (set_object) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700674 annotation_value->value_.SetL(element_object.Ptr());
David Sehr9323e6e2016-09-13 08:58:35 -0700675 }
676
677 return true;
678}
679
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000680mirror::Object* CreateAnnotationMember(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700681 Handle<mirror::Class> annotation_class,
682 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000683 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700684 Thread* self = Thread::Current();
685 ScopedObjectAccessUnchecked soa(self);
686 StackHandleScope<5> hs(self);
687 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800688 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700689 Handle<mirror::String> string_name(
690 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
691
692 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
693 ArtMethod* annotation_method =
694 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
695 if (annotation_method == nullptr) {
696 return nullptr;
697 }
698 Handle<mirror::Class> method_return(hs.NewHandle(
Vladimir Marko942fd312017-01-16 20:52:19 +0000699 annotation_method->GetReturnType(true /* resolve */)));
David Sehr9323e6e2016-09-13 08:58:35 -0700700
701 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800702 if (!ProcessAnnotationValue<false>(klass,
703 annotation,
704 &annotation_value,
705 method_return,
706 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700707 return nullptr;
708 }
709 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
710
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700711 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700712 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
713 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
714 mirror::Method* method_obj_ptr;
715 DCHECK(!Runtime::Current()->IsActiveTransaction());
716 if (pointer_size == PointerSize::k64) {
717 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
718 self, annotation_method);
719 } else {
720 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
721 self, annotation_method);
722 }
723 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
724
Andreas Gampefa4333d2017-02-14 11:10:34 -0800725 if (new_member == nullptr || string_name == nullptr ||
726 method_object == nullptr || method_return == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700727 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
728 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
729 return nullptr;
730 }
731
732 JValue result;
733 ArtMethod* annotation_member_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800734 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
David Sehr9323e6e2016-09-13 08:58:35 -0700735 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
736 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
737 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
738 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
739 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
740 };
741 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
742 if (self->IsExceptionPending()) {
743 LOG(INFO) << "Exception in AnnotationMember.<init>";
744 return nullptr;
745 }
746
747 return new_member.Get();
748}
749
750const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000751 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700752 const DexFile::AnnotationSetItem* annotation_set,
753 uint32_t visibility,
754 Handle<mirror::Class> annotation_class)
755 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000756 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700757 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
758 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
759 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
760 continue;
761 }
762 const uint8_t* annotation = annotation_item->annotation_;
763 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000764 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -0700765 mirror::Class* resolved_class = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000766 klass.GetDexFile(),
767 dex::TypeIndex(type_index),
768 hs.NewHandle(klass.GetDexCache()),
769 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700770 if (resolved_class == nullptr) {
771 std::string temp;
772 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000773 klass.GetRealClass()->GetDescriptor(&temp), type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700774 CHECK(Thread::Current()->IsExceptionPending());
775 Thread::Current()->ClearException();
776 continue;
777 }
778 if (resolved_class == annotation_class.Get()) {
779 return annotation_item;
780 }
781 }
782
783 return nullptr;
784}
785
786mirror::Object* GetAnnotationObjectFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000787 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700788 const DexFile::AnnotationSetItem* annotation_set,
789 uint32_t visibility,
790 Handle<mirror::Class> annotation_class)
791 REQUIRES_SHARED(Locks::mutator_lock_) {
792 const DexFile::AnnotationItem* annotation_item =
793 GetAnnotationItemFromAnnotationSet(klass, annotation_set, visibility, annotation_class);
794 if (annotation_item == nullptr) {
795 return nullptr;
796 }
797 const uint8_t* annotation = annotation_item->annotation_;
798 return ProcessEncodedAnnotation(klass, &annotation);
799}
800
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000801mirror::Object* GetAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700802 const DexFile::AnnotationItem* annotation_item,
803 const char* annotation_name,
804 Handle<mirror::Class> array_class,
805 uint32_t expected_type)
806 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000807 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700808 const uint8_t* annotation =
809 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
810 if (annotation == nullptr) {
811 return nullptr;
812 }
813 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800814 bool result = Runtime::Current()->IsActiveTransaction()
815 ? ProcessAnnotationValue<true>(klass,
816 &annotation,
817 &annotation_value,
818 array_class,
819 DexFile::kAllObjects)
820 : ProcessAnnotationValue<false>(klass,
821 &annotation,
822 &annotation_value,
823 array_class,
824 DexFile::kAllObjects);
825 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700826 return nullptr;
827 }
828 if (annotation_value.type_ != expected_type) {
829 return nullptr;
830 }
831 return annotation_value.value_.GetL();
832}
833
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000834mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700835 const DexFile::AnnotationSetItem* annotation_set)
836 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000837 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700838 StackHandleScope<1> hs(Thread::Current());
839 const DexFile::AnnotationItem* annotation_item =
840 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
841 DexFile::kDexVisibilitySystem);
842 if (annotation_item == nullptr) {
843 return nullptr;
844 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700845 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
David Sehr9323e6e2016-09-13 08:58:35 -0700846 Handle<mirror::Class> string_array_class(hs.NewHandle(
847 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800848 if (string_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700849 return nullptr;
850 }
851 mirror::Object* obj =
852 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
853 DexFile::kDexAnnotationArray);
854 if (obj == nullptr) {
855 return nullptr;
856 }
857 return obj->AsObjectArray<mirror::String>();
858}
859
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000860mirror::ObjectArray<mirror::Class>* GetThrowsValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700861 const DexFile::AnnotationSetItem* annotation_set)
862 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000863 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700864 StackHandleScope<1> hs(Thread::Current());
865 const DexFile::AnnotationItem* annotation_item =
866 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
867 DexFile::kDexVisibilitySystem);
868 if (annotation_item == nullptr) {
869 return nullptr;
870 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700871 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -0700872 Handle<mirror::Class> class_array_class(hs.NewHandle(
873 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800874 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700875 return nullptr;
876 }
877 mirror::Object* obj =
878 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
879 DexFile::kDexAnnotationArray);
880 if (obj == nullptr) {
881 return nullptr;
882 }
883 return obj->AsObjectArray<mirror::Class>();
884}
885
886mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000887 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700888 const DexFile::AnnotationSetItem* annotation_set,
889 uint32_t visibility)
890 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000891 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700892 Thread* self = Thread::Current();
893 ScopedObjectAccessUnchecked soa(self);
894 StackHandleScope<2> hs(self);
895 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700896 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700897 if (annotation_set == nullptr) {
898 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
899 }
900
901 uint32_t size = annotation_set->size_;
902 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
903 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800904 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700905 return nullptr;
906 }
907
908 uint32_t dest_index = 0;
909 for (uint32_t i = 0; i < size; ++i) {
910 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
911 // Note that we do not use IsVisibilityCompatible here because older code
912 // was correct for this case.
913 if (annotation_item->visibility_ != visibility) {
914 continue;
915 }
916 const uint8_t* annotation = annotation_item->annotation_;
917 mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
918 if (annotation_obj != nullptr) {
919 result->SetWithoutChecks<false>(dest_index, annotation_obj);
920 ++dest_index;
921 } else if (self->IsExceptionPending()) {
922 return nullptr;
923 }
924 }
925
926 if (dest_index == size) {
927 return result.Get();
928 }
929
930 mirror::ObjectArray<mirror::Object>* trimmed_result =
931 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
932 if (trimmed_result == nullptr) {
933 return nullptr;
934 }
935
936 for (uint32_t i = 0; i < dest_index; ++i) {
937 mirror::Object* obj = result->GetWithoutChecks(i);
938 trimmed_result->SetWithoutChecks<false>(i, obj);
939 }
940
941 return trimmed_result;
942}
943
944mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000945 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700946 const DexFile::AnnotationSetRefList* set_ref_list,
947 uint32_t size)
948 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000949 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700950 Thread* self = Thread::Current();
951 ScopedObjectAccessUnchecked soa(self);
952 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700953 ObjPtr<mirror::Class> annotation_array_class =
954 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
David Sehr9323e6e2016-09-13 08:58:35 -0700955 mirror::Class* annotation_array_array_class =
956 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
957 if (annotation_array_array_class == nullptr) {
958 return nullptr;
959 }
960 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
961 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800962 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700963 LOG(ERROR) << "Annotation set ref array allocation failed";
964 return nullptr;
965 }
966 for (uint32_t index = 0; index < size; ++index) {
967 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
968 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
969 mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item,
970 DexFile::kDexVisibilityRuntime);
971 if (annotation_set == nullptr) {
972 return nullptr;
973 }
974 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
975 }
976 return annotation_array_array.Get();
977}
978} // namespace
979
980namespace annotations {
981
982mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) {
983 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
984 if (annotation_set == nullptr) {
985 return nullptr;
986 }
987 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000988 const ClassData field_class(hs, field);
989 return GetAnnotationObjectFromAnnotationSet(field_class,
990 annotation_set,
991 DexFile::kDexVisibilityRuntime,
992 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700993}
994
995mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) {
996 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
997 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000998 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -0700999 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1000}
1001
1002mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
1003 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1004 if (annotation_set == nullptr) {
1005 return nullptr;
1006 }
1007 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001008 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001009 return GetSignatureValue(field_class, annotation_set);
1010}
1011
1012bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1013 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1014 if (annotation_set == nullptr) {
1015 return false;
1016 }
1017 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001018 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001019 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1020 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1021 return annotation_item != nullptr;
1022}
1023
1024mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001025 const ClassData klass(method);
1026 const DexFile* dex_file = &klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -07001027 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001028 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001029 if (annotations_dir == nullptr) {
1030 return nullptr;
1031 }
1032 const DexFile::AnnotationSetItem* annotation_set =
1033 dex_file->GetClassAnnotationSet(annotations_dir);
1034 if (annotation_set == nullptr) {
1035 return nullptr;
1036 }
1037 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1038 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1039 if (annotation_item == nullptr) {
1040 return nullptr;
1041 }
1042 const uint8_t* annotation =
1043 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1044 if (annotation == nullptr) {
1045 return nullptr;
1046 }
1047 uint8_t header_byte = *(annotation++);
1048 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1049 return nullptr;
1050 }
1051 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1052 if (annotation == nullptr) {
1053 return nullptr;
1054 }
1055 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001056 StackHandleScope<1> hs(Thread::Current());
Vladimir Marko942fd312017-01-16 20:52:19 +00001057 Handle<mirror::Class> return_type(hs.NewHandle(method->GetReturnType(true /* resolve */)));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001058 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001059 &annotation,
1060 &annotation_value,
1061 return_type,
1062 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001063 return nullptr;
1064 }
1065 return annotation_value.value_.GetL();
1066}
1067
1068mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
1069 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1070 if (annotation_set == nullptr) {
1071 return nullptr;
1072 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001073 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001074 DexFile::kDexVisibilityRuntime, annotation_class);
1075}
1076
1077mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
1078 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001079 return ProcessAnnotationSet(ClassData(method),
1080 annotation_set,
1081 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001082}
1083
1084mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
1085 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1086 if (annotation_set == nullptr) {
1087 return nullptr;
1088 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001089 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001090}
1091
1092mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
1093 const DexFile* dex_file = method->GetDexFile();
1094 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1095 FindAnnotationsItemForMethod(method);
1096 if (parameter_annotations == nullptr) {
1097 return nullptr;
1098 }
1099 const DexFile::AnnotationSetRefList* set_ref_list =
1100 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1101 if (set_ref_list == nullptr) {
1102 return nullptr;
1103 }
1104 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001105 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001106}
1107
1108mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1109 uint32_t parameter_idx,
1110 Handle<mirror::Class> annotation_class) {
1111 const DexFile* dex_file = method->GetDexFile();
1112 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1113 FindAnnotationsItemForMethod(method);
1114 if (parameter_annotations == nullptr) {
1115 return nullptr;
1116 }
1117 const DexFile::AnnotationSetRefList* set_ref_list =
1118 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1119 if (set_ref_list == nullptr) {
1120 return nullptr;
1121 }
1122 if (parameter_idx >= set_ref_list->size_) {
1123 return nullptr;
1124 }
1125 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1126 const DexFile::AnnotationSetItem* annotation_set =
1127 dex_file->GetSetRefItemItem(annotation_set_ref);
1128
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001129 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001130 annotation_set,
1131 DexFile::kDexVisibilityRuntime,
1132 annotation_class);
1133}
1134
Neil Fuller79a21e72016-09-09 14:24:51 +01001135bool GetParametersMetadataForMethod(ArtMethod* method,
1136 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1137 MutableHandle<mirror::IntArray>* access_flags) {
1138 const DexFile::AnnotationSetItem::AnnotationSetItem* annotation_set =
1139 FindAnnotationSetForMethod(method);
1140 if (annotation_set == nullptr) {
1141 return false;
1142 }
1143
1144 const DexFile* dex_file = method->GetDexFile();
1145 const DexFile::AnnotationItem* annotation_item =
1146 SearchAnnotationSet(*dex_file,
1147 annotation_set,
1148 "Ldalvik/annotation/MethodParameters;",
1149 DexFile::kDexVisibilitySystem);
1150 if (annotation_item == nullptr) {
1151 return false;
1152 }
1153
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001154 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001155
1156 // Extract the parameters' names String[].
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001157 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
Neil Fuller79a21e72016-09-09 14:24:51 +01001158 Handle<mirror::Class> string_array_class(hs.NewHandle(
1159 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001160 if (UNLIKELY(string_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001161 return false;
1162 }
1163
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001164 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001165 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001166 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001167 annotation_item,
1168 "names",
1169 string_array_class,
1170 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001171 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001172 return false;
1173 }
1174
1175 // Extract the parameters' access flags int[].
1176 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001177 if (UNLIKELY(int_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001178 return false;
1179 }
1180 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001181 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001182 annotation_item,
1183 "accessFlags",
1184 int_array_class,
1185 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001186 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001187 return false;
1188 }
1189
1190 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1191 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1192 return true;
1193}
1194
David Sehr9323e6e2016-09-13 08:58:35 -07001195mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1196 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1197 if (annotation_set == nullptr) {
1198 return nullptr;
1199 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001200 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001201}
1202
1203bool IsMethodAnnotationPresent(ArtMethod* method, Handle<mirror::Class> annotation_class,
1204 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
1205 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1206 if (annotation_set == nullptr) {
1207 return false;
1208 }
David Sehr9323e6e2016-09-13 08:58:35 -07001209 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001210 GetAnnotationItemFromAnnotationSet(ClassData(method),
1211 annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001212 return annotation_item != nullptr;
1213}
1214
1215mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1216 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001217 ClassData data(klass);
1218 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001219 if (annotation_set == nullptr) {
1220 return nullptr;
1221 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001222 return GetAnnotationObjectFromAnnotationSet(data,
1223 annotation_set,
1224 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001225 annotation_class);
1226}
1227
1228mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001229 ClassData data(klass);
1230 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1231 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001232}
1233
1234mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001235 ClassData data(klass);
1236 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001237 if (annotation_set == nullptr) {
1238 return nullptr;
1239 }
1240 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001241 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001242 DexFile::kDexVisibilitySystem);
1243 if (annotation_item == nullptr) {
1244 return nullptr;
1245 }
1246 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001247 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -07001248 Handle<mirror::Class> class_array_class(hs.NewHandle(
1249 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001250 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -07001251 return nullptr;
1252 }
1253 mirror::Object* obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001254 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001255 DexFile::kDexAnnotationArray);
1256 if (obj == nullptr) {
1257 return nullptr;
1258 }
1259 return obj->AsObjectArray<mirror::Class>();
1260}
1261
1262mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001263 ClassData data(klass);
1264 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001265 if (annotation_set == nullptr) {
1266 return nullptr;
1267 }
1268 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001269 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001270 DexFile::kDexVisibilitySystem);
1271 if (annotation_item == nullptr) {
1272 return nullptr;
1273 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001274 mirror::Object* obj = GetAnnotationValue(data, annotation_item, "value",
David Sehr9323e6e2016-09-13 08:58:35 -07001275 ScopedNullHandle<mirror::Class>(),
1276 DexFile::kDexAnnotationType);
1277 if (obj == nullptr) {
1278 return nullptr;
1279 }
1280 return obj->AsClass();
1281}
1282
1283mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
David Sehr9323e6e2016-09-13 08:58:35 -07001284 mirror::Class* declaring_class = GetDeclaringClass(klass);
1285 if (declaring_class != nullptr) {
1286 return declaring_class;
1287 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001288 ClassData data(klass);
1289 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001290 if (annotation_set == nullptr) {
1291 return nullptr;
1292 }
1293 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001294 SearchAnnotationSet(data.GetDexFile(),
1295 annotation_set,
1296 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001297 DexFile::kDexVisibilitySystem);
1298 if (annotation_item == nullptr) {
1299 return nullptr;
1300 }
1301 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001302 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001303 if (annotation == nullptr) {
1304 return nullptr;
1305 }
1306 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001307 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001308 &annotation,
1309 &annotation_value,
1310 ScopedNullHandle<mirror::Class>(),
1311 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001312 return nullptr;
1313 }
1314 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1315 return nullptr;
1316 }
1317 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001318 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001319 data.GetDexFile(),
1320 annotation_value.value_.GetI(),
1321 hs.NewHandle(data.GetDexCache()),
1322 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001323 if (method == nullptr) {
1324 return nullptr;
1325 }
1326 return method->GetDeclaringClass();
1327}
1328
1329mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001330 ClassData data(klass);
1331 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001332 if (annotation_set == nullptr) {
1333 return nullptr;
1334 }
1335 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001336 SearchAnnotationSet(data.GetDexFile(),
1337 annotation_set,
1338 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001339 DexFile::kDexVisibilitySystem);
1340 if (annotation_item == nullptr) {
1341 return nullptr;
1342 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001343 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001344 DexFile::kDexAnnotationMethod);
1345}
1346
1347bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001348 ClassData data(klass);
1349 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001350 if (annotation_set == nullptr) {
1351 return false;
1352 }
1353 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001354 data.GetDexFile(),
1355 annotation_set,
1356 "Ldalvik/annotation/InnerClass;",
1357 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001358 if (annotation_item == nullptr) {
1359 return false;
1360 }
1361 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001362 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001363 if (annotation == nullptr) {
1364 return false;
1365 }
1366 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001367 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001368 &annotation,
1369 &annotation_value,
1370 ScopedNullHandle<mirror::Class>(),
1371 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001372 return false;
1373 }
1374 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1375 annotation_value.type_ != DexFile::kDexAnnotationString) {
1376 return false;
1377 }
1378 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1379 return true;
1380}
1381
1382bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001383 ClassData data(klass);
1384 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001385 if (annotation_set == nullptr) {
1386 return false;
1387 }
1388 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001389 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001390 DexFile::kDexVisibilitySystem);
1391 if (annotation_item == nullptr) {
1392 return false;
1393 }
1394 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001395 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001396 if (annotation == nullptr) {
1397 return false;
1398 }
1399 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001400 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001401 &annotation,
1402 &annotation_value,
1403 ScopedNullHandle<mirror::Class>(),
1404 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001405 return false;
1406 }
1407 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1408 return false;
1409 }
1410 *flags = annotation_value.value_.GetI();
1411 return true;
1412}
1413
1414mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001415 ClassData data(klass);
1416 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001417 if (annotation_set == nullptr) {
1418 return nullptr;
1419 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001420 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001421}
1422
1423bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001424 ClassData data(klass);
1425 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001426 if (annotation_set == nullptr) {
1427 return false;
1428 }
1429 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001430 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001431 return annotation_item != nullptr;
1432}
1433
1434int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1435 // For native method, lineno should be -2 to indicate it is native. Note that
1436 // "line number == -2" is how libcore tells from StackTraceElement.
1437 if (method->GetCodeItemOffset() == 0) {
1438 return -2;
1439 }
1440
1441 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
David Sehr709b0702016-10-13 09:12:37 -07001442 DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001443
1444 // A method with no line number info should return -1
1445 DexFile::LineNumFromPcContext context(rel_pc, -1);
1446 dex_file->DecodeDebugPositionInfo(code_item, DexFile::LineNumForPcCb, &context);
1447 return context.line_num_;
1448}
1449
1450template<bool kTransactionActive>
1451void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1452 DCHECK(dex_cache_ != nullptr);
1453 DCHECK(class_loader_ != nullptr);
1454 switch (type_) {
1455 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1456 break;
1457 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1458 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1459 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1460 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1461 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1462 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1463 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1464 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1465 case kString: {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001466 mirror::String* resolved = linker_->ResolveString(dex_file_,
1467 dex::StringIndex(jval_.i),
1468 *dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001469 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1470 break;
1471 }
1472 case kType: {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001473 mirror::Class* resolved = linker_->ResolveType(dex_file_,
1474 dex::TypeIndex(jval_.i),
1475 *dex_cache_,
David Sehr9323e6e2016-09-13 08:58:35 -07001476 *class_loader_);
1477 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1478 break;
1479 }
1480 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1481 }
1482}
1483template
1484void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1485template
1486void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1487
1488} // namespace annotations
1489
1490} // namespace art