blob: c5c7daa47a26e99ed7282f265cc8f4fb17ca55a6 [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"
Vladimir Marko0278be72018-05-24 13:30:24 +010026#include "class_root.h"
David Sehr334b9d72018-02-12 18:27:56 -080027#include "dex/dex_file-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010028#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070029#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070030#include "mirror/array-alloc-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070031#include "mirror/field.h"
32#include "mirror/method.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070033#include "mirror/object_array-alloc-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070034#include "mirror/object_array-inl.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000035#include "oat_file.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010036#include "obj_ptr-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070037#include "reflection.h"
38#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070039#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070040
41namespace art {
42
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043using android::base::StringPrintf;
44
David Sehr9323e6e2016-09-13 08:58:35 -070045struct DexFile::AnnotationValue {
46 JValue value_;
47 uint8_t type_;
48};
49
50namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000051
52// A helper class that contains all the data needed to do annotation lookup.
53class ClassData {
54 public:
55 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
56 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
57 method,
58 *method->GetDexFile(),
59 &method->GetClassDef()) {}
60
61 // Requires Scope to be able to create at least 1 handles.
62 template <typename Scope>
63 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
64 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
65
66 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
67 : ClassData(klass, // klass
68 nullptr, // method
69 klass->GetDexFile(),
70 klass->GetClassDef()) {}
71
72 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
73 return dex_file_;
74 }
75
76 const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
77 return class_def_;
78 }
79
80 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
81 if (method_ != nullptr) {
82 return method_->GetDexCache();
83 } else {
84 return real_klass_->GetDexCache();
85 }
86 }
87
88 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
89 if (method_ != nullptr) {
90 return method_->GetDeclaringClass()->GetClassLoader();
91 } else {
92 return real_klass_->GetClassLoader();
93 }
94 }
95
96 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
97 if (method_ != nullptr) {
98 return method_->GetDeclaringClass();
99 } else {
100 return real_klass_.Get();
101 }
102 }
103
104 private:
105 ClassData(Handle<mirror::Class> klass,
106 ArtMethod* method,
107 const DexFile& dex_file,
108 const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
109 : real_klass_(klass),
110 method_(method),
111 dex_file_(dex_file),
112 class_def_(class_def) {
113 DCHECK((method_ == nullptr) || real_klass_.IsNull());
114 }
115
116 Handle<mirror::Class> real_klass_;
117 ArtMethod* method_;
118 const DexFile& dex_file_;
119 const DexFile::ClassDef* class_def_;
120
121 DISALLOW_COPY_AND_ASSIGN(ClassData);
122};
123
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100124ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
125 Handle<mirror::Class> annotation_class,
126 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700127 REQUIRES_SHARED(Locks::mutator_lock_);
128
129bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
130 if (expected == DexFile::kDexVisibilityRuntime) {
131 int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
132 if (sdk_version > 0 && sdk_version <= 23) {
133 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
134 }
135 }
136 return actual == expected;
137}
138
139const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
140 REQUIRES_SHARED(Locks::mutator_lock_) {
141 const DexFile* dex_file = field->GetDexFile();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700142 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Alex Light89f33b82017-10-23 16:37:03 -0700143 const DexFile::ClassDef* class_def = klass->GetClassDef();
144 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700145 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700146 return nullptr;
147 }
David Sehr9323e6e2016-09-13 08:58:35 -0700148 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700149 dex_file->GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700150 if (annotations_dir == nullptr) {
151 return nullptr;
152 }
153 const DexFile::FieldAnnotationsItem* field_annotations =
154 dex_file->GetFieldAnnotations(annotations_dir);
155 if (field_annotations == nullptr) {
156 return nullptr;
157 }
158 uint32_t field_index = field->GetDexFieldIndex();
159 uint32_t field_count = annotations_dir->fields_size_;
160 for (uint32_t i = 0; i < field_count; ++i) {
161 if (field_annotations[i].field_idx_ == field_index) {
162 return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
163 }
164 }
165 return nullptr;
166}
167
168const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
169 const DexFile::AnnotationSetItem* annotation_set,
170 const char* descriptor,
171 uint32_t visibility)
172 REQUIRES_SHARED(Locks::mutator_lock_) {
173 const DexFile::AnnotationItem* result = nullptr;
174 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
175 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
176 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
177 continue;
178 }
179 const uint8_t* annotation = annotation_item->annotation_;
180 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
181
Andreas Gampea5b09a62016-11-17 15:21:22 -0800182 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700183 result = annotation_item;
184 break;
185 }
186 }
187 return result;
188}
189
190bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
191 REQUIRES_SHARED(Locks::mutator_lock_) {
192 const uint8_t* annotation = *annotation_ptr;
193 uint8_t header_byte = *(annotation++);
194 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
195 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
196 int32_t width = value_arg + 1;
197
198 switch (value_type) {
199 case DexFile::kDexAnnotationByte:
200 case DexFile::kDexAnnotationShort:
201 case DexFile::kDexAnnotationChar:
202 case DexFile::kDexAnnotationInt:
203 case DexFile::kDexAnnotationLong:
204 case DexFile::kDexAnnotationFloat:
205 case DexFile::kDexAnnotationDouble:
206 case DexFile::kDexAnnotationString:
207 case DexFile::kDexAnnotationType:
208 case DexFile::kDexAnnotationMethod:
209 case DexFile::kDexAnnotationField:
210 case DexFile::kDexAnnotationEnum:
211 break;
212 case DexFile::kDexAnnotationArray:
213 {
214 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700215 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700216 if (!SkipAnnotationValue(dex_file, &annotation)) {
217 return false;
218 }
219 }
220 width = 0;
221 break;
222 }
223 case DexFile::kDexAnnotationAnnotation:
224 {
225 DecodeUnsignedLeb128(&annotation); // unused type_index
226 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700227 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700228 DecodeUnsignedLeb128(&annotation); // unused element_name_index
229 if (!SkipAnnotationValue(dex_file, &annotation)) {
230 return false;
231 }
232 }
233 width = 0;
234 break;
235 }
236 case DexFile::kDexAnnotationBoolean:
237 case DexFile::kDexAnnotationNull:
238 width = 0;
239 break;
240 default:
241 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
242 return false;
243 }
244
245 annotation += width;
246 *annotation_ptr = annotation;
247 return true;
248}
249
250const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
251 const uint8_t* annotation,
252 const char* name)
253 REQUIRES_SHARED(Locks::mutator_lock_) {
254 DecodeUnsignedLeb128(&annotation); // unused type_index
255 uint32_t size = DecodeUnsignedLeb128(&annotation);
256
257 while (size != 0) {
258 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800259 const char* element_name =
260 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700261 if (strcmp(name, element_name) == 0) {
262 return annotation;
263 }
264 SkipAnnotationValue(dex_file, &annotation);
265 size--;
266 }
267 return nullptr;
268}
269
Vladimir Marko0db16e02017-11-08 14:32:33 +0000270const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
271 const DexFile::ClassDef& class_def,
272 uint32_t method_index) {
David Sehr9323e6e2016-09-13 08:58:35 -0700273 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000274 dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700275 if (annotations_dir == nullptr) {
276 return nullptr;
277 }
278 const DexFile::MethodAnnotationsItem* method_annotations =
Vladimir Marko0db16e02017-11-08 14:32:33 +0000279 dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700280 if (method_annotations == nullptr) {
281 return nullptr;
282 }
David Sehr9323e6e2016-09-13 08:58:35 -0700283 uint32_t method_count = annotations_dir->methods_size_;
284 for (uint32_t i = 0; i < method_count; ++i) {
285 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000286 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700287 }
288 }
289 return nullptr;
290}
291
Vladimir Marko0db16e02017-11-08 14:32:33 +0000292inline const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
293 REQUIRES_SHARED(Locks::mutator_lock_) {
294 if (method->IsProxyMethod()) {
295 return nullptr;
296 }
297 return FindAnnotationSetForMethod(*method->GetDexFile(),
298 method->GetClassDef(),
299 method->GetDexMethodIndex());
300}
301
David Sehr9323e6e2016-09-13 08:58:35 -0700302const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
303 REQUIRES_SHARED(Locks::mutator_lock_) {
304 const DexFile* dex_file = method->GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700305 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000306 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700307 if (annotations_dir == nullptr) {
308 return nullptr;
309 }
310 const DexFile::ParameterAnnotationsItem* parameter_annotations =
311 dex_file->GetParameterAnnotations(annotations_dir);
312 if (parameter_annotations == nullptr) {
313 return nullptr;
314 }
315 uint32_t method_index = method->GetDexMethodIndex();
316 uint32_t parameter_count = annotations_dir->parameters_size_;
317 for (uint32_t i = 0; i < parameter_count; ++i) {
318 if (parameter_annotations[i].method_idx_ == method_index) {
319 return &parameter_annotations[i];
320 }
321 }
322 return nullptr;
323}
324
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000325const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700326 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000327 const DexFile& dex_file = klass.GetDexFile();
Alex Light89f33b82017-10-23 16:37:03 -0700328 const DexFile::ClassDef* class_def = klass.GetClassDef();
329 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700330 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700331 return nullptr;
332 }
David Sehr9323e6e2016-09-13 08:58:35 -0700333 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Light89f33b82017-10-23 16:37:03 -0700334 dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700335 if (annotations_dir == nullptr) {
336 return nullptr;
337 }
338 return dex_file.GetClassAnnotationSet(annotations_dir);
339}
340
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100341ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700342 REQUIRES_SHARED(Locks::mutator_lock_) {
343 uint32_t type_index = DecodeUnsignedLeb128(annotation);
344 uint32_t size = DecodeUnsignedLeb128(annotation);
345
346 Thread* self = Thread::Current();
347 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000348 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700349 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
350 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000351 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000352 hs.NewHandle(klass.GetDexCache()),
353 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800354 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000355 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
356 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700357 DCHECK(Thread::Current()->IsExceptionPending());
358 Thread::Current()->ClearException();
359 return nullptr;
360 }
361
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700362 ObjPtr<mirror::Class> annotation_member_class =
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100363 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember);
364 ObjPtr<mirror::Class> annotation_member_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100365 class_linker->FindArrayClass(self, annotation_member_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700366 if (annotation_member_array_class == nullptr) {
367 return nullptr;
368 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100369 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700370 if (size > 0) {
371 element_array =
372 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
373 if (element_array == nullptr) {
374 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
375 return nullptr;
376 }
377 }
378
379 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
380 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100381 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700382 if (new_member == nullptr) {
383 return nullptr;
384 }
385 h_element_array->SetWithoutChecks<false>(i, new_member);
386 }
387
388 JValue result;
389 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800390 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700391 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
392 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
393 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
394 if (self->IsExceptionPending()) {
395 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
396 return nullptr;
397 }
398
399 return result.GetL();
400}
401
Andreas Gampe9486a162017-02-16 15:17:47 -0800402template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000403bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700404 const uint8_t** annotation_ptr,
405 DexFile::AnnotationValue* annotation_value,
406 Handle<mirror::Class> array_class,
407 DexFile::AnnotationResultStyle result_style)
408 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000409 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700410 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700411 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700412 bool set_object = false;
413 Primitive::Type primitive_type = Primitive::kPrimVoid;
414 const uint8_t* annotation = *annotation_ptr;
415 uint8_t header_byte = *(annotation++);
416 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
417 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
418 int32_t width = value_arg + 1;
419 annotation_value->type_ = value_type;
420
421 switch (value_type) {
422 case DexFile::kDexAnnotationByte:
423 annotation_value->value_.SetB(
424 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
425 primitive_type = Primitive::kPrimByte;
426 break;
427 case DexFile::kDexAnnotationShort:
428 annotation_value->value_.SetS(
429 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
430 primitive_type = Primitive::kPrimShort;
431 break;
432 case DexFile::kDexAnnotationChar:
433 annotation_value->value_.SetC(
434 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
435 primitive_type = Primitive::kPrimChar;
436 break;
437 case DexFile::kDexAnnotationInt:
438 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
439 primitive_type = Primitive::kPrimInt;
440 break;
441 case DexFile::kDexAnnotationLong:
442 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
443 primitive_type = Primitive::kPrimLong;
444 break;
445 case DexFile::kDexAnnotationFloat:
446 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
447 primitive_type = Primitive::kPrimFloat;
448 break;
449 case DexFile::kDexAnnotationDouble:
450 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
451 primitive_type = Primitive::kPrimDouble;
452 break;
453 case DexFile::kDexAnnotationBoolean:
454 annotation_value->value_.SetZ(value_arg != 0);
455 primitive_type = Primitive::kPrimBoolean;
456 width = 0;
457 break;
458 case DexFile::kDexAnnotationString: {
459 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
460 if (result_style == DexFile::kAllRaw) {
461 annotation_value->value_.SetI(index);
462 } else {
463 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700464 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000465 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700466 set_object = true;
467 if (element_object == nullptr) {
468 return false;
469 }
470 }
471 break;
472 }
473 case DexFile::kDexAnnotationType: {
474 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
475 if (result_style == DexFile::kAllRaw) {
476 annotation_value->value_.SetI(index);
477 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800478 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000479 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700480 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000481 type_index,
482 hs.NewHandle(klass.GetDexCache()),
483 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700484 set_object = true;
485 if (element_object == nullptr) {
486 CHECK(self->IsExceptionPending());
487 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800488 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700489 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
490 element_object = self->GetException();
491 self->ClearException();
492 } else {
493 return false;
494 }
495 }
496 }
497 break;
498 }
499 case DexFile::kDexAnnotationMethod: {
500 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
501 if (result_style == DexFile::kAllRaw) {
502 annotation_value->value_.SetI(index);
503 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000504 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000505 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700506 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000507 index,
508 hs.NewHandle(klass.GetDexCache()),
509 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700510 if (method == nullptr) {
511 return false;
512 }
513 PointerSize pointer_size = class_linker->GetImagePointerSize();
514 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700515 if (method->IsConstructor()) {
516 if (pointer_size == PointerSize::k64) {
517 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800518 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700519 } else {
520 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800521 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700522 }
523 } else {
524 if (pointer_size == PointerSize::k64) {
525 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800526 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700527 } else {
528 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800529 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700530 }
531 }
532 if (element_object == nullptr) {
533 return false;
534 }
535 }
536 break;
537 }
538 case DexFile::kDexAnnotationField: {
539 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
540 if (result_style == DexFile::kAllRaw) {
541 annotation_value->value_.SetI(index);
542 } else {
543 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700544 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000545 index,
546 hs.NewHandle(klass.GetDexCache()),
547 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700548 if (field == nullptr) {
549 return false;
550 }
551 set_object = true;
552 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
553 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800554 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
555 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700556 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800557 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
558 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700559 }
560 if (element_object == nullptr) {
561 return false;
562 }
563 }
564 break;
565 }
566 case DexFile::kDexAnnotationEnum: {
567 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
568 if (result_style == DexFile::kAllRaw) {
569 annotation_value->value_.SetI(index);
570 } else {
571 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700572 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000573 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()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100613 ObjPtr<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) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100690 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700691 set_object = true;
692 }
693
694 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100695 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700696 }
697
698 return true;
699}
700
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100701ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
702 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)));
Vladimir Markod93e3742018-07-18 10:58:13 +0100734 ObjPtr<mirror::Method> method_obj_ptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700735 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 dex::TypeIndex(type_index),
789 hs.NewHandle(klass.GetDexCache()),
790 hs.NewHandle(klass.GetClassLoader()));
791 if (resolved_class == nullptr) {
792 std::string temp;
793 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
794 klass.GetRealClass()->GetDescriptor(&temp), type_index);
795 CHECK(self->IsExceptionPending());
796 self->ClearException();
797 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700798 }
799 if (resolved_class == annotation_class.Get()) {
800 return annotation_item;
801 }
802 }
803
804 return nullptr;
805}
806
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100807ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000808 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700809 const DexFile::AnnotationSetItem* annotation_set,
810 uint32_t visibility,
811 Handle<mirror::Class> annotation_class)
812 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000813 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
814 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700815 if (annotation_item == nullptr) {
816 return nullptr;
817 }
818 const uint8_t* annotation = annotation_item->annotation_;
819 return ProcessEncodedAnnotation(klass, &annotation);
820}
821
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100822ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
823 const DexFile::AnnotationItem* annotation_item,
824 const char* annotation_name,
825 Handle<mirror::Class> array_class,
826 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700827 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000828 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700829 const uint8_t* annotation =
830 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
831 if (annotation == nullptr) {
832 return nullptr;
833 }
834 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800835 bool result = Runtime::Current()->IsActiveTransaction()
836 ? ProcessAnnotationValue<true>(klass,
837 &annotation,
838 &annotation_value,
839 array_class,
840 DexFile::kAllObjects)
841 : ProcessAnnotationValue<false>(klass,
842 &annotation,
843 &annotation_value,
844 array_class,
845 DexFile::kAllObjects);
846 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700847 return nullptr;
848 }
849 if (annotation_value.type_ != expected_type) {
850 return nullptr;
851 }
852 return annotation_value.value_.GetL();
853}
854
Vladimir Markoacb906d2018-05-30 10:23:49 +0100855static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue(
856 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 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100867 Handle<mirror::Class> string_array_class =
868 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>());
869 DCHECK(string_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100870 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700871 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
872 DexFile::kDexAnnotationArray);
873 if (obj == nullptr) {
874 return nullptr;
875 }
876 return obj->AsObjectArray<mirror::String>();
877}
878
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100879ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(
880 const ClassData& klass,
881 const DexFile::AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700882 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000883 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700884 const DexFile::AnnotationItem* annotation_item =
885 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
886 DexFile::kDexVisibilitySystem);
887 if (annotation_item == nullptr) {
888 return nullptr;
889 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100890 StackHandleScope<1> hs(Thread::Current());
891 Handle<mirror::Class> class_array_class =
892 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
893 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100894 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700895 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
896 DexFile::kDexAnnotationArray);
897 if (obj == nullptr) {
898 return nullptr;
899 }
900 return obj->AsObjectArray<mirror::Class>();
901}
902
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100903ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000904 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700905 const DexFile::AnnotationSetItem* annotation_set,
906 uint32_t visibility)
907 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000908 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700909 Thread* self = Thread::Current();
910 ScopedObjectAccessUnchecked soa(self);
911 StackHandleScope<2> hs(self);
912 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700913 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700914 if (annotation_set == nullptr) {
915 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
916 }
917
918 uint32_t size = annotation_set->size_;
919 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
920 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800921 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700922 return nullptr;
923 }
924
925 uint32_t dest_index = 0;
926 for (uint32_t i = 0; i < size; ++i) {
927 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
928 // Note that we do not use IsVisibilityCompatible here because older code
929 // was correct for this case.
930 if (annotation_item->visibility_ != visibility) {
931 continue;
932 }
933 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100934 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700935 if (annotation_obj != nullptr) {
936 result->SetWithoutChecks<false>(dest_index, annotation_obj);
937 ++dest_index;
938 } else if (self->IsExceptionPending()) {
939 return nullptr;
940 }
941 }
942
943 if (dest_index == size) {
944 return result.Get();
945 }
946
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100947 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700948 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
949 if (trimmed_result == nullptr) {
950 return nullptr;
951 }
952
953 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100954 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700955 trimmed_result->SetWithoutChecks<false>(i, obj);
956 }
957
958 return trimmed_result;
959}
960
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100961ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000962 const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700963 const DexFile::AnnotationSetRefList* set_ref_list,
964 uint32_t size)
965 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000966 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700967 Thread* self = Thread::Current();
968 ScopedObjectAccessUnchecked soa(self);
969 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700970 ObjPtr<mirror::Class> annotation_array_class =
971 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100972 ObjPtr<mirror::Class> annotation_array_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100973 Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700974 if (annotation_array_array_class == nullptr) {
975 return nullptr;
976 }
977 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
978 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800979 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700980 LOG(ERROR) << "Annotation set ref array allocation failed";
981 return nullptr;
982 }
983 for (uint32_t index = 0; index < size; ++index) {
984 const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
985 const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100986 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
987 set_item,
988 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -0700989 if (annotation_set == nullptr) {
990 return nullptr;
991 }
992 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
993 }
994 return annotation_array_array.Get();
995}
996} // namespace
997
998namespace annotations {
999
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001000ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1001 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001002 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1003 if (annotation_set == nullptr) {
1004 return nullptr;
1005 }
1006 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001007 const ClassData field_class(hs, field);
1008 return GetAnnotationObjectFromAnnotationSet(field_class,
1009 annotation_set,
1010 DexFile::kDexVisibilityRuntime,
1011 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001012}
1013
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001014ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
David Sehr9323e6e2016-09-13 08:58:35 -07001015 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1016 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001017 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001018 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1019}
1020
Vladimir Markoacb906d2018-05-30 10:23:49 +01001021ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) {
David Sehr9323e6e2016-09-13 08:58:35 -07001022 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1023 if (annotation_set == nullptr) {
1024 return nullptr;
1025 }
1026 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001027 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001028 return GetSignatureValue(field_class, annotation_set);
1029}
1030
1031bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1032 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1033 if (annotation_set == nullptr) {
1034 return false;
1035 }
1036 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001037 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001038 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1039 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1040 return annotation_item != nullptr;
1041}
1042
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001043ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001044 const ClassData klass(method);
1045 const DexFile* dex_file = &klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -07001046 const DexFile::AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001047 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001048 if (annotations_dir == nullptr) {
1049 return nullptr;
1050 }
1051 const DexFile::AnnotationSetItem* annotation_set =
1052 dex_file->GetClassAnnotationSet(annotations_dir);
1053 if (annotation_set == nullptr) {
1054 return nullptr;
1055 }
1056 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1057 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1058 if (annotation_item == nullptr) {
1059 return nullptr;
1060 }
1061 const uint8_t* annotation =
1062 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1063 if (annotation == nullptr) {
1064 return nullptr;
1065 }
1066 uint8_t header_byte = *(annotation++);
1067 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1068 return nullptr;
1069 }
1070 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1071 if (annotation == nullptr) {
1072 return nullptr;
1073 }
1074 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001075 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001076 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001077 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001078 &annotation,
1079 &annotation_value,
1080 return_type,
1081 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001082 return nullptr;
1083 }
1084 return annotation_value.value_.GetL();
1085}
1086
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001087ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1088 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001089 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1090 if (annotation_set == nullptr) {
1091 return nullptr;
1092 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001093 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001094 DexFile::kDexVisibilityRuntime, annotation_class);
1095}
1096
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001097ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001098 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001099 return ProcessAnnotationSet(ClassData(method),
1100 annotation_set,
1101 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001102}
1103
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001104ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001105 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1106 if (annotation_set == nullptr) {
1107 return nullptr;
1108 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001109 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001110}
1111
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001112ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001113 const DexFile* dex_file = method->GetDexFile();
1114 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1115 FindAnnotationsItemForMethod(method);
1116 if (parameter_annotations == nullptr) {
1117 return nullptr;
1118 }
1119 const DexFile::AnnotationSetRefList* set_ref_list =
1120 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1121 if (set_ref_list == nullptr) {
1122 return nullptr;
1123 }
1124 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001125 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001126}
1127
Orion Hodson58143d22018-02-20 08:44:20 +00001128uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1129 const DexFile* dex_file = method->GetDexFile();
1130 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1131 FindAnnotationsItemForMethod(method);
1132 if (parameter_annotations == nullptr) {
1133 return 0u;
1134 }
1135 const DexFile::AnnotationSetRefList* set_ref_list =
1136 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1137 if (set_ref_list == nullptr) {
1138 return 0u;
1139 }
1140 return set_ref_list->size_;
1141}
1142
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001143ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1144 uint32_t parameter_idx,
1145 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001146 const DexFile* dex_file = method->GetDexFile();
1147 const DexFile::ParameterAnnotationsItem* parameter_annotations =
1148 FindAnnotationsItemForMethod(method);
1149 if (parameter_annotations == nullptr) {
1150 return nullptr;
1151 }
1152 const DexFile::AnnotationSetRefList* set_ref_list =
1153 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1154 if (set_ref_list == nullptr) {
1155 return nullptr;
1156 }
1157 if (parameter_idx >= set_ref_list->size_) {
1158 return nullptr;
1159 }
1160 const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1161 const DexFile::AnnotationSetItem* annotation_set =
1162 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001163 if (annotation_set == nullptr) {
1164 return nullptr;
1165 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001166 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001167 annotation_set,
1168 DexFile::kDexVisibilityRuntime,
1169 annotation_class);
1170}
1171
Vladimir Markoacb906d2018-05-30 10:23:49 +01001172bool GetParametersMetadataForMethod(
1173 ArtMethod* method,
1174 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1175 /*out*/ MutableHandle<mirror::IntArray>* access_flags) {
Yi Kong88307ed2017-04-25 22:33:06 -07001176 const DexFile::AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001177 FindAnnotationSetForMethod(method);
1178 if (annotation_set == nullptr) {
1179 return false;
1180 }
1181
1182 const DexFile* dex_file = method->GetDexFile();
1183 const DexFile::AnnotationItem* annotation_item =
1184 SearchAnnotationSet(*dex_file,
1185 annotation_set,
1186 "Ldalvik/annotation/MethodParameters;",
1187 DexFile::kDexVisibilitySystem);
1188 if (annotation_item == nullptr) {
1189 return false;
1190 }
1191
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001192 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001193
1194 // Extract the parameters' names String[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001195 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1196 Handle<mirror::Class> string_array_class =
1197 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker));
1198 DCHECK(string_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001199
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001200 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001201 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001202 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001203 annotation_item,
1204 "names",
1205 string_array_class,
1206 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001207 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001208 return false;
1209 }
1210
1211 // Extract the parameters' access flags int[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001212 Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker)));
1213 DCHECK(int_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001214 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001215 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001216 annotation_item,
1217 "accessFlags",
1218 int_array_class,
1219 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001220 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001221 return false;
1222 }
1223
Vladimir Markoacb906d2018-05-30 10:23:49 +01001224 names->Assign(names_obj->AsObjectArray<mirror::String>());
1225 access_flags->Assign(access_flags_obj->AsIntArray());
Neil Fuller79a21e72016-09-09 14:24:51 +01001226 return true;
1227}
1228
Vladimir Markoacb906d2018-05-30 10:23:49 +01001229ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001230 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1231 if (annotation_set == nullptr) {
1232 return nullptr;
1233 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001234 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001235}
1236
Roland Levillain35e42f02017-06-26 18:14:39 +01001237bool IsMethodAnnotationPresent(ArtMethod* method,
1238 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001239 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
David Sehr9323e6e2016-09-13 08:58:35 -07001240 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1241 if (annotation_set == nullptr) {
1242 return false;
1243 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001244 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1245 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001246 return annotation_item != nullptr;
1247}
1248
Vladimir Marko0db16e02017-11-08 14:32:33 +00001249static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1250 if (kIsDebugBuild) {
1251 ScopedObjectAccess soa(Thread::Current());
1252 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1253 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001254 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1255 if (klass != nullptr) {
1256 // Lookup using the boot class path loader should yield the annotation class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001257 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr));
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001258 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001259 }
1260}
1261
1262// Check whether a method from the `dex_file` with the given `annotation_set`
1263// is annotated with `annotation_descriptor` with build visibility.
1264static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
1265 const DexFile::AnnotationSetItem& annotation_set,
1266 const char* annotation_descriptor,
1267 jclass annotation_class) {
1268 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
1269 const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
1270 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1271 continue;
1272 }
1273 const uint8_t* annotation = annotation_item->annotation_;
1274 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1275 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1276 if (strcmp(descriptor, annotation_descriptor) == 0) {
1277 DCheckNativeAnnotation(descriptor, annotation_class);
1278 return true;
1279 }
1280 }
1281 return false;
1282}
1283
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001284uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
1285 const DexFile::ClassDef& class_def,
1286 uint32_t method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +00001287 const DexFile::AnnotationSetItem* annotation_set =
1288 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001289 if (annotation_set == nullptr) {
1290 return 0u;
1291 }
1292 uint32_t access_flags = 0u;
1293 if (IsMethodBuildAnnotationPresent(
1294 dex_file,
1295 *annotation_set,
1296 "Ldalvik/annotation/optimization/FastNative;",
1297 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1298 access_flags |= kAccFastNative;
1299 }
1300 if (IsMethodBuildAnnotationPresent(
1301 dex_file,
1302 *annotation_set,
1303 "Ldalvik/annotation/optimization/CriticalNative;",
1304 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1305 access_flags |= kAccCriticalNative;
1306 }
1307 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1308 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001309}
1310
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001311ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1312 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001313 ClassData data(klass);
1314 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001315 if (annotation_set == nullptr) {
1316 return nullptr;
1317 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001318 return GetAnnotationObjectFromAnnotationSet(data,
1319 annotation_set,
1320 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001321 annotation_class);
1322}
1323
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001324ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001325 ClassData data(klass);
1326 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1327 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001328}
1329
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001330ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001331 ClassData data(klass);
1332 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001333 if (annotation_set == nullptr) {
1334 return nullptr;
1335 }
1336 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001337 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001338 DexFile::kDexVisibilitySystem);
1339 if (annotation_item == nullptr) {
1340 return nullptr;
1341 }
1342 StackHandleScope<1> hs(Thread::Current());
Vladimir Markoacb906d2018-05-30 10:23:49 +01001343 Handle<mirror::Class> class_array_class =
1344 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
1345 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001346 ObjPtr<mirror::Object> obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001347 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001348 DexFile::kDexAnnotationArray);
1349 if (obj == nullptr) {
1350 return nullptr;
1351 }
1352 return obj->AsObjectArray<mirror::Class>();
1353}
1354
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001355ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001356 ClassData data(klass);
1357 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001358 if (annotation_set == nullptr) {
1359 return nullptr;
1360 }
1361 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001362 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001363 DexFile::kDexVisibilitySystem);
1364 if (annotation_item == nullptr) {
1365 return nullptr;
1366 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001367 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1368 annotation_item,
1369 "value",
1370 ScopedNullHandle<mirror::Class>(),
1371 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001372 if (obj == nullptr) {
1373 return nullptr;
1374 }
1375 return obj->AsClass();
1376}
1377
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001378ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1379 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
David Sehr9323e6e2016-09-13 08:58:35 -07001380 if (declaring_class != nullptr) {
1381 return declaring_class;
1382 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001383 ClassData data(klass);
1384 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001385 if (annotation_set == nullptr) {
1386 return nullptr;
1387 }
1388 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001389 SearchAnnotationSet(data.GetDexFile(),
1390 annotation_set,
1391 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001392 DexFile::kDexVisibilitySystem);
1393 if (annotation_item == nullptr) {
1394 return nullptr;
1395 }
1396 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001397 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001398 if (annotation == nullptr) {
1399 return nullptr;
1400 }
1401 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001402 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001403 &annotation,
1404 &annotation_value,
1405 ScopedNullHandle<mirror::Class>(),
1406 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001407 return nullptr;
1408 }
1409 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1410 return nullptr;
1411 }
1412 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001413 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001414 annotation_value.value_.GetI(),
1415 hs.NewHandle(data.GetDexCache()),
1416 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001417 if (method == nullptr) {
1418 return nullptr;
1419 }
1420 return method->GetDeclaringClass();
1421}
1422
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001423ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001424 ClassData data(klass);
1425 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001426 if (annotation_set == nullptr) {
1427 return nullptr;
1428 }
1429 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001430 SearchAnnotationSet(data.GetDexFile(),
1431 annotation_set,
1432 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001433 DexFile::kDexVisibilitySystem);
1434 if (annotation_item == nullptr) {
1435 return nullptr;
1436 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001437 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001438 DexFile::kDexAnnotationMethod);
1439}
1440
Vladimir Markoacb906d2018-05-30 10:23:49 +01001441bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001442 ClassData data(klass);
1443 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001444 if (annotation_set == nullptr) {
1445 return false;
1446 }
1447 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001448 data.GetDexFile(),
1449 annotation_set,
1450 "Ldalvik/annotation/InnerClass;",
1451 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001452 if (annotation_item == nullptr) {
1453 return false;
1454 }
1455 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001456 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001457 if (annotation == nullptr) {
1458 return false;
1459 }
1460 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001461 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001462 &annotation,
1463 &annotation_value,
1464 ScopedNullHandle<mirror::Class>(),
1465 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001466 return false;
1467 }
1468 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1469 annotation_value.type_ != DexFile::kDexAnnotationString) {
1470 return false;
1471 }
1472 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1473 return true;
1474}
1475
1476bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001477 ClassData data(klass);
1478 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001479 if (annotation_set == nullptr) {
1480 return false;
1481 }
1482 const DexFile::AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001483 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001484 DexFile::kDexVisibilitySystem);
1485 if (annotation_item == nullptr) {
1486 return false;
1487 }
1488 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001489 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001490 if (annotation == nullptr) {
1491 return false;
1492 }
1493 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001494 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001495 &annotation,
1496 &annotation_value,
1497 ScopedNullHandle<mirror::Class>(),
1498 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001499 return false;
1500 }
1501 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1502 return false;
1503 }
1504 *flags = annotation_value.value_.GetI();
1505 return true;
1506}
1507
Vladimir Markoacb906d2018-05-30 10:23:49 +01001508ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
1509 Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001510 ClassData data(klass);
1511 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001512 if (annotation_set == nullptr) {
1513 return nullptr;
1514 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001515 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001516}
1517
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001518const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001519 // Before instantiating ClassData, check that klass has a DexCache
1520 // assigned. The ClassData constructor indirectly dereferences it
1521 // when calling klass->GetDexFile().
1522 if (klass->GetDexCache() == nullptr) {
1523 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1524 return nullptr;
1525 }
1526
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001527 ClassData data(klass);
1528 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1529 if (annotation_set == nullptr) {
1530 return nullptr;
1531 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001532
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001533 const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1534 data.GetDexFile(),
1535 annotation_set,
1536 "Ldalvik/annotation/SourceDebugExtension;",
1537 DexFile::kDexVisibilitySystem);
1538 if (annotation_item == nullptr) {
1539 return nullptr;
1540 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001541
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001542 const uint8_t* annotation =
1543 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1544 if (annotation == nullptr) {
1545 return nullptr;
1546 }
1547 DexFile::AnnotationValue annotation_value;
1548 if (!ProcessAnnotationValue<false>(data,
1549 &annotation,
1550 &annotation_value,
1551 ScopedNullHandle<mirror::Class>(),
1552 DexFile::kAllRaw)) {
1553 return nullptr;
1554 }
1555 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1556 return nullptr;
1557 }
1558 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1559 return data.GetDexFile().StringDataByIdx(index);
1560}
1561
David Sehr9323e6e2016-09-13 08:58:35 -07001562bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001563 ClassData data(klass);
1564 const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001565 if (annotation_set == nullptr) {
1566 return false;
1567 }
1568 const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001569 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001570 return annotation_item != nullptr;
1571}
1572
1573int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1574 // For native method, lineno should be -2 to indicate it is native. Note that
1575 // "line number == -2" is how libcore tells from StackTraceElement.
1576 if (method->GetCodeItemOffset() == 0) {
1577 return -2;
1578 }
1579
David Sehr0225f8e2018-01-31 08:52:24 +00001580 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001581 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001582
1583 // A method with no line number info should return -1
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001584 uint32_t line_num = -1;
1585 accessor.GetLineNumForPc(rel_pc, &line_num);
1586 return line_num;
David Sehr9323e6e2016-09-13 08:58:35 -07001587}
1588
1589template<bool kTransactionActive>
1590void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1591 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001592 switch (type_) {
1593 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1594 break;
1595 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1596 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1597 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1598 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1599 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1600 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1601 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1602 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1603 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001604 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001605 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001606 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1607 break;
1608 }
1609 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001610 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001611 dex_cache_,
1612 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001613 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1614 break;
1615 }
1616 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1617 }
1618}
1619template
1620void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1621template
1622void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1623
1624} // namespace annotations
1625
1626} // namespace art