blob: 27b9202d259cb0dda492b7d90f7af565bc8079e1 [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"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000031#include "oat_file.h"
David Sehr9323e6e2016-09-13 08:58:35 -070032#include "reflection.h"
33#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070034#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070035
36namespace art {
37
Andreas Gampe46ee31b2016-12-14 10:11:49 -080038using android::base::StringPrintf;
39
David Sehr9323e6e2016-09-13 08:58:35 -070040struct DexFile::AnnotationValue {
41 JValue value_;
42 uint8_t type_;
43};
44
45namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000046
47// A helper class that contains all the data needed to do annotation lookup.
48class ClassData {
49 public:
50 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
51 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
52 method,
53 *method->GetDexFile(),
54 &method->GetClassDef()) {}
55
56 // Requires Scope to be able to create at least 1 handles.
57 template <typename Scope>
58 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
59 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
60
61 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
62 : ClassData(klass, // klass
63 nullptr, // method
64 klass->GetDexFile(),
65 klass->GetClassDef()) {}
66
67 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
68 return dex_file_;
69 }
70
71 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
72 return class_def_;
73 }
74
75 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
76 if (method_ != nullptr) {
77 return method_->GetDexCache();
78 } else {
79 return real_klass_->GetDexCache();
80 }
81 }
82
83 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
84 if (method_ != nullptr) {
85 return method_->GetDeclaringClass()->GetClassLoader();
86 } else {
87 return real_klass_->GetClassLoader();
88 }
89 }
90
91 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
92 if (method_ != nullptr) {
93 return method_->GetDeclaringClass();
94 } else {
95 return real_klass_.Get();
96 }
97 }
98
99 private:
100 ClassData(Handle<mirror::Class> klass,
101 ArtMethod* method,
102 const DexFile& dex_file,
103 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
104 : real_klass_(klass),
105 method_(method),
106 dex_file_(dex_file),
107 class_def_(class_def) {
108 DCHECK((method_ == nullptr) || real_klass_.IsNull());
109 }
110
111 Handle<mirror::Class> real_klass_;
112 ArtMethod* method_;
113 const DexFile& dex_file_;
114 const DexFile::ClassDef* class_def_;
115
116 DISALLOW_COPY_AND_ASSIGN(ClassData);
117};
118
119mirror::Object* CreateAnnotationMember(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700120 Handle<mirror::Class> annotation_class,
121 const uint8_t** annotation)
122 REQUIRES_SHARED(Locks::mutator_lock_);
123
124bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
125 if (expected == DexFile::kDexVisibilityRuntime) {
126 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
127 if (sdk_version > 0 && sdk_version <= 23) {
128 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
129 }
130 }
131 return actual == expected;
132}
133
134const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
135 REQUIRES_SHARED(Locks::mutator_lock_) {
136 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700137 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Alex Light89f33b82017-10-23 16:37:03 -0700138 const DexFile::ClassDef* class_def = klass->GetClassDef();
139 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700140 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700141 return nullptr;
142 }
David Sehr9323e6e2016-09-13 08:58:35 -0700143 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700144 dex_file->GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700145 if (annotations_dir == nullptr) {
146 return nullptr;
147 }
148 const DexFile::FieldAnnotationsItem* field_annotations =
149 dex_file->GetFieldAnnotations(annotations_dir);
150 if (field_annotations == nullptr) {
151 return nullptr;
152 }
153 uint32_t field_index = field->GetDexFieldIndex();
154 uint32_t field_count = annotations_dir->fields_size_;
155 for (uint32_t i = 0; i < field_count; ++i) {
156 if (field_annotations[i].field_idx_ == field_index) {
157 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
158 }
159 }
160 return nullptr;
161}
162
163const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
164 const DexFile::AnnotationSetItem* annotation_set,
165 const char* descriptor,
166 uint32_t visibility)
167 REQUIRES_SHARED(Locks::mutator_lock_) {
168 const DexFile::AnnotationItem* result = nullptr;
169 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
170 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
171 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
172 continue;
173 }
174 const uint8_t* annotation = annotation_item->annotation_;
175 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
176
Andreas Gampea5b09a62016-11-17 15:21:22 -0800177 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700178 result = annotation_item;
179 break;
180 }
181 }
182 return result;
183}
184
185bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
186 REQUIRES_SHARED(Locks::mutator_lock_) {
187 const uint8_t* annotation = *annotation_ptr;
188 uint8_t header_byte = *(annotation++);
189 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
190 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
191 int32_t width = value_arg + 1;
192
193 switch (value_type) {
194 case DexFile::kDexAnnotationByte:
195 case DexFile::kDexAnnotationShort:
196 case DexFile::kDexAnnotationChar:
197 case DexFile::kDexAnnotationInt:
198 case DexFile::kDexAnnotationLong:
199 case DexFile::kDexAnnotationFloat:
200 case DexFile::kDexAnnotationDouble:
201 case DexFile::kDexAnnotationString:
202 case DexFile::kDexAnnotationType:
203 case DexFile::kDexAnnotationMethod:
204 case DexFile::kDexAnnotationField:
205 case DexFile::kDexAnnotationEnum:
206 break;
207 case DexFile::kDexAnnotationArray:
208 {
209 uint32_t size = DecodeUnsignedLeb128(&annotation);
210 while (size--) {
211 if (!SkipAnnotationValue(dex_file, &annotation)) {
212 return false;
213 }
214 }
215 width = 0;
216 break;
217 }
218 case DexFile::kDexAnnotationAnnotation:
219 {
220 DecodeUnsignedLeb128(&annotation); // unused type_index
221 uint32_t size = DecodeUnsignedLeb128(&annotation);
222 while (size--) {
223 DecodeUnsignedLeb128(&annotation); // unused element_name_index
224 if (!SkipAnnotationValue(dex_file, &annotation)) {
225 return false;
226 }
227 }
228 width = 0;
229 break;
230 }
231 case DexFile::kDexAnnotationBoolean:
232 case DexFile::kDexAnnotationNull:
233 width = 0;
234 break;
235 default:
236 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
237 return false;
238 }
239
240 annotation += width;
241 *annotation_ptr = annotation;
242 return true;
243}
244
245const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
246 const uint8_t* annotation,
247 const char* name)
248 REQUIRES_SHARED(Locks::mutator_lock_) {
249 DecodeUnsignedLeb128(&annotation); // unused type_index
250 uint32_t size = DecodeUnsignedLeb128(&annotation);
251
252 while (size != 0) {
253 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800254 const char* element_name =
255 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700256 if (strcmp(name, element_name) == 0) {
257 return annotation;
258 }
259 SkipAnnotationValue(dex_file, &annotation);
260 size--;
261 }
262 return nullptr;
263}
264
Vladimir Marko0db16e02017-11-08 14:32:33 +0000265const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
266 const DexFile::ClassDef& class_def,
267 uint32_t method_index) {
David Sehr9323e6e2016-09-13 08:58:35 -0700268 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000269 dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700270 if (annotations_dir == nullptr) {
271 return nullptr;
272 }
273 const DexFile::MethodAnnotationsItem* method_annotations =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000274 dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700275 if (method_annotations == nullptr) {
276 return nullptr;
277 }
David Sehr9323e6e2016-09-13 08:58:35 -0700278 uint32_t method_count = annotations_dir->methods_size_;
279 for (uint32_t i = 0; i < method_count; ++i) {
280 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000281 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700282 }
283 }
284 return nullptr;
285}
286
Vladimir Marko0db16e02017-11-08 14:32:33 +0000287inline const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
288 REQUIRES_SHARED(Locks::mutator_lock_) {
289 if (method->IsProxyMethod()) {
290 return nullptr;
291 }
292 return FindAnnotationSetForMethod(*method->GetDexFile(),
293 method->GetClassDef(),
294 method->GetDexMethodIndex());
295}
296
David Sehr9323e6e2016-09-13 08:58:35 -0700297const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
298 REQUIRES_SHARED(Locks::mutator_lock_) {
299 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700300 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000301 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700302 if (annotations_dir == nullptr) {
303 return nullptr;
304 }
305 const DexFile::ParameterAnnotationsItem* parameter_annotations =
306 dex_file->GetParameterAnnotations(annotations_dir);
307 if (parameter_annotations == nullptr) {
308 return nullptr;
309 }
310 uint32_t method_index = method->GetDexMethodIndex();
311 uint32_t parameter_count = annotations_dir->parameters_size_;
312 for (uint32_t i = 0; i < parameter_count; ++i) {
313 if (parameter_annotations[i].method_idx_ == method_index) {
314 return &parameter_annotations[i];
315 }
316 }
317 return nullptr;
318}
319
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000320const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700321 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000322 const DexFile& dex_file = klass.GetDexFile();
Alex Light89f33b82017-10-23 16:37:03 -0700323 const DexFile::ClassDef* class_def = klass.GetClassDef();
324 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700325 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700326 return nullptr;
327 }
David Sehr9323e6e2016-09-13 08:58:35 -0700328 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700329 dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700330 if (annotations_dir == nullptr) {
331 return nullptr;
332 }
333 return dex_file.GetClassAnnotationSet(annotations_dir);
334}
335
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000336mirror::Object* ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700337 REQUIRES_SHARED(Locks::mutator_lock_) {
338 uint32_t type_index = DecodeUnsignedLeb128(annotation);
339 uint32_t size = DecodeUnsignedLeb128(annotation);
340
341 Thread* self = Thread::Current();
342 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000343 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700344 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
345 Handle<mirror::Class> annotation_class(hs.NewHandle(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000346 class_linker->ResolveType(klass.GetDexFile(),
347 dex::TypeIndex(type_index),
348 hs.NewHandle(klass.GetDexCache()),
349 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800350 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000351 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
352 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700353 DCHECK(Thread::Current()->IsExceptionPending());
354 Thread::Current()->ClearException();
355 return nullptr;
356 }
357
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700358 ObjPtr<mirror::Class> annotation_member_class =
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700359 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700360 mirror::Class* annotation_member_array_class =
361 class_linker->FindArrayClass(self, &annotation_member_class);
362 if (annotation_member_array_class == nullptr) {
363 return nullptr;
364 }
365 mirror::ObjectArray<mirror::Object>* element_array = nullptr;
366 if (size > 0) {
367 element_array =
368 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
369 if (element_array == nullptr) {
370 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
371 return nullptr;
372 }
373 }
374
375 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
376 for (uint32_t i = 0; i < size; ++i) {
377 mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation);
378 if (new_member == nullptr) {
379 return nullptr;
380 }
381 h_element_array->SetWithoutChecks<false>(i, new_member);
382 }
383
384 JValue result;
385 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800386 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700387 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
388 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
389 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
390 if (self->IsExceptionPending()) {
391 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
392 return nullptr;
393 }
394
395 return result.GetL();
396}
397
Andreas Gampe9486a162017-02-16 15:17:47 -0800398template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000399bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700400 const uint8_t** annotation_ptr,
401 DexFile::AnnotationValue* annotation_value,
402 Handle<mirror::Class> array_class,
403 DexFile::AnnotationResultStyle result_style)
404 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000405 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700406 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700407 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700408 bool set_object = false;
409 Primitive::Type primitive_type = Primitive::kPrimVoid;
410 const uint8_t* annotation = *annotation_ptr;
411 uint8_t header_byte = *(annotation++);
412 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
413 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
414 int32_t width = value_arg + 1;
415 annotation_value->type_ = value_type;
416
417 switch (value_type) {
418 case DexFile::kDexAnnotationByte:
419 annotation_value->value_.SetB(
420 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
421 primitive_type = Primitive::kPrimByte;
422 break;
423 case DexFile::kDexAnnotationShort:
424 annotation_value->value_.SetS(
425 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
426 primitive_type = Primitive::kPrimShort;
427 break;
428 case DexFile::kDexAnnotationChar:
429 annotation_value->value_.SetC(
430 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
431 primitive_type = Primitive::kPrimChar;
432 break;
433 case DexFile::kDexAnnotationInt:
434 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
435 primitive_type = Primitive::kPrimInt;
436 break;
437 case DexFile::kDexAnnotationLong:
438 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
439 primitive_type = Primitive::kPrimLong;
440 break;
441 case DexFile::kDexAnnotationFloat:
442 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
443 primitive_type = Primitive::kPrimFloat;
444 break;
445 case DexFile::kDexAnnotationDouble:
446 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
447 primitive_type = Primitive::kPrimDouble;
448 break;
449 case DexFile::kDexAnnotationBoolean:
450 annotation_value->value_.SetZ(value_arg != 0);
451 primitive_type = Primitive::kPrimBoolean;
452 width = 0;
453 break;
454 case DexFile::kDexAnnotationString: {
455 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
456 if (result_style == DexFile::kAllRaw) {
457 annotation_value->value_.SetI(index);
458 } else {
459 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700460 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000461 klass.GetDexFile(), dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700462 set_object = true;
463 if (element_object == nullptr) {
464 return false;
465 }
466 }
467 break;
468 }
469 case DexFile::kDexAnnotationType: {
470 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
471 if (result_style == DexFile::kAllRaw) {
472 annotation_value->value_.SetI(index);
473 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800474 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000475 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700476 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000477 klass.GetDexFile(),
478 type_index,
479 hs.NewHandle(klass.GetDexCache()),
480 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700481 set_object = true;
482 if (element_object == nullptr) {
483 CHECK(self->IsExceptionPending());
484 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800485 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700486 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
487 element_object = self->GetException();
488 self->ClearException();
489 } else {
490 return false;
491 }
492 }
493 }
494 break;
495 }
496 case DexFile::kDexAnnotationMethod: {
497 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
498 if (result_style == DexFile::kAllRaw) {
499 annotation_value->value_.SetI(index);
500 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000501 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000502 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700503 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000504 klass.GetDexFile(),
505 index,
506 hs.NewHandle(klass.GetDexCache()),
507 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700508 if (method == nullptr) {
509 return false;
510 }
511 PointerSize pointer_size = class_linker->GetImagePointerSize();
512 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700513 if (method->IsConstructor()) {
514 if (pointer_size == PointerSize::k64) {
515 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800516 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700517 } else {
518 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800519 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700520 }
521 } else {
522 if (pointer_size == PointerSize::k64) {
523 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800524 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700525 } else {
526 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800527 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700528 }
529 }
530 if (element_object == nullptr) {
531 return false;
532 }
533 }
534 break;
535 }
536 case DexFile::kDexAnnotationField: {
537 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
538 if (result_style == DexFile::kAllRaw) {
539 annotation_value->value_.SetI(index);
540 } else {
541 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700542 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000543 klass.GetDexFile(),
544 index,
545 hs.NewHandle(klass.GetDexCache()),
546 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700547 if (field == nullptr) {
548 return false;
549 }
550 set_object = true;
551 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
552 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800553 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
554 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700555 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800556 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
557 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700558 }
559 if (element_object == nullptr) {
560 return false;
561 }
562 }
563 break;
564 }
565 case DexFile::kDexAnnotationEnum: {
566 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
567 if (result_style == DexFile::kAllRaw) {
568 annotation_value->value_.SetI(index);
569 } else {
570 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700571 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000572 klass.GetDexFile(),
573 index,
574 hs.NewHandle(klass.GetDexCache()),
575 hs.NewHandle(klass.GetClassLoader()),
576 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700577 if (enum_field == nullptr) {
578 return false;
579 } else {
580 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
581 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
582 element_object = enum_field->GetObject(field_class.Get());
583 set_object = true;
584 }
585 }
586 break;
587 }
588 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800589 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700590 return false;
591 } else {
592 ScopedObjectAccessUnchecked soa(self);
593 StackHandleScope<2> hs(self);
594 uint32_t size = DecodeUnsignedLeb128(&annotation);
595 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
596 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
597 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
598 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800599 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700600 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
601 return false;
602 }
603 DexFile::AnnotationValue new_annotation_value;
604 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800605 if (!ProcessAnnotationValue<kTransactionActive>(klass,
606 &annotation,
607 &new_annotation_value,
608 component_type,
609 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700610 return false;
611 }
612 if (!component_type->IsPrimitive()) {
613 mirror::Object* obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800614 new_array->AsObjectArray<mirror::Object>()->
615 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700616 } else {
617 switch (new_annotation_value.type_) {
618 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800619 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700620 i, new_annotation_value.value_.GetB());
621 break;
622 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800623 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700624 i, new_annotation_value.value_.GetS());
625 break;
626 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800627 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700628 i, new_annotation_value.value_.GetC());
629 break;
630 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800631 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700632 i, new_annotation_value.value_.GetI());
633 break;
634 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800635 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700636 i, new_annotation_value.value_.GetJ());
637 break;
638 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800639 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700640 i, new_annotation_value.value_.GetF());
641 break;
642 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800643 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700644 i, new_annotation_value.value_.GetD());
645 break;
646 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800647 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700648 i, new_annotation_value.value_.GetZ());
649 break;
650 default:
651 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
652 return false;
653 }
654 }
655 }
656 element_object = new_array.Get();
657 set_object = true;
658 width = 0;
659 }
660 break;
661 case DexFile::kDexAnnotationAnnotation:
662 if (result_style == DexFile::kAllRaw) {
663 return false;
664 }
665 element_object = ProcessEncodedAnnotation(klass, &annotation);
666 if (element_object == nullptr) {
667 return false;
668 }
669 set_object = true;
670 width = 0;
671 break;
672 case DexFile::kDexAnnotationNull:
673 if (result_style == DexFile::kAllRaw) {
674 annotation_value->value_.SetI(0);
675 } else {
676 CHECK(element_object == nullptr);
677 set_object = true;
678 }
679 width = 0;
680 break;
681 default:
682 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
683 return false;
684 }
685
686 annotation += width;
687 *annotation_ptr = annotation;
688
689 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700690 element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr();
David Sehr9323e6e2016-09-13 08:58:35 -0700691 set_object = true;
692 }
693
694 if (set_object) {
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700695 annotation_value->value_.SetL(element_object.Ptr());
David Sehr9323e6e2016-09-13 08:58:35 -0700696 }
697
698 return true;
699}
700
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000701mirror::Object* CreateAnnotationMember(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700702 Handle<mirror::Class> annotation_class,
703 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000704 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700705 Thread* self = Thread::Current();
706 ScopedObjectAccessUnchecked soa(self);
707 StackHandleScope<5> hs(self);
708 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800709 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700710 Handle<mirror::String> string_name(
711 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
712
713 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
714 ArtMethod* annotation_method =
715 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
716 if (annotation_method == nullptr) {
717 return nullptr;
718 }
Vladimir Markob45528c2017-07-27 14:14:28 +0100719 Handle<mirror::Class> method_return(hs.NewHandle(annotation_method->ResolveReturnType()));
David Sehr9323e6e2016-09-13 08:58:35 -0700720
721 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800722 if (!ProcessAnnotationValue<false>(klass,
723 annotation,
724 &annotation_value,
725 method_return,
726 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700727 return nullptr;
728 }
729 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
730
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700731 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700732 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
733 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
734 mirror::Method* method_obj_ptr;
735 DCHECK(!Runtime::Current()->IsActiveTransaction());
736 if (pointer_size == PointerSize::k64) {
737 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
738 self, annotation_method);
739 } else {
740 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
741 self, annotation_method);
742 }
743 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
744
Andreas Gampefa4333d2017-02-14 11:10:34 -0800745 if (new_member == nullptr || string_name == nullptr ||
746 method_object == nullptr || method_return == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700747 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
748 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
749 return nullptr;
750 }
751
752 JValue result;
753 ArtMethod* annotation_member_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800754 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
David Sehr9323e6e2016-09-13 08:58:35 -0700755 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
756 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
757 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
758 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
759 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
760 };
761 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
762 if (self->IsExceptionPending()) {
763 LOG(INFO) << "Exception in AnnotationMember.<init>";
764 return nullptr;
765 }
766
767 return new_member.Get();
768}
769
770const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000771 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700772 const DexFile::AnnotationSetItem* annotation_set,
773 uint32_t visibility,
Vladimir Marko0db16e02017-11-08 14:32:33 +0000774 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700775 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000776 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700777 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
778 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
779 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
780 continue;
781 }
782 const uint8_t* annotation = annotation_item->annotation_;
783 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100784 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
785 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000786 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000787 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000788 klass.GetDexFile(),
789 dex::TypeIndex(type_index),
790 hs.NewHandle(klass.GetDexCache()),
791 hs.NewHandle(klass.GetClassLoader()));
792 if (resolved_class == nullptr) {
793 std::string temp;
794 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
795 klass.GetRealClass()->GetDescriptor(&temp), type_index);
796 CHECK(self->IsExceptionPending());
797 self->ClearException();
798 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700799 }
800 if (resolved_class == annotation_class.Get()) {
801 return annotation_item;
802 }
803 }
804
805 return nullptr;
806}
807
808mirror::Object* GetAnnotationObjectFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000809 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700810 const DexFile::AnnotationSetItem* annotation_set,
811 uint32_t visibility,
812 Handle<mirror::Class> annotation_class)
813 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000814 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
815 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700816 if (annotation_item == nullptr) {
817 return nullptr;
818 }
819 const uint8_t* annotation = annotation_item->annotation_;
820 return ProcessEncodedAnnotation(klass, &annotation);
821}
822
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000823mirror::Object* GetAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700824 const DexFile::AnnotationItem* annotation_item,
825 const char* annotation_name,
826 Handle<mirror::Class> array_class,
827 uint32_t expected_type)
828 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000829 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700830 const uint8_t* annotation =
831 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
832 if (annotation == nullptr) {
833 return nullptr;
834 }
835 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800836 bool result = Runtime::Current()->IsActiveTransaction()
837 ? ProcessAnnotationValue<true>(klass,
838 &annotation,
839 &annotation_value,
840 array_class,
841 DexFile::kAllObjects)
842 : ProcessAnnotationValue<false>(klass,
843 &annotation,
844 &annotation_value,
845 array_class,
846 DexFile::kAllObjects);
847 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700848 return nullptr;
849 }
850 if (annotation_value.type_ != expected_type) {
851 return nullptr;
852 }
853 return annotation_value.value_.GetL();
854}
855
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000856mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700857 const DexFile::AnnotationSetItem* annotation_set)
858 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000859 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700860 StackHandleScope<1> hs(Thread::Current());
861 const DexFile::AnnotationItem* annotation_item =
862 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
863 DexFile::kDexVisibilitySystem);
864 if (annotation_item == nullptr) {
865 return nullptr;
866 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700867 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
David Sehr9323e6e2016-09-13 08:58:35 -0700868 Handle<mirror::Class> string_array_class(hs.NewHandle(
869 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800870 if (string_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700871 return nullptr;
872 }
873 mirror::Object* obj =
874 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
875 DexFile::kDexAnnotationArray);
876 if (obj == nullptr) {
877 return nullptr;
878 }
879 return obj->AsObjectArray<mirror::String>();
880}
881
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000882mirror::ObjectArray<mirror::Class>* GetThrowsValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700883 const DexFile::AnnotationSetItem* annotation_set)
884 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000885 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700886 StackHandleScope<1> hs(Thread::Current());
887 const DexFile::AnnotationItem* annotation_item =
888 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
889 DexFile::kDexVisibilitySystem);
890 if (annotation_item == nullptr) {
891 return nullptr;
892 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700893 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -0700894 Handle<mirror::Class> class_array_class(hs.NewHandle(
895 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800896 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700897 return nullptr;
898 }
899 mirror::Object* obj =
900 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
901 DexFile::kDexAnnotationArray);
902 if (obj == nullptr) {
903 return nullptr;
904 }
905 return obj->AsObjectArray<mirror::Class>();
906}
907
908mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000909 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700910 const DexFile::AnnotationSetItem* annotation_set,
911 uint32_t visibility)
912 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000913 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700914 Thread* self = Thread::Current();
915 ScopedObjectAccessUnchecked soa(self);
916 StackHandleScope<2> hs(self);
917 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700918 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700919 if (annotation_set == nullptr) {
920 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
921 }
922
923 uint32_t size = annotation_set->size_;
924 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
925 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800926 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700927 return nullptr;
928 }
929
930 uint32_t dest_index = 0;
931 for (uint32_t i = 0; i < size; ++i) {
932 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
933 // Note that we do not use IsVisibilityCompatible here because older code
934 // was correct for this case.
935 if (annotation_item->visibility_ != visibility) {
936 continue;
937 }
938 const uint8_t* annotation = annotation_item->annotation_;
939 mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
940 if (annotation_obj != nullptr) {
941 result->SetWithoutChecks<false>(dest_index, annotation_obj);
942 ++dest_index;
943 } else if (self->IsExceptionPending()) {
944 return nullptr;
945 }
946 }
947
948 if (dest_index == size) {
949 return result.Get();
950 }
951
952 mirror::ObjectArray<mirror::Object>* trimmed_result =
953 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
954 if (trimmed_result == nullptr) {
955 return nullptr;
956 }
957
958 for (uint32_t i = 0; i < dest_index; ++i) {
959 mirror::Object* obj = result->GetWithoutChecks(i);
960 trimmed_result->SetWithoutChecks<false>(i, obj);
961 }
962
963 return trimmed_result;
964}
965
966mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000967 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700968 const DexFile::AnnotationSetRefList* set_ref_list,
969 uint32_t size)
970 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000971 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700972 Thread* self = Thread::Current();
973 ScopedObjectAccessUnchecked soa(self);
974 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700975 ObjPtr<mirror::Class> annotation_array_class =
976 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
David Sehr9323e6e2016-09-13 08:58:35 -0700977 mirror::Class* annotation_array_array_class =
978 Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
979 if (annotation_array_array_class == nullptr) {
980 return nullptr;
981 }
982 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
983 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800984 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700985 LOG(ERROR) << "Annotation set ref array allocation failed";
986 return nullptr;
987 }
988 for (uint32_t index = 0; index < size; ++index) {
989 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
990 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
991 mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item,
992 DexFile::kDexVisibilityRuntime);
993 if (annotation_set == nullptr) {
994 return nullptr;
995 }
996 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
997 }
998 return annotation_array_array.Get();
999}
1000} // namespace
1001
1002namespace annotations {
1003
1004mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) {
1005 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1006 if (annotation_set == nullptr) {
1007 return nullptr;
1008 }
1009 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001010 const ClassData field_class(hs, field);
1011 return GetAnnotationObjectFromAnnotationSet(field_class,
1012 annotation_set,
1013 DexFile::kDexVisibilityRuntime,
1014 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001015}
1016
1017mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) {
1018 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1019 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001020 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001021 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1022}
1023
1024mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
1025 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1026 if (annotation_set == nullptr) {
1027 return nullptr;
1028 }
1029 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001030 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001031 return GetSignatureValue(field_class, annotation_set);
1032}
1033
1034bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1035 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1036 if (annotation_set == nullptr) {
1037 return false;
1038 }
1039 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001040 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001041 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1042 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1043 return annotation_item != nullptr;
1044}
1045
1046mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001047 const ClassData klass(method);
1048 const DexFile* dex_file = &klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -07001049 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001050 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001051 if (annotations_dir == nullptr) {
1052 return nullptr;
1053 }
1054 const DexFile::AnnotationSetItem* annotation_set =
1055 dex_file->GetClassAnnotationSet(annotations_dir);
1056 if (annotation_set == nullptr) {
1057 return nullptr;
1058 }
1059 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1060 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1061 if (annotation_item == nullptr) {
1062 return nullptr;
1063 }
1064 const uint8_t* annotation =
1065 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1066 if (annotation == nullptr) {
1067 return nullptr;
1068 }
1069 uint8_t header_byte = *(annotation++);
1070 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1071 return nullptr;
1072 }
1073 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1074 if (annotation == nullptr) {
1075 return nullptr;
1076 }
1077 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001078 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001079 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001080 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001081 &annotation,
1082 &annotation_value,
1083 return_type,
1084 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001085 return nullptr;
1086 }
1087 return annotation_value.value_.GetL();
1088}
1089
1090mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
1091 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1092 if (annotation_set == nullptr) {
1093 return nullptr;
1094 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001095 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001096 DexFile::kDexVisibilityRuntime, annotation_class);
1097}
1098
1099mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
1100 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001101 return ProcessAnnotationSet(ClassData(method),
1102 annotation_set,
1103 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001104}
1105
1106mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
1107 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1108 if (annotation_set == nullptr) {
1109 return nullptr;
1110 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001111 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001112}
1113
1114mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
1115 const DexFile* dex_file = method->GetDexFile();
1116 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1117 FindAnnotationsItemForMethod(method);
1118 if (parameter_annotations == nullptr) {
1119 return nullptr;
1120 }
1121 const DexFile::AnnotationSetRefList* set_ref_list =
1122 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1123 if (set_ref_list == nullptr) {
1124 return nullptr;
1125 }
1126 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001127 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001128}
1129
1130mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1131 uint32_t parameter_idx,
1132 Handle<mirror::Class> annotation_class) {
1133 const DexFile* dex_file = method->GetDexFile();
1134 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1135 FindAnnotationsItemForMethod(method);
1136 if (parameter_annotations == nullptr) {
1137 return nullptr;
1138 }
1139 const DexFile::AnnotationSetRefList* set_ref_list =
1140 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1141 if (set_ref_list == nullptr) {
1142 return nullptr;
1143 }
1144 if (parameter_idx >= set_ref_list->size_) {
1145 return nullptr;
1146 }
1147 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1148 const DexFile::AnnotationSetItem* annotation_set =
1149 dex_file->GetSetRefItemItem(annotation_set_ref);
1150
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001151 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001152 annotation_set,
1153 DexFile::kDexVisibilityRuntime,
1154 annotation_class);
1155}
1156
Neil Fuller79a21e72016-09-09 14:24:51 +01001157bool GetParametersMetadataForMethod(ArtMethod* method,
1158 MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1159 MutableHandle<mirror::IntArray>* access_flags) {
Yi Kong88307ed2017-04-25 22:33:06 -07001160 const DexFile::AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001161 FindAnnotationSetForMethod(method);
1162 if (annotation_set == nullptr) {
1163 return false;
1164 }
1165
1166 const DexFile* dex_file = method->GetDexFile();
1167 const DexFile::AnnotationItem* annotation_item =
1168 SearchAnnotationSet(*dex_file,
1169 annotation_set,
1170 "Ldalvik/annotation/MethodParameters;",
1171 DexFile::kDexVisibilitySystem);
1172 if (annotation_item == nullptr) {
1173 return false;
1174 }
1175
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001176 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001177
1178 // Extract the parameters' names String[].
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001179 ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
Neil Fuller79a21e72016-09-09 14:24:51 +01001180 Handle<mirror::Class> string_array_class(hs.NewHandle(
1181 Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001182 if (UNLIKELY(string_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001183 return false;
1184 }
1185
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001186 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001187 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001188 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001189 annotation_item,
1190 "names",
1191 string_array_class,
1192 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001193 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001194 return false;
1195 }
1196
1197 // Extract the parameters' access flags int[].
1198 Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001199 if (UNLIKELY(int_array_class == nullptr)) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001200 return false;
1201 }
1202 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001203 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001204 annotation_item,
1205 "accessFlags",
1206 int_array_class,
1207 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001208 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001209 return false;
1210 }
1211
1212 names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1213 access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1214 return true;
1215}
1216
David Sehr9323e6e2016-09-13 08:58:35 -07001217mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1218 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1219 if (annotation_set == nullptr) {
1220 return nullptr;
1221 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001222 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001223}
1224
Roland Levillain35e42f02017-06-26 18:14:39 +01001225bool IsMethodAnnotationPresent(ArtMethod* method,
1226 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001227 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
David Sehr9323e6e2016-09-13 08:58:35 -07001228 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1229 if (annotation_set == nullptr) {
1230 return false;
1231 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001232 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1233 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001234 return annotation_item != nullptr;
1235}
1236
Vladimir Marko0db16e02017-11-08 14:32:33 +00001237static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1238 if (kIsDebugBuild) {
1239 ScopedObjectAccess soa(Thread::Current());
1240 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1241 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001242 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1243 if (klass != nullptr) {
1244 // Lookup using the boot class path loader should yield the annotation class.
1245 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader */ nullptr));
1246 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001247 }
1248}
1249
1250// Check whether a method from the `dex_file` with the given `annotation_set`
1251// is annotated with `annotation_descriptor` with build visibility.
1252static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
1253 const DexFile::AnnotationSetItem& annotation_set,
1254 const char* annotation_descriptor,
1255 jclass annotation_class) {
1256 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
1257 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
1258 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1259 continue;
1260 }
1261 const uint8_t* annotation = annotation_item->annotation_;
1262 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1263 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1264 if (strcmp(descriptor, annotation_descriptor) == 0) {
1265 DCheckNativeAnnotation(descriptor, annotation_class);
1266 return true;
1267 }
1268 }
1269 return false;
1270}
1271
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001272uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
1273 const DexFile::ClassDef& class_def,
1274 uint32_t method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +00001275 const DexFile::AnnotationSetItem* annotation_set =
1276 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001277 if (annotation_set == nullptr) {
1278 return 0u;
1279 }
1280 uint32_t access_flags = 0u;
1281 if (IsMethodBuildAnnotationPresent(
1282 dex_file,
1283 *annotation_set,
1284 "Ldalvik/annotation/optimization/FastNative;",
1285 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1286 access_flags |= kAccFastNative;
1287 }
1288 if (IsMethodBuildAnnotationPresent(
1289 dex_file,
1290 *annotation_set,
1291 "Ldalvik/annotation/optimization/CriticalNative;",
1292 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1293 access_flags |= kAccCriticalNative;
1294 }
1295 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1296 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001297}
1298
David Sehr9323e6e2016-09-13 08:58:35 -07001299mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1300 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001301 ClassData data(klass);
1302 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001303 if (annotation_set == nullptr) {
1304 return nullptr;
1305 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001306 return GetAnnotationObjectFromAnnotationSet(data,
1307 annotation_set,
1308 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001309 annotation_class);
1310}
1311
1312mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001313 ClassData data(klass);
1314 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1315 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001316}
1317
1318mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001319 ClassData data(klass);
1320 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001321 if (annotation_set == nullptr) {
1322 return nullptr;
1323 }
1324 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001325 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001326 DexFile::kDexVisibilitySystem);
1327 if (annotation_item == nullptr) {
1328 return nullptr;
1329 }
1330 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001331 ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
David Sehr9323e6e2016-09-13 08:58:35 -07001332 Handle<mirror::Class> class_array_class(hs.NewHandle(
1333 Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001334 if (class_array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -07001335 return nullptr;
1336 }
1337 mirror::Object* obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001338 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001339 DexFile::kDexAnnotationArray);
1340 if (obj == nullptr) {
1341 return nullptr;
1342 }
1343 return obj->AsObjectArray<mirror::Class>();
1344}
1345
1346mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001347 ClassData data(klass);
1348 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001349 if (annotation_set == nullptr) {
1350 return nullptr;
1351 }
1352 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001353 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001354 DexFile::kDexVisibilitySystem);
1355 if (annotation_item == nullptr) {
1356 return nullptr;
1357 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001358 mirror::Object* obj = GetAnnotationValue(data, annotation_item, "value",
David Sehr9323e6e2016-09-13 08:58:35 -07001359 ScopedNullHandle<mirror::Class>(),
1360 DexFile::kDexAnnotationType);
1361 if (obj == nullptr) {
1362 return nullptr;
1363 }
1364 return obj->AsClass();
1365}
1366
1367mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
David Sehr9323e6e2016-09-13 08:58:35 -07001368 mirror::Class* declaring_class = GetDeclaringClass(klass);
1369 if (declaring_class != nullptr) {
1370 return declaring_class;
1371 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001372 ClassData data(klass);
1373 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001374 if (annotation_set == nullptr) {
1375 return nullptr;
1376 }
1377 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001378 SearchAnnotationSet(data.GetDexFile(),
1379 annotation_set,
1380 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001381 DexFile::kDexVisibilitySystem);
1382 if (annotation_item == nullptr) {
1383 return nullptr;
1384 }
1385 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001386 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001387 if (annotation == nullptr) {
1388 return nullptr;
1389 }
1390 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001391 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001392 &annotation,
1393 &annotation_value,
1394 ScopedNullHandle<mirror::Class>(),
1395 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001396 return nullptr;
1397 }
1398 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1399 return nullptr;
1400 }
1401 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001402 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001403 data.GetDexFile(),
1404 annotation_value.value_.GetI(),
1405 hs.NewHandle(data.GetDexCache()),
1406 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001407 if (method == nullptr) {
1408 return nullptr;
1409 }
1410 return method->GetDeclaringClass();
1411}
1412
1413mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001414 ClassData data(klass);
1415 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001416 if (annotation_set == nullptr) {
1417 return nullptr;
1418 }
1419 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001420 SearchAnnotationSet(data.GetDexFile(),
1421 annotation_set,
1422 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001423 DexFile::kDexVisibilitySystem);
1424 if (annotation_item == nullptr) {
1425 return nullptr;
1426 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001427 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001428 DexFile::kDexAnnotationMethod);
1429}
1430
1431bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001432 ClassData data(klass);
1433 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001434 if (annotation_set == nullptr) {
1435 return false;
1436 }
1437 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001438 data.GetDexFile(),
1439 annotation_set,
1440 "Ldalvik/annotation/InnerClass;",
1441 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001442 if (annotation_item == nullptr) {
1443 return false;
1444 }
1445 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001446 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001447 if (annotation == nullptr) {
1448 return false;
1449 }
1450 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001451 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001452 &annotation,
1453 &annotation_value,
1454 ScopedNullHandle<mirror::Class>(),
1455 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001456 return false;
1457 }
1458 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1459 annotation_value.type_ != DexFile::kDexAnnotationString) {
1460 return false;
1461 }
1462 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1463 return true;
1464}
1465
1466bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001467 ClassData data(klass);
1468 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001469 if (annotation_set == nullptr) {
1470 return false;
1471 }
1472 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001473 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001474 DexFile::kDexVisibilitySystem);
1475 if (annotation_item == nullptr) {
1476 return false;
1477 }
1478 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001479 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001480 if (annotation == nullptr) {
1481 return false;
1482 }
1483 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001484 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001485 &annotation,
1486 &annotation_value,
1487 ScopedNullHandle<mirror::Class>(),
1488 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001489 return false;
1490 }
1491 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1492 return false;
1493 }
1494 *flags = annotation_value.value_.GetI();
1495 return true;
1496}
1497
1498mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001499 ClassData data(klass);
1500 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001501 if (annotation_set == nullptr) {
1502 return nullptr;
1503 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001504 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001505}
1506
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001507const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001508 // Before instantiating ClassData, check that klass has a DexCache
1509 // assigned. The ClassData constructor indirectly dereferences it
1510 // when calling klass->GetDexFile().
1511 if (klass->GetDexCache() == nullptr) {
1512 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1513 return nullptr;
1514 }
1515
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001516 ClassData data(klass);
1517 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1518 if (annotation_set == nullptr) {
1519 return nullptr;
1520 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001521
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001522 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1523 data.GetDexFile(),
1524 annotation_set,
1525 "Ldalvik/annotation/SourceDebugExtension;",
1526 DexFile::kDexVisibilitySystem);
1527 if (annotation_item == nullptr) {
1528 return nullptr;
1529 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001530
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001531 const uint8_t* annotation =
1532 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1533 if (annotation == nullptr) {
1534 return nullptr;
1535 }
1536 DexFile::AnnotationValue annotation_value;
1537 if (!ProcessAnnotationValue<false>(data,
1538 &annotation,
1539 &annotation_value,
1540 ScopedNullHandle<mirror::Class>(),
1541 DexFile::kAllRaw)) {
1542 return nullptr;
1543 }
1544 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1545 return nullptr;
1546 }
1547 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1548 return data.GetDexFile().StringDataByIdx(index);
1549}
1550
David Sehr9323e6e2016-09-13 08:58:35 -07001551bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001552 ClassData data(klass);
1553 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001554 if (annotation_set == nullptr) {
1555 return false;
1556 }
1557 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001558 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001559 return annotation_item != nullptr;
1560}
1561
1562int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1563 // For native method, lineno should be -2 to indicate it is native. Note that
1564 // "line number == -2" is how libcore tells from StackTraceElement.
1565 if (method->GetCodeItemOffset() == 0) {
1566 return -2;
1567 }
1568
1569 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
David Sehr709b0702016-10-13 09:12:37 -07001570 DCHECK(code_item != nullptr) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001571
1572 // A method with no line number info should return -1
1573 DexFile::LineNumFromPcContext context(rel_pc, -1);
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +00001574 uint32_t debug_info_offset = OatFile::GetDebugInfoOffset(*dex_file, code_item);
1575 dex_file->DecodeDebugPositionInfo(
1576 code_item, debug_info_offset, DexFile::LineNumForPcCb, &context);
David Sehr9323e6e2016-09-13 08:58:35 -07001577 return context.line_num_;
1578}
1579
1580template<bool kTransactionActive>
1581void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1582 DCHECK(dex_cache_ != nullptr);
1583 DCHECK(class_loader_ != nullptr);
1584 switch (type_) {
1585 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1586 break;
1587 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1588 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1589 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1590 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1591 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1592 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1593 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1594 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1595 case kString: {
Vladimir Marko28e012a2017-12-07 11:22:59 +00001596 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex_file_,
1597 dex::StringIndex(jval_.i),
1598 *dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001599 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1600 break;
1601 }
1602 case kType: {
Vladimir Marko28e012a2017-12-07 11:22:59 +00001603 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex_file_,
1604 dex::TypeIndex(jval_.i),
1605 *dex_cache_,
1606 *class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001607 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1608 break;
1609 }
1610 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1611 }
1612}
1613template
1614void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1615template
1616void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1617
1618} // namespace annotations
1619
1620} // namespace art