blob: 64348282984c59ee6f01a23e708d72a056d6b910 [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"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000025#include "base/sdk_version.h"
David Sehr9323e6e2016-09-13 08:58:35 -070026#include "class_linker-inl.h"
Vladimir Marko0278be72018-05-24 13:30:24 +010027#include "class_root.h"
David Sehr334b9d72018-02-12 18:27:56 -080028#include "dex/dex_file-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010029#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070030#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070031#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070032#include "mirror/class-alloc-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070033#include "mirror/field.h"
34#include "mirror/method.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070035#include "mirror/object_array-alloc-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070036#include "mirror/object_array-inl.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000037#include "oat_file.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010038#include "obj_ptr-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070039#include "reflection.h"
40#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070041#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070042
43namespace art {
44
Andreas Gampe46ee31b2016-12-14 10:11:49 -080045using android::base::StringPrintf;
46
David Sehr9323e6e2016-09-13 08:58:35 -070047struct DexFile::AnnotationValue {
48 JValue value_;
49 uint8_t type_;
50};
51
52namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000053
54// A helper class that contains all the data needed to do annotation lookup.
55class ClassData {
56 public:
57 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
58 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
59 method,
60 *method->GetDexFile(),
61 &method->GetClassDef()) {}
62
63 // Requires Scope to be able to create at least 1 handles.
64 template <typename Scope>
65 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
66 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
67
68 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
69 : ClassData(klass, // klass
70 nullptr, // method
71 klass->GetDexFile(),
72 klass->GetClassDef()) {}
73
74 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
75 return dex_file_;
76 }
77
78 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
79 return class_def_;
80 }
81
82 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
83 if (method_ != nullptr) {
84 return method_->GetDexCache();
85 } else {
86 return real_klass_->GetDexCache();
87 }
88 }
89
90 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
91 if (method_ != nullptr) {
92 return method_->GetDeclaringClass()->GetClassLoader();
93 } else {
94 return real_klass_->GetClassLoader();
95 }
96 }
97
98 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
99 if (method_ != nullptr) {
100 return method_->GetDeclaringClass();
101 } else {
102 return real_klass_.Get();
103 }
104 }
105
106 private:
107 ClassData(Handle<mirror::Class> klass,
108 ArtMethod* method,
109 const DexFile& dex_file,
110 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
111 : real_klass_(klass),
112 method_(method),
113 dex_file_(dex_file),
114 class_def_(class_def) {
115 DCHECK((method_ == nullptr) || real_klass_.IsNull());
116 }
117
118 Handle<mirror::Class> real_klass_;
119 ArtMethod* method_;
120 const DexFile& dex_file_;
121 const DexFile::ClassDef* class_def_;
122
123 DISALLOW_COPY_AND_ASSIGN(ClassData);
124};
125
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100126ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
127 Handle<mirror::Class> annotation_class,
128 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700129 REQUIRES_SHARED(Locks::mutator_lock_);
130
131bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
132 if (expected == DexFile::kDexVisibilityRuntime) {
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000133 if (IsSdkVersionSetAndAtMost(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kM)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700134 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
135 }
136 }
137 return actual == expected;
138}
139
140const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
141 REQUIRES_SHARED(Locks::mutator_lock_) {
142 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700143 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Alex Light89f33b82017-10-23 16:37:03 -0700144 const DexFile::ClassDef* class_def = klass->GetClassDef();
145 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700146 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700147 return nullptr;
148 }
David Sehr9323e6e2016-09-13 08:58:35 -0700149 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700150 dex_file->GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700151 if (annotations_dir == nullptr) {
152 return nullptr;
153 }
154 const DexFile::FieldAnnotationsItem* field_annotations =
155 dex_file->GetFieldAnnotations(annotations_dir);
156 if (field_annotations == nullptr) {
157 return nullptr;
158 }
159 uint32_t field_index = field->GetDexFieldIndex();
160 uint32_t field_count = annotations_dir->fields_size_;
161 for (uint32_t i = 0; i < field_count; ++i) {
162 if (field_annotations[i].field_idx_ == field_index) {
163 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
164 }
165 }
166 return nullptr;
167}
168
169const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
170 const DexFile::AnnotationSetItem* annotation_set,
171 const char* descriptor,
172 uint32_t visibility)
173 REQUIRES_SHARED(Locks::mutator_lock_) {
174 const DexFile::AnnotationItem* result = nullptr;
175 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
176 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
177 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
178 continue;
179 }
180 const uint8_t* annotation = annotation_item->annotation_;
181 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
182
Andreas Gampea5b09a62016-11-17 15:21:22 -0800183 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700184 result = annotation_item;
185 break;
186 }
187 }
188 return result;
189}
190
191bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
192 REQUIRES_SHARED(Locks::mutator_lock_) {
193 const uint8_t* annotation = *annotation_ptr;
194 uint8_t header_byte = *(annotation++);
195 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
196 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
197 int32_t width = value_arg + 1;
198
199 switch (value_type) {
200 case DexFile::kDexAnnotationByte:
201 case DexFile::kDexAnnotationShort:
202 case DexFile::kDexAnnotationChar:
203 case DexFile::kDexAnnotationInt:
204 case DexFile::kDexAnnotationLong:
205 case DexFile::kDexAnnotationFloat:
206 case DexFile::kDexAnnotationDouble:
207 case DexFile::kDexAnnotationString:
208 case DexFile::kDexAnnotationType:
209 case DexFile::kDexAnnotationMethod:
210 case DexFile::kDexAnnotationField:
211 case DexFile::kDexAnnotationEnum:
212 break;
213 case DexFile::kDexAnnotationArray:
214 {
215 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700216 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700217 if (!SkipAnnotationValue(dex_file, &annotation)) {
218 return false;
219 }
220 }
221 width = 0;
222 break;
223 }
224 case DexFile::kDexAnnotationAnnotation:
225 {
226 DecodeUnsignedLeb128(&annotation); // unused type_index
227 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700228 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700229 DecodeUnsignedLeb128(&annotation); // unused element_name_index
230 if (!SkipAnnotationValue(dex_file, &annotation)) {
231 return false;
232 }
233 }
234 width = 0;
235 break;
236 }
237 case DexFile::kDexAnnotationBoolean:
238 case DexFile::kDexAnnotationNull:
239 width = 0;
240 break;
241 default:
242 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
243 return false;
244 }
245
246 annotation += width;
247 *annotation_ptr = annotation;
248 return true;
249}
250
251const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
252 const uint8_t* annotation,
253 const char* name)
254 REQUIRES_SHARED(Locks::mutator_lock_) {
255 DecodeUnsignedLeb128(&annotation); // unused type_index
256 uint32_t size = DecodeUnsignedLeb128(&annotation);
257
258 while (size != 0) {
259 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800260 const char* element_name =
261 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700262 if (strcmp(name, element_name) == 0) {
263 return annotation;
264 }
265 SkipAnnotationValue(dex_file, &annotation);
266 size--;
267 }
268 return nullptr;
269}
270
Vladimir Marko0db16e02017-11-08 14:32:33 +0000271const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
272 const DexFile::ClassDef& class_def,
273 uint32_t method_index) {
David Sehr9323e6e2016-09-13 08:58:35 -0700274 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000275 dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700276 if (annotations_dir == nullptr) {
277 return nullptr;
278 }
279 const DexFile::MethodAnnotationsItem* method_annotations =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000280 dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700281 if (method_annotations == nullptr) {
282 return nullptr;
283 }
David Sehr9323e6e2016-09-13 08:58:35 -0700284 uint32_t method_count = annotations_dir->methods_size_;
285 for (uint32_t i = 0; i < method_count; ++i) {
286 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000287 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700288 }
289 }
290 return nullptr;
291}
292
Vladimir Marko0db16e02017-11-08 14:32:33 +0000293inline const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
294 REQUIRES_SHARED(Locks::mutator_lock_) {
295 if (method->IsProxyMethod()) {
296 return nullptr;
297 }
298 return FindAnnotationSetForMethod(*method->GetDexFile(),
299 method->GetClassDef(),
300 method->GetDexMethodIndex());
301}
302
David Sehr9323e6e2016-09-13 08:58:35 -0700303const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
304 REQUIRES_SHARED(Locks::mutator_lock_) {
305 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700306 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000307 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700308 if (annotations_dir == nullptr) {
309 return nullptr;
310 }
311 const DexFile::ParameterAnnotationsItem* parameter_annotations =
312 dex_file->GetParameterAnnotations(annotations_dir);
313 if (parameter_annotations == nullptr) {
314 return nullptr;
315 }
316 uint32_t method_index = method->GetDexMethodIndex();
317 uint32_t parameter_count = annotations_dir->parameters_size_;
318 for (uint32_t i = 0; i < parameter_count; ++i) {
319 if (parameter_annotations[i].method_idx_ == method_index) {
320 return &parameter_annotations[i];
321 }
322 }
323 return nullptr;
324}
325
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000326const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700327 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000328 const DexFile& dex_file = klass.GetDexFile();
Alex Light89f33b82017-10-23 16:37:03 -0700329 const DexFile::ClassDef* class_def = klass.GetClassDef();
330 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700331 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700332 return nullptr;
333 }
David Sehr9323e6e2016-09-13 08:58:35 -0700334 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700335 dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700336 if (annotations_dir == nullptr) {
337 return nullptr;
338 }
339 return dex_file.GetClassAnnotationSet(annotations_dir);
340}
341
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100342ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700343 REQUIRES_SHARED(Locks::mutator_lock_) {
344 uint32_t type_index = DecodeUnsignedLeb128(annotation);
345 uint32_t size = DecodeUnsignedLeb128(annotation);
346
347 Thread* self = Thread::Current();
348 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000349 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700350 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
351 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000352 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000353 hs.NewHandle(klass.GetDexCache()),
354 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800355 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000356 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
357 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700358 DCHECK(Thread::Current()->IsExceptionPending());
359 Thread::Current()->ClearException();
360 return nullptr;
361 }
362
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700363 ObjPtr<mirror::Class> annotation_member_class =
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100364 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember);
365 ObjPtr<mirror::Class> annotation_member_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100366 class_linker->FindArrayClass(self, annotation_member_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700367 if (annotation_member_array_class == nullptr) {
368 return nullptr;
369 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100370 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700371 if (size > 0) {
372 element_array =
373 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
374 if (element_array == nullptr) {
375 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
376 return nullptr;
377 }
378 }
379
380 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
381 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100382 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700383 if (new_member == nullptr) {
384 return nullptr;
385 }
386 h_element_array->SetWithoutChecks<false>(i, new_member);
387 }
388
389 JValue result;
390 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800391 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700392 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
393 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
394 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
395 if (self->IsExceptionPending()) {
396 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
397 return nullptr;
398 }
399
400 return result.GetL();
401}
402
Andreas Gampe9486a162017-02-16 15:17:47 -0800403template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000404bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700405 const uint8_t** annotation_ptr,
406 DexFile::AnnotationValue* annotation_value,
407 Handle<mirror::Class> array_class,
408 DexFile::AnnotationResultStyle result_style)
409 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000410 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700411 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700412 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700413 bool set_object = false;
414 Primitive::Type primitive_type = Primitive::kPrimVoid;
415 const uint8_t* annotation = *annotation_ptr;
416 uint8_t header_byte = *(annotation++);
417 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
418 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
419 int32_t width = value_arg + 1;
420 annotation_value->type_ = value_type;
421
422 switch (value_type) {
423 case DexFile::kDexAnnotationByte:
424 annotation_value->value_.SetB(
425 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
426 primitive_type = Primitive::kPrimByte;
427 break;
428 case DexFile::kDexAnnotationShort:
429 annotation_value->value_.SetS(
430 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
431 primitive_type = Primitive::kPrimShort;
432 break;
433 case DexFile::kDexAnnotationChar:
434 annotation_value->value_.SetC(
435 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
436 primitive_type = Primitive::kPrimChar;
437 break;
438 case DexFile::kDexAnnotationInt:
439 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
440 primitive_type = Primitive::kPrimInt;
441 break;
442 case DexFile::kDexAnnotationLong:
443 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
444 primitive_type = Primitive::kPrimLong;
445 break;
446 case DexFile::kDexAnnotationFloat:
447 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
448 primitive_type = Primitive::kPrimFloat;
449 break;
450 case DexFile::kDexAnnotationDouble:
451 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
452 primitive_type = Primitive::kPrimDouble;
453 break;
454 case DexFile::kDexAnnotationBoolean:
455 annotation_value->value_.SetZ(value_arg != 0);
456 primitive_type = Primitive::kPrimBoolean;
457 width = 0;
458 break;
459 case DexFile::kDexAnnotationString: {
460 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
461 if (result_style == DexFile::kAllRaw) {
462 annotation_value->value_.SetI(index);
463 } else {
464 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700465 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000466 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700467 set_object = true;
468 if (element_object == nullptr) {
469 return false;
470 }
471 }
472 break;
473 }
474 case DexFile::kDexAnnotationType: {
475 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
476 if (result_style == DexFile::kAllRaw) {
477 annotation_value->value_.SetI(index);
478 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800479 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000480 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700481 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000482 type_index,
483 hs.NewHandle(klass.GetDexCache()),
484 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700485 set_object = true;
486 if (element_object == nullptr) {
487 CHECK(self->IsExceptionPending());
488 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800489 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700490 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
491 element_object = self->GetException();
492 self->ClearException();
493 } else {
494 return false;
495 }
496 }
497 }
498 break;
499 }
500 case DexFile::kDexAnnotationMethod: {
501 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
502 if (result_style == DexFile::kAllRaw) {
503 annotation_value->value_.SetI(index);
504 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000505 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000506 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700507 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000508 index,
509 hs.NewHandle(klass.GetDexCache()),
510 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700511 if (method == nullptr) {
512 return false;
513 }
514 PointerSize pointer_size = class_linker->GetImagePointerSize();
515 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700516 if (method->IsConstructor()) {
517 if (pointer_size == PointerSize::k64) {
518 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800519 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700520 } else {
521 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800522 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700523 }
524 } else {
525 if (pointer_size == PointerSize::k64) {
526 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800527 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700528 } else {
529 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800530 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700531 }
532 }
533 if (element_object == nullptr) {
534 return false;
535 }
536 }
537 break;
538 }
539 case DexFile::kDexAnnotationField: {
540 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
541 if (result_style == DexFile::kAllRaw) {
542 annotation_value->value_.SetI(index);
543 } else {
544 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700545 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000546 index,
547 hs.NewHandle(klass.GetDexCache()),
548 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700549 if (field == nullptr) {
550 return false;
551 }
552 set_object = true;
553 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
554 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800555 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
556 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700557 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800558 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
559 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700560 }
561 if (element_object == nullptr) {
562 return false;
563 }
564 }
565 break;
566 }
567 case DexFile::kDexAnnotationEnum: {
568 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
569 if (result_style == DexFile::kAllRaw) {
570 annotation_value->value_.SetI(index);
571 } else {
572 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700573 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000574 index,
575 hs.NewHandle(klass.GetDexCache()),
576 hs.NewHandle(klass.GetClassLoader()),
577 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700578 if (enum_field == nullptr) {
579 return false;
580 } else {
581 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
582 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
583 element_object = enum_field->GetObject(field_class.Get());
584 set_object = true;
585 }
586 }
587 break;
588 }
589 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800590 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700591 return false;
592 } else {
593 ScopedObjectAccessUnchecked soa(self);
594 StackHandleScope<2> hs(self);
595 uint32_t size = DecodeUnsignedLeb128(&annotation);
596 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
597 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
598 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
599 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800600 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700601 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
602 return false;
603 }
604 DexFile::AnnotationValue new_annotation_value;
605 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800606 if (!ProcessAnnotationValue<kTransactionActive>(klass,
607 &annotation,
608 &new_annotation_value,
609 component_type,
610 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700611 return false;
612 }
613 if (!component_type->IsPrimitive()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100614 ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800615 new_array->AsObjectArray<mirror::Object>()->
616 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700617 } else {
618 switch (new_annotation_value.type_) {
619 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800620 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700621 i, new_annotation_value.value_.GetB());
622 break;
623 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800624 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700625 i, new_annotation_value.value_.GetS());
626 break;
627 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800628 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700629 i, new_annotation_value.value_.GetC());
630 break;
631 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800632 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700633 i, new_annotation_value.value_.GetI());
634 break;
635 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800636 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700637 i, new_annotation_value.value_.GetJ());
638 break;
639 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800640 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700641 i, new_annotation_value.value_.GetF());
642 break;
643 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800644 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700645 i, new_annotation_value.value_.GetD());
646 break;
647 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800648 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700649 i, new_annotation_value.value_.GetZ());
650 break;
651 default:
652 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
653 return false;
654 }
655 }
656 }
657 element_object = new_array.Get();
658 set_object = true;
659 width = 0;
660 }
661 break;
662 case DexFile::kDexAnnotationAnnotation:
663 if (result_style == DexFile::kAllRaw) {
664 return false;
665 }
666 element_object = ProcessEncodedAnnotation(klass, &annotation);
667 if (element_object == nullptr) {
668 return false;
669 }
670 set_object = true;
671 width = 0;
672 break;
673 case DexFile::kDexAnnotationNull:
674 if (result_style == DexFile::kAllRaw) {
675 annotation_value->value_.SetI(0);
676 } else {
677 CHECK(element_object == nullptr);
678 set_object = true;
679 }
680 width = 0;
681 break;
682 default:
683 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
684 return false;
685 }
686
687 annotation += width;
688 *annotation_ptr = annotation;
689
690 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100691 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700692 set_object = true;
693 }
694
695 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100696 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700697 }
698
699 return true;
700}
701
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100702ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
703 Handle<mirror::Class> annotation_class,
704 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000705 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700706 Thread* self = Thread::Current();
707 ScopedObjectAccessUnchecked soa(self);
708 StackHandleScope<5> hs(self);
709 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800710 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700711 Handle<mirror::String> string_name(
712 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
713
714 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
715 ArtMethod* annotation_method =
716 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
717 if (annotation_method == nullptr) {
718 return nullptr;
719 }
Vladimir Markob45528c2017-07-27 14:14:28 +0100720 Handle<mirror::Class> method_return(hs.NewHandle(annotation_method->ResolveReturnType()));
David Sehr9323e6e2016-09-13 08:58:35 -0700721
722 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800723 if (!ProcessAnnotationValue<false>(klass,
724 annotation,
725 &annotation_value,
726 method_return,
727 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700728 return nullptr;
729 }
730 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
731
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700732 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700733 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
734 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
Vladimir Markod93e3742018-07-18 10:58:13 +0100735 ObjPtr<mirror::Method> method_obj_ptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700736 DCHECK(!Runtime::Current()->IsActiveTransaction());
737 if (pointer_size == PointerSize::k64) {
738 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
739 self, annotation_method);
740 } else {
741 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
742 self, annotation_method);
743 }
744 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
745
Andreas Gampefa4333d2017-02-14 11:10:34 -0800746 if (new_member == nullptr || string_name == nullptr ||
747 method_object == nullptr || method_return == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700748 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
749 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
750 return nullptr;
751 }
752
753 JValue result;
754 ArtMethod* annotation_member_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800755 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
David Sehr9323e6e2016-09-13 08:58:35 -0700756 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
757 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
758 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
759 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
760 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
761 };
762 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
763 if (self->IsExceptionPending()) {
764 LOG(INFO) << "Exception in AnnotationMember.<init>";
765 return nullptr;
766 }
767
768 return new_member.Get();
769}
770
771const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000772 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700773 const DexFile::AnnotationSetItem* annotation_set,
774 uint32_t visibility,
Vladimir Marko0db16e02017-11-08 14:32:33 +0000775 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700776 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000777 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700778 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
779 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
780 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
781 continue;
782 }
783 const uint8_t* annotation = annotation_item->annotation_;
784 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100785 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
786 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000787 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000788 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000789 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
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100808ObjPtr<mirror::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
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100823ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
824 const DexFile::AnnotationItem* annotation_item,
825 const char* annotation_name,
826 Handle<mirror::Class> array_class,
827 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700828 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
Vladimir Markoacb906d2018-05-30 10:23:49 +0100856static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue(
857 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700858 const DexFile::AnnotationSetItem* annotation_set)
859 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000860 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700861 StackHandleScope<1> hs(Thread::Current());
862 const DexFile::AnnotationItem* annotation_item =
863 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
864 DexFile::kDexVisibilitySystem);
865 if (annotation_item == nullptr) {
866 return nullptr;
867 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100868 Handle<mirror::Class> string_array_class =
869 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>());
870 DCHECK(string_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100871 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700872 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
873 DexFile::kDexAnnotationArray);
874 if (obj == nullptr) {
875 return nullptr;
876 }
877 return obj->AsObjectArray<mirror::String>();
878}
879
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100880ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(
881 const ClassData& klass,
882 const DexFile::AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700883 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000884 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700885 const DexFile::AnnotationItem* annotation_item =
886 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
887 DexFile::kDexVisibilitySystem);
888 if (annotation_item == nullptr) {
889 return nullptr;
890 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100891 StackHandleScope<1> hs(Thread::Current());
892 Handle<mirror::Class> class_array_class =
893 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
894 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100895 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700896 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
897 DexFile::kDexAnnotationArray);
898 if (obj == nullptr) {
899 return nullptr;
900 }
901 return obj->AsObjectArray<mirror::Class>();
902}
903
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100904ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000905 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700906 const DexFile::AnnotationSetItem* annotation_set,
907 uint32_t visibility)
908 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000909 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700910 Thread* self = Thread::Current();
911 ScopedObjectAccessUnchecked soa(self);
912 StackHandleScope<2> hs(self);
913 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700914 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700915 if (annotation_set == nullptr) {
916 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
917 }
918
919 uint32_t size = annotation_set->size_;
920 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
921 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800922 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700923 return nullptr;
924 }
925
926 uint32_t dest_index = 0;
927 for (uint32_t i = 0; i < size; ++i) {
928 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
929 // Note that we do not use IsVisibilityCompatible here because older code
930 // was correct for this case.
931 if (annotation_item->visibility_ != visibility) {
932 continue;
933 }
934 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100935 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700936 if (annotation_obj != nullptr) {
937 result->SetWithoutChecks<false>(dest_index, annotation_obj);
938 ++dest_index;
939 } else if (self->IsExceptionPending()) {
940 return nullptr;
941 }
942 }
943
944 if (dest_index == size) {
945 return result.Get();
946 }
947
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100948 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700949 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
950 if (trimmed_result == nullptr) {
951 return nullptr;
952 }
953
954 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100955 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700956 trimmed_result->SetWithoutChecks<false>(i, obj);
957 }
958
959 return trimmed_result;
960}
961
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100962ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000963 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700964 const DexFile::AnnotationSetRefList* set_ref_list,
965 uint32_t size)
966 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000967 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700968 Thread* self = Thread::Current();
969 ScopedObjectAccessUnchecked soa(self);
970 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700971 ObjPtr<mirror::Class> annotation_array_class =
972 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100973 ObjPtr<mirror::Class> annotation_array_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100974 Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700975 if (annotation_array_array_class == nullptr) {
976 return nullptr;
977 }
978 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
979 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800980 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700981 LOG(ERROR) << "Annotation set ref array allocation failed";
982 return nullptr;
983 }
984 for (uint32_t index = 0; index < size; ++index) {
985 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
986 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100987 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
988 set_item,
989 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -0700990 if (annotation_set == nullptr) {
991 return nullptr;
992 }
993 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
994 }
995 return annotation_array_array.Get();
996}
997} // namespace
998
999namespace annotations {
1000
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001001ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1002 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001003 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);
1009 return GetAnnotationObjectFromAnnotationSet(field_class,
1010 annotation_set,
1011 DexFile::kDexVisibilityRuntime,
1012 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001013}
1014
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001015ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
David Sehr9323e6e2016-09-13 08:58:35 -07001016 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
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 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1020}
1021
Vladimir Markoacb906d2018-05-30 10:23:49 +01001022ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) {
David Sehr9323e6e2016-09-13 08:58:35 -07001023 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1024 if (annotation_set == nullptr) {
1025 return nullptr;
1026 }
1027 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001028 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001029 return GetSignatureValue(field_class, annotation_set);
1030}
1031
1032bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1033 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1034 if (annotation_set == nullptr) {
1035 return false;
1036 }
1037 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001038 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001039 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1040 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1041 return annotation_item != nullptr;
1042}
1043
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001044ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001045 const ClassData klass(method);
1046 const DexFile* dex_file = &klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -07001047 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001048 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001049 if (annotations_dir == nullptr) {
1050 return nullptr;
1051 }
1052 const DexFile::AnnotationSetItem* annotation_set =
1053 dex_file->GetClassAnnotationSet(annotations_dir);
1054 if (annotation_set == nullptr) {
1055 return nullptr;
1056 }
1057 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1058 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1059 if (annotation_item == nullptr) {
1060 return nullptr;
1061 }
1062 const uint8_t* annotation =
1063 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1064 if (annotation == nullptr) {
1065 return nullptr;
1066 }
1067 uint8_t header_byte = *(annotation++);
1068 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1069 return nullptr;
1070 }
1071 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1072 if (annotation == nullptr) {
1073 return nullptr;
1074 }
1075 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001076 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001077 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001078 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001079 &annotation,
1080 &annotation_value,
1081 return_type,
1082 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001083 return nullptr;
1084 }
1085 return annotation_value.value_.GetL();
1086}
1087
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001088ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1089 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001090 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1091 if (annotation_set == nullptr) {
1092 return nullptr;
1093 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001094 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001095 DexFile::kDexVisibilityRuntime, annotation_class);
1096}
1097
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001098ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001099 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001100 return ProcessAnnotationSet(ClassData(method),
1101 annotation_set,
1102 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001103}
1104
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001105ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001106 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1107 if (annotation_set == nullptr) {
1108 return nullptr;
1109 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001110 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001111}
1112
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001113ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001114 const DexFile* dex_file = method->GetDexFile();
1115 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1116 FindAnnotationsItemForMethod(method);
1117 if (parameter_annotations == nullptr) {
1118 return nullptr;
1119 }
1120 const DexFile::AnnotationSetRefList* set_ref_list =
1121 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1122 if (set_ref_list == nullptr) {
1123 return nullptr;
1124 }
1125 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001126 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001127}
1128
Orion Hodson58143d22018-02-20 08:44:20 +00001129uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1130 const DexFile* dex_file = method->GetDexFile();
1131 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1132 FindAnnotationsItemForMethod(method);
1133 if (parameter_annotations == nullptr) {
1134 return 0u;
1135 }
1136 const DexFile::AnnotationSetRefList* set_ref_list =
1137 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1138 if (set_ref_list == nullptr) {
1139 return 0u;
1140 }
1141 return set_ref_list->size_;
1142}
1143
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001144ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1145 uint32_t parameter_idx,
1146 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001147 const DexFile* dex_file = method->GetDexFile();
1148 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1149 FindAnnotationsItemForMethod(method);
1150 if (parameter_annotations == nullptr) {
1151 return nullptr;
1152 }
1153 const DexFile::AnnotationSetRefList* set_ref_list =
1154 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1155 if (set_ref_list == nullptr) {
1156 return nullptr;
1157 }
1158 if (parameter_idx >= set_ref_list->size_) {
1159 return nullptr;
1160 }
1161 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1162 const DexFile::AnnotationSetItem* annotation_set =
1163 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001164 if (annotation_set == nullptr) {
1165 return nullptr;
1166 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001167 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001168 annotation_set,
1169 DexFile::kDexVisibilityRuntime,
1170 annotation_class);
1171}
1172
Vladimir Markoacb906d2018-05-30 10:23:49 +01001173bool GetParametersMetadataForMethod(
1174 ArtMethod* method,
1175 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1176 /*out*/ MutableHandle<mirror::IntArray>* access_flags) {
Yi Kong88307ed2017-04-25 22:33:06 -07001177 const DexFile::AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001178 FindAnnotationSetForMethod(method);
1179 if (annotation_set == nullptr) {
1180 return false;
1181 }
1182
1183 const DexFile* dex_file = method->GetDexFile();
1184 const DexFile::AnnotationItem* annotation_item =
1185 SearchAnnotationSet(*dex_file,
1186 annotation_set,
1187 "Ldalvik/annotation/MethodParameters;",
1188 DexFile::kDexVisibilitySystem);
1189 if (annotation_item == nullptr) {
1190 return false;
1191 }
1192
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001193 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001194
1195 // Extract the parameters' names String[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001196 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1197 Handle<mirror::Class> string_array_class =
1198 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker));
1199 DCHECK(string_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001200
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001201 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001202 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001203 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001204 annotation_item,
1205 "names",
1206 string_array_class,
1207 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001208 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001209 return false;
1210 }
1211
1212 // Extract the parameters' access flags int[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001213 Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker)));
1214 DCHECK(int_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001215 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001216 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001217 annotation_item,
1218 "accessFlags",
1219 int_array_class,
1220 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001221 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001222 return false;
1223 }
1224
Vladimir Markoacb906d2018-05-30 10:23:49 +01001225 names->Assign(names_obj->AsObjectArray<mirror::String>());
1226 access_flags->Assign(access_flags_obj->AsIntArray());
Neil Fuller79a21e72016-09-09 14:24:51 +01001227 return true;
1228}
1229
Vladimir Markoacb906d2018-05-30 10:23:49 +01001230ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001231 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1232 if (annotation_set == nullptr) {
1233 return nullptr;
1234 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001235 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001236}
1237
Roland Levillain35e42f02017-06-26 18:14:39 +01001238bool IsMethodAnnotationPresent(ArtMethod* method,
1239 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001240 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
David Sehr9323e6e2016-09-13 08:58:35 -07001241 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1242 if (annotation_set == nullptr) {
1243 return false;
1244 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001245 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1246 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001247 return annotation_item != nullptr;
1248}
1249
Vladimir Marko0db16e02017-11-08 14:32:33 +00001250static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1251 if (kIsDebugBuild) {
1252 ScopedObjectAccess soa(Thread::Current());
1253 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1254 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001255 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1256 if (klass != nullptr) {
1257 // Lookup using the boot class path loader should yield the annotation class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001258 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr));
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001259 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001260 }
1261}
1262
1263// Check whether a method from the `dex_file` with the given `annotation_set`
1264// is annotated with `annotation_descriptor` with build visibility.
1265static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
1266 const DexFile::AnnotationSetItem& annotation_set,
1267 const char* annotation_descriptor,
1268 jclass annotation_class) {
1269 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
1270 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
1271 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1272 continue;
1273 }
1274 const uint8_t* annotation = annotation_item->annotation_;
1275 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1276 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1277 if (strcmp(descriptor, annotation_descriptor) == 0) {
1278 DCheckNativeAnnotation(descriptor, annotation_class);
1279 return true;
1280 }
1281 }
1282 return false;
1283}
1284
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001285uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
1286 const DexFile::ClassDef& class_def,
1287 uint32_t method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +00001288 const DexFile::AnnotationSetItem* annotation_set =
1289 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001290 if (annotation_set == nullptr) {
1291 return 0u;
1292 }
1293 uint32_t access_flags = 0u;
1294 if (IsMethodBuildAnnotationPresent(
1295 dex_file,
1296 *annotation_set,
1297 "Ldalvik/annotation/optimization/FastNative;",
1298 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1299 access_flags |= kAccFastNative;
1300 }
1301 if (IsMethodBuildAnnotationPresent(
1302 dex_file,
1303 *annotation_set,
1304 "Ldalvik/annotation/optimization/CriticalNative;",
1305 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1306 access_flags |= kAccCriticalNative;
1307 }
1308 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1309 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001310}
1311
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001312ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1313 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001314 ClassData data(klass);
1315 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001316 if (annotation_set == nullptr) {
1317 return nullptr;
1318 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001319 return GetAnnotationObjectFromAnnotationSet(data,
1320 annotation_set,
1321 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001322 annotation_class);
1323}
1324
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001325ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001326 ClassData data(klass);
1327 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1328 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001329}
1330
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001331ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001332 ClassData data(klass);
1333 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001334 if (annotation_set == nullptr) {
1335 return nullptr;
1336 }
1337 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001338 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001339 DexFile::kDexVisibilitySystem);
1340 if (annotation_item == nullptr) {
1341 return nullptr;
1342 }
1343 StackHandleScope<1> hs(Thread::Current());
Vladimir Markoacb906d2018-05-30 10:23:49 +01001344 Handle<mirror::Class> class_array_class =
1345 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
1346 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001347 ObjPtr<mirror::Object> obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001348 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001349 DexFile::kDexAnnotationArray);
1350 if (obj == nullptr) {
1351 return nullptr;
1352 }
1353 return obj->AsObjectArray<mirror::Class>();
1354}
1355
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001356ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001357 ClassData data(klass);
1358 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001359 if (annotation_set == nullptr) {
1360 return nullptr;
1361 }
1362 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001363 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001364 DexFile::kDexVisibilitySystem);
1365 if (annotation_item == nullptr) {
1366 return nullptr;
1367 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001368 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1369 annotation_item,
1370 "value",
1371 ScopedNullHandle<mirror::Class>(),
1372 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001373 if (obj == nullptr) {
1374 return nullptr;
1375 }
1376 return obj->AsClass();
1377}
1378
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001379ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1380 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
David Sehr9323e6e2016-09-13 08:58:35 -07001381 if (declaring_class != nullptr) {
1382 return declaring_class;
1383 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001384 ClassData data(klass);
1385 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001386 if (annotation_set == nullptr) {
1387 return nullptr;
1388 }
1389 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001390 SearchAnnotationSet(data.GetDexFile(),
1391 annotation_set,
1392 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001393 DexFile::kDexVisibilitySystem);
1394 if (annotation_item == nullptr) {
1395 return nullptr;
1396 }
1397 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001398 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001399 if (annotation == nullptr) {
1400 return nullptr;
1401 }
1402 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001403 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001404 &annotation,
1405 &annotation_value,
1406 ScopedNullHandle<mirror::Class>(),
1407 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001408 return nullptr;
1409 }
1410 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1411 return nullptr;
1412 }
1413 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001414 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001415 annotation_value.value_.GetI(),
1416 hs.NewHandle(data.GetDexCache()),
1417 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001418 if (method == nullptr) {
1419 return nullptr;
1420 }
1421 return method->GetDeclaringClass();
1422}
1423
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001424ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001425 ClassData data(klass);
1426 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001427 if (annotation_set == nullptr) {
1428 return nullptr;
1429 }
1430 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001431 SearchAnnotationSet(data.GetDexFile(),
1432 annotation_set,
1433 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001434 DexFile::kDexVisibilitySystem);
1435 if (annotation_item == nullptr) {
1436 return nullptr;
1437 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001438 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001439 DexFile::kDexAnnotationMethod);
1440}
1441
Vladimir Markoacb906d2018-05-30 10:23:49 +01001442bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001443 ClassData data(klass);
1444 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001445 if (annotation_set == nullptr) {
1446 return false;
1447 }
1448 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001449 data.GetDexFile(),
1450 annotation_set,
1451 "Ldalvik/annotation/InnerClass;",
1452 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001453 if (annotation_item == nullptr) {
1454 return false;
1455 }
1456 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001457 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001458 if (annotation == nullptr) {
1459 return false;
1460 }
1461 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001462 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001463 &annotation,
1464 &annotation_value,
1465 ScopedNullHandle<mirror::Class>(),
1466 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001467 return false;
1468 }
1469 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1470 annotation_value.type_ != DexFile::kDexAnnotationString) {
1471 return false;
1472 }
1473 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1474 return true;
1475}
1476
1477bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001478 ClassData data(klass);
1479 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001480 if (annotation_set == nullptr) {
1481 return false;
1482 }
1483 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001484 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001485 DexFile::kDexVisibilitySystem);
1486 if (annotation_item == nullptr) {
1487 return false;
1488 }
1489 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001490 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001491 if (annotation == nullptr) {
1492 return false;
1493 }
1494 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001495 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001496 &annotation,
1497 &annotation_value,
1498 ScopedNullHandle<mirror::Class>(),
1499 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001500 return false;
1501 }
1502 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1503 return false;
1504 }
1505 *flags = annotation_value.value_.GetI();
1506 return true;
1507}
1508
Vladimir Markoacb906d2018-05-30 10:23:49 +01001509ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
1510 Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001511 ClassData data(klass);
1512 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001513 if (annotation_set == nullptr) {
1514 return nullptr;
1515 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001516 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001517}
1518
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001519const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001520 // Before instantiating ClassData, check that klass has a DexCache
1521 // assigned. The ClassData constructor indirectly dereferences it
1522 // when calling klass->GetDexFile().
1523 if (klass->GetDexCache() == nullptr) {
1524 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1525 return nullptr;
1526 }
1527
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001528 ClassData data(klass);
1529 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1530 if (annotation_set == nullptr) {
1531 return nullptr;
1532 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001533
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001534 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1535 data.GetDexFile(),
1536 annotation_set,
1537 "Ldalvik/annotation/SourceDebugExtension;",
1538 DexFile::kDexVisibilitySystem);
1539 if (annotation_item == nullptr) {
1540 return nullptr;
1541 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001542
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001543 const uint8_t* annotation =
1544 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1545 if (annotation == nullptr) {
1546 return nullptr;
1547 }
1548 DexFile::AnnotationValue annotation_value;
1549 if (!ProcessAnnotationValue<false>(data,
1550 &annotation,
1551 &annotation_value,
1552 ScopedNullHandle<mirror::Class>(),
1553 DexFile::kAllRaw)) {
1554 return nullptr;
1555 }
1556 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1557 return nullptr;
1558 }
1559 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1560 return data.GetDexFile().StringDataByIdx(index);
1561}
1562
David Sehr9323e6e2016-09-13 08:58:35 -07001563bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001564 ClassData data(klass);
1565 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001566 if (annotation_set == nullptr) {
1567 return false;
1568 }
1569 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001570 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001571 return annotation_item != nullptr;
1572}
1573
1574int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1575 // For native method, lineno should be -2 to indicate it is native. Note that
1576 // "line number == -2" is how libcore tells from StackTraceElement.
1577 if (method->GetCodeItemOffset() == 0) {
1578 return -2;
1579 }
1580
David Sehr0225f8e2018-01-31 08:52:24 +00001581 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001582 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001583
1584 // A method with no line number info should return -1
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001585 uint32_t line_num = -1;
1586 accessor.GetLineNumForPc(rel_pc, &line_num);
1587 return line_num;
David Sehr9323e6e2016-09-13 08:58:35 -07001588}
1589
1590template<bool kTransactionActive>
1591void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1592 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001593 switch (type_) {
1594 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1595 break;
1596 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1597 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1598 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1599 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1600 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1601 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1602 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1603 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1604 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001605 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001606 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001607 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1608 break;
1609 }
1610 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001611 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001612 dex_cache_,
1613 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001614 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1615 break;
1616 }
1617 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1618 }
1619}
1620template
1621void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1622template
1623void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1624
1625} // namespace annotations
1626
1627} // namespace art