blob: 050be4ad1c6bfff1a476ffe9413adffb02c62318 [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"
Hans Boehm206348c2018-12-05 11:11:33 -080029#include "dex/dex_instruction-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010030#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070031#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070032#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070033#include "mirror/class-alloc-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070034#include "mirror/field.h"
35#include "mirror/method.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070036#include "mirror/object_array-alloc-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070037#include "mirror/object_array-inl.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000038#include "oat_file.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010039#include "obj_ptr-inl.h"
Hans Boehm206348c2018-12-05 11:11:33 -080040#include "quicken_info.h"
David Sehr9323e6e2016-09-13 08:58:35 -070041#include "reflection.h"
42#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070043#include "well_known_classes.h"
David Sehr9323e6e2016-09-13 08:58:35 -070044
45namespace art {
46
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080049using dex::AnnotationItem;
50using dex::AnnotationSetItem;
51using dex::AnnotationSetRefItem;
52using dex::AnnotationSetRefList;
53using dex::AnnotationsDirectoryItem;
54using dex::FieldAnnotationsItem;
55using dex::MethodAnnotationsItem;
56using dex::ParameterAnnotationsItem;
57
David Sehr9323e6e2016-09-13 08:58:35 -070058struct DexFile::AnnotationValue {
59 JValue value_;
60 uint8_t type_;
61};
62
63namespace {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000064
65// A helper class that contains all the data needed to do annotation lookup.
66class ClassData {
67 public:
68 explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
69 : ClassData(ScopedNullHandle<mirror::Class>(), // klass
70 method,
71 *method->GetDexFile(),
72 &method->GetClassDef()) {}
73
74 // Requires Scope to be able to create at least 1 handles.
75 template <typename Scope>
76 ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
77 : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
78
79 explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
80 : ClassData(klass, // klass
81 nullptr, // method
82 klass->GetDexFile(),
83 klass->GetClassDef()) {}
84
85 const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
86 return dex_file_;
87 }
88
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080089 const dex::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +000090 return class_def_;
91 }
92
93 ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
94 if (method_ != nullptr) {
95 return method_->GetDexCache();
96 } else {
97 return real_klass_->GetDexCache();
98 }
99 }
100
101 ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
102 if (method_ != nullptr) {
103 return method_->GetDeclaringClass()->GetClassLoader();
104 } else {
105 return real_klass_->GetClassLoader();
106 }
107 }
108
109 ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
110 if (method_ != nullptr) {
111 return method_->GetDeclaringClass();
112 } else {
113 return real_klass_.Get();
114 }
115 }
116
117 private:
118 ClassData(Handle<mirror::Class> klass,
119 ArtMethod* method,
120 const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800121 const dex::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000122 : real_klass_(klass),
123 method_(method),
124 dex_file_(dex_file),
125 class_def_(class_def) {
126 DCHECK((method_ == nullptr) || real_klass_.IsNull());
127 }
128
129 Handle<mirror::Class> real_klass_;
130 ArtMethod* method_;
131 const DexFile& dex_file_;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800132 const dex::ClassDef* class_def_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000133
134 DISALLOW_COPY_AND_ASSIGN(ClassData);
135};
136
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100137ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
138 Handle<mirror::Class> annotation_class,
139 const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700140 REQUIRES_SHARED(Locks::mutator_lock_);
141
142bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
143 if (expected == DexFile::kDexVisibilityRuntime) {
David Brazdil2bb2fbd2018-11-13 18:24:26 +0000144 if (IsSdkVersionSetAndAtMost(Runtime::Current()->GetTargetSdkVersion(), SdkVersion::kM)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700145 return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
146 }
147 }
148 return actual == expected;
149}
150
Hans Boehm206348c2018-12-05 11:11:33 -0800151static const AnnotationSetItem* FindAnnotationSetForField(const DexFile& dex_file,
152 const dex::ClassDef& class_def,
153 uint32_t field_index)
David Sehr9323e6e2016-09-13 08:58:35 -0700154 REQUIRES_SHARED(Locks::mutator_lock_) {
Hans Boehm206348c2018-12-05 11:11:33 -0800155 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
156 if (annotations_dir == nullptr) {
157 return nullptr;
158 }
159 const FieldAnnotationsItem* field_annotations = dex_file.GetFieldAnnotations(annotations_dir);
160 if (field_annotations == nullptr) {
161 return nullptr;
162 }
163 uint32_t field_count = annotations_dir->fields_size_;
164 for (uint32_t i = 0; i < field_count; ++i) {
165 if (field_annotations[i].field_idx_ == field_index) {
166 return dex_file.GetFieldAnnotationSetItem(field_annotations[i]);
167 }
168 }
169 return nullptr;
170}
171
172static const AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
173 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700174 ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800175 const dex::ClassDef* class_def = klass->GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700176 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700177 DCHECK(klass->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700178 return nullptr;
179 }
Hans Boehm206348c2018-12-05 11:11:33 -0800180 return FindAnnotationSetForField(*field->GetDexFile(), *class_def, field->GetDexFieldIndex());
David Sehr9323e6e2016-09-13 08:58:35 -0700181}
182
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800183const AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
184 const AnnotationSetItem* annotation_set,
185 const char* descriptor,
186 uint32_t visibility)
David Sehr9323e6e2016-09-13 08:58:35 -0700187 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800188 const AnnotationItem* result = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700189 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800190 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700191 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
192 continue;
193 }
194 const uint8_t* annotation = annotation_item->annotation_;
195 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
196
Andreas Gampea5b09a62016-11-17 15:21:22 -0800197 if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
David Sehr9323e6e2016-09-13 08:58:35 -0700198 result = annotation_item;
199 break;
200 }
201 }
202 return result;
203}
204
205bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
206 REQUIRES_SHARED(Locks::mutator_lock_) {
207 const uint8_t* annotation = *annotation_ptr;
208 uint8_t header_byte = *(annotation++);
209 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
210 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
211 int32_t width = value_arg + 1;
212
213 switch (value_type) {
214 case DexFile::kDexAnnotationByte:
215 case DexFile::kDexAnnotationShort:
216 case DexFile::kDexAnnotationChar:
217 case DexFile::kDexAnnotationInt:
218 case DexFile::kDexAnnotationLong:
219 case DexFile::kDexAnnotationFloat:
220 case DexFile::kDexAnnotationDouble:
221 case DexFile::kDexAnnotationString:
222 case DexFile::kDexAnnotationType:
223 case DexFile::kDexAnnotationMethod:
224 case DexFile::kDexAnnotationField:
225 case DexFile::kDexAnnotationEnum:
226 break;
227 case DexFile::kDexAnnotationArray:
228 {
229 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700230 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700231 if (!SkipAnnotationValue(dex_file, &annotation)) {
232 return false;
233 }
234 }
235 width = 0;
236 break;
237 }
238 case DexFile::kDexAnnotationAnnotation:
239 {
240 DecodeUnsignedLeb128(&annotation); // unused type_index
241 uint32_t size = DecodeUnsignedLeb128(&annotation);
Andreas Gampec74d9cb2018-09-20 13:44:44 -0700242 for (; size != 0u; --size) {
David Sehr9323e6e2016-09-13 08:58:35 -0700243 DecodeUnsignedLeb128(&annotation); // unused element_name_index
244 if (!SkipAnnotationValue(dex_file, &annotation)) {
245 return false;
246 }
247 }
248 width = 0;
249 break;
250 }
251 case DexFile::kDexAnnotationBoolean:
252 case DexFile::kDexAnnotationNull:
253 width = 0;
254 break;
255 default:
256 LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
Elliott Hughesc1896c92018-11-29 11:33:18 -0800257 UNREACHABLE();
David Sehr9323e6e2016-09-13 08:58:35 -0700258 }
259
260 annotation += width;
261 *annotation_ptr = annotation;
262 return true;
263}
264
265const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
266 const uint8_t* annotation,
267 const char* name)
268 REQUIRES_SHARED(Locks::mutator_lock_) {
269 DecodeUnsignedLeb128(&annotation); // unused type_index
270 uint32_t size = DecodeUnsignedLeb128(&annotation);
271
272 while (size != 0) {
273 uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800274 const char* element_name =
275 dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
David Sehr9323e6e2016-09-13 08:58:35 -0700276 if (strcmp(name, element_name) == 0) {
277 return annotation;
278 }
279 SkipAnnotationValue(dex_file, &annotation);
280 size--;
281 }
282 return nullptr;
283}
284
Hans Boehm206348c2018-12-05 11:11:33 -0800285static const AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
286 const dex::ClassDef& class_def,
287 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800288 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700289 if (annotations_dir == nullptr) {
290 return nullptr;
291 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800292 const MethodAnnotationsItem* method_annotations = dex_file.GetMethodAnnotations(annotations_dir);
David Sehr9323e6e2016-09-13 08:58:35 -0700293 if (method_annotations == nullptr) {
294 return nullptr;
295 }
David Sehr9323e6e2016-09-13 08:58:35 -0700296 uint32_t method_count = annotations_dir->methods_size_;
297 for (uint32_t i = 0; i < method_count; ++i) {
298 if (method_annotations[i].method_idx_ == method_index) {
Vladimir Marko0db16e02017-11-08 14:32:33 +0000299 return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
David Sehr9323e6e2016-09-13 08:58:35 -0700300 }
301 }
302 return nullptr;
303}
304
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800305inline const AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
Vladimir Marko0db16e02017-11-08 14:32:33 +0000306 REQUIRES_SHARED(Locks::mutator_lock_) {
307 if (method->IsProxyMethod()) {
308 return nullptr;
309 }
310 return FindAnnotationSetForMethod(*method->GetDexFile(),
311 method->GetClassDef(),
312 method->GetDexMethodIndex());
313}
314
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800315const ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -0700316 REQUIRES_SHARED(Locks::mutator_lock_) {
317 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800318 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000319 dex_file->GetAnnotationsDirectory(method->GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -0700320 if (annotations_dir == nullptr) {
321 return nullptr;
322 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800323 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -0700324 dex_file->GetParameterAnnotations(annotations_dir);
325 if (parameter_annotations == nullptr) {
326 return nullptr;
327 }
328 uint32_t method_index = method->GetDexMethodIndex();
329 uint32_t parameter_count = annotations_dir->parameters_size_;
330 for (uint32_t i = 0; i < parameter_count; ++i) {
331 if (parameter_annotations[i].method_idx_ == method_index) {
332 return &parameter_annotations[i];
333 }
334 }
335 return nullptr;
336}
337
Hans Boehm206348c2018-12-05 11:11:33 -0800338static const AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700339 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000340 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800341 const dex::ClassDef* class_def = klass.GetClassDef();
Alex Light89f33b82017-10-23 16:37:03 -0700342 if (class_def == nullptr) {
Alex Lighte0d8ae22017-10-24 10:23:16 -0700343 DCHECK(klass.GetRealClass()->IsProxyClass());
Alex Light89f33b82017-10-23 16:37:03 -0700344 return nullptr;
345 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800346 const AnnotationsDirectoryItem* annotations_dir = dex_file.GetAnnotationsDirectory(*class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700347 if (annotations_dir == nullptr) {
348 return nullptr;
349 }
350 return dex_file.GetClassAnnotationSet(annotations_dir);
351}
352
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100353ObjPtr<mirror::Object> ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
David Sehr9323e6e2016-09-13 08:58:35 -0700354 REQUIRES_SHARED(Locks::mutator_lock_) {
355 uint32_t type_index = DecodeUnsignedLeb128(annotation);
356 uint32_t size = DecodeUnsignedLeb128(annotation);
357
358 Thread* self = Thread::Current();
359 ScopedObjectAccessUnchecked soa(self);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000360 StackHandleScope<4> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700361 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
362 Handle<mirror::Class> annotation_class(hs.NewHandle(
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000363 class_linker->ResolveType(dex::TypeIndex(type_index),
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000364 hs.NewHandle(klass.GetDexCache()),
365 hs.NewHandle(klass.GetClassLoader()))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800366 if (annotation_class == nullptr) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000367 LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
368 << " annotation class " << type_index;
David Sehr9323e6e2016-09-13 08:58:35 -0700369 DCHECK(Thread::Current()->IsExceptionPending());
370 Thread::Current()->ClearException();
371 return nullptr;
372 }
373
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700374 ObjPtr<mirror::Class> annotation_member_class =
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100375 soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember);
376 ObjPtr<mirror::Class> annotation_member_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100377 class_linker->FindArrayClass(self, annotation_member_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700378 if (annotation_member_array_class == nullptr) {
379 return nullptr;
380 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100381 ObjPtr<mirror::ObjectArray<mirror::Object>> element_array = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700382 if (size > 0) {
383 element_array =
384 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
385 if (element_array == nullptr) {
386 LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
387 return nullptr;
388 }
389 }
390
391 Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
392 for (uint32_t i = 0; i < size; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100393 ObjPtr<mirror::Object> new_member = CreateAnnotationMember(klass, annotation_class, annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700394 if (new_member == nullptr) {
395 return nullptr;
396 }
397 h_element_array->SetWithoutChecks<false>(i, new_member);
398 }
399
400 JValue result;
401 ArtMethod* create_annotation_method =
Andreas Gampe13b27842016-11-07 16:48:23 -0800402 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700403 uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
404 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
405 create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
406 if (self->IsExceptionPending()) {
407 LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
408 return nullptr;
409 }
410
411 return result.GetL();
412}
413
Andreas Gampe9486a162017-02-16 15:17:47 -0800414template <bool kTransactionActive>
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000415bool ProcessAnnotationValue(const ClassData& klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700416 const uint8_t** annotation_ptr,
417 DexFile::AnnotationValue* annotation_value,
418 Handle<mirror::Class> array_class,
419 DexFile::AnnotationResultStyle result_style)
420 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000421 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700422 Thread* self = Thread::Current();
Mathieu Chartier3398c782016-09-30 10:27:43 -0700423 ObjPtr<mirror::Object> element_object = nullptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700424 bool set_object = false;
425 Primitive::Type primitive_type = Primitive::kPrimVoid;
426 const uint8_t* annotation = *annotation_ptr;
427 uint8_t header_byte = *(annotation++);
428 uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
429 uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
430 int32_t width = value_arg + 1;
431 annotation_value->type_ = value_type;
432
433 switch (value_type) {
434 case DexFile::kDexAnnotationByte:
435 annotation_value->value_.SetB(
436 static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
437 primitive_type = Primitive::kPrimByte;
438 break;
439 case DexFile::kDexAnnotationShort:
440 annotation_value->value_.SetS(
441 static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
442 primitive_type = Primitive::kPrimShort;
443 break;
444 case DexFile::kDexAnnotationChar:
445 annotation_value->value_.SetC(
446 static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
447 primitive_type = Primitive::kPrimChar;
448 break;
449 case DexFile::kDexAnnotationInt:
450 annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
451 primitive_type = Primitive::kPrimInt;
452 break;
453 case DexFile::kDexAnnotationLong:
454 annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
455 primitive_type = Primitive::kPrimLong;
456 break;
457 case DexFile::kDexAnnotationFloat:
458 annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
459 primitive_type = Primitive::kPrimFloat;
460 break;
461 case DexFile::kDexAnnotationDouble:
462 annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
463 primitive_type = Primitive::kPrimDouble;
464 break;
465 case DexFile::kDexAnnotationBoolean:
466 annotation_value->value_.SetZ(value_arg != 0);
467 primitive_type = Primitive::kPrimBoolean;
468 width = 0;
469 break;
470 case DexFile::kDexAnnotationString: {
471 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
472 if (result_style == DexFile::kAllRaw) {
473 annotation_value->value_.SetI(index);
474 } else {
475 StackHandleScope<1> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700476 element_object = Runtime::Current()->GetClassLinker()->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000477 dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
David Sehr9323e6e2016-09-13 08:58:35 -0700478 set_object = true;
479 if (element_object == nullptr) {
480 return false;
481 }
482 }
483 break;
484 }
485 case DexFile::kDexAnnotationType: {
486 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
487 if (result_style == DexFile::kAllRaw) {
488 annotation_value->value_.SetI(index);
489 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800490 dex::TypeIndex type_index(index);
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000491 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700492 element_object = Runtime::Current()->GetClassLinker()->ResolveType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000493 type_index,
494 hs.NewHandle(klass.GetDexCache()),
495 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700496 set_object = true;
497 if (element_object == nullptr) {
498 CHECK(self->IsExceptionPending());
499 if (result_style == DexFile::kAllObjects) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800500 const char* msg = dex_file.StringByTypeIdx(type_index);
David Sehr9323e6e2016-09-13 08:58:35 -0700501 self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
502 element_object = self->GetException();
503 self->ClearException();
504 } else {
505 return false;
506 }
507 }
508 }
509 break;
510 }
511 case DexFile::kDexAnnotationMethod: {
512 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
513 if (result_style == DexFile::kAllRaw) {
514 annotation_value->value_.SetI(index);
515 } else {
Nicolas Geoffray65e07752017-03-15 06:56:35 +0000516 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000517 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700518 ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000519 index,
520 hs.NewHandle(klass.GetDexCache()),
521 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700522 if (method == nullptr) {
523 return false;
524 }
525 PointerSize pointer_size = class_linker->GetImagePointerSize();
526 set_object = true;
David Sehr9323e6e2016-09-13 08:58:35 -0700527 if (method->IsConstructor()) {
528 if (pointer_size == PointerSize::k64) {
529 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800530 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700531 } else {
532 element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800533 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700534 }
535 } else {
536 if (pointer_size == PointerSize::k64) {
537 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
Andreas Gampe9486a162017-02-16 15:17:47 -0800538 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700539 } else {
540 element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
Andreas Gampe9486a162017-02-16 15:17:47 -0800541 kTransactionActive>(self, method);
David Sehr9323e6e2016-09-13 08:58:35 -0700542 }
543 }
544 if (element_object == nullptr) {
545 return false;
546 }
547 }
548 break;
549 }
550 case DexFile::kDexAnnotationField: {
551 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
552 if (result_style == DexFile::kAllRaw) {
553 annotation_value->value_.SetI(index);
554 } else {
555 StackHandleScope<2> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700556 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000557 index,
558 hs.NewHandle(klass.GetDexCache()),
559 hs.NewHandle(klass.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -0700560 if (field == nullptr) {
561 return false;
562 }
563 set_object = true;
564 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
565 if (pointer_size == PointerSize::k64) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800566 element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
567 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700568 } else {
Andreas Gampe9486a162017-02-16 15:17:47 -0800569 element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
570 kTransactionActive>(self, field, true);
David Sehr9323e6e2016-09-13 08:58:35 -0700571 }
572 if (element_object == nullptr) {
573 return false;
574 }
575 }
576 break;
577 }
578 case DexFile::kDexAnnotationEnum: {
579 uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
580 if (result_style == DexFile::kAllRaw) {
581 annotation_value->value_.SetI(index);
582 } else {
583 StackHandleScope<3> hs(self);
David Sehr9323e6e2016-09-13 08:58:35 -0700584 ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000585 index,
586 hs.NewHandle(klass.GetDexCache()),
587 hs.NewHandle(klass.GetClassLoader()),
588 true);
David Sehr9323e6e2016-09-13 08:58:35 -0700589 if (enum_field == nullptr) {
590 return false;
591 } else {
592 Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
593 Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
594 element_object = enum_field->GetObject(field_class.Get());
595 set_object = true;
596 }
597 }
598 break;
599 }
600 case DexFile::kDexAnnotationArray:
Andreas Gampefa4333d2017-02-14 11:10:34 -0800601 if (result_style == DexFile::kAllRaw || array_class == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700602 return false;
603 } else {
604 ScopedObjectAccessUnchecked soa(self);
605 StackHandleScope<2> hs(self);
606 uint32_t size = DecodeUnsignedLeb128(&annotation);
607 Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
608 Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
609 self, array_class.Get(), size, array_class->GetComponentSizeShift(),
610 Runtime::Current()->GetHeap()->GetCurrentAllocator())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800611 if (new_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700612 LOG(ERROR) << "Annotation element array allocation failed with size " << size;
613 return false;
614 }
615 DexFile::AnnotationValue new_annotation_value;
616 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe9486a162017-02-16 15:17:47 -0800617 if (!ProcessAnnotationValue<kTransactionActive>(klass,
618 &annotation,
619 &new_annotation_value,
620 component_type,
621 DexFile::kPrimitivesOrObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700622 return false;
623 }
624 if (!component_type->IsPrimitive()) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100625 ObjPtr<mirror::Object> obj = new_annotation_value.value_.GetL();
Andreas Gampe9486a162017-02-16 15:17:47 -0800626 new_array->AsObjectArray<mirror::Object>()->
627 SetWithoutChecks<kTransactionActive>(i, obj);
David Sehr9323e6e2016-09-13 08:58:35 -0700628 } else {
629 switch (new_annotation_value.type_) {
630 case DexFile::kDexAnnotationByte:
Andreas Gampe9486a162017-02-16 15:17:47 -0800631 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700632 i, new_annotation_value.value_.GetB());
633 break;
634 case DexFile::kDexAnnotationShort:
Andreas Gampe9486a162017-02-16 15:17:47 -0800635 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700636 i, new_annotation_value.value_.GetS());
637 break;
638 case DexFile::kDexAnnotationChar:
Andreas Gampe9486a162017-02-16 15:17:47 -0800639 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700640 i, new_annotation_value.value_.GetC());
641 break;
642 case DexFile::kDexAnnotationInt:
Andreas Gampe9486a162017-02-16 15:17:47 -0800643 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700644 i, new_annotation_value.value_.GetI());
645 break;
646 case DexFile::kDexAnnotationLong:
Andreas Gampe9486a162017-02-16 15:17:47 -0800647 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700648 i, new_annotation_value.value_.GetJ());
649 break;
650 case DexFile::kDexAnnotationFloat:
Andreas Gampe9486a162017-02-16 15:17:47 -0800651 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700652 i, new_annotation_value.value_.GetF());
653 break;
654 case DexFile::kDexAnnotationDouble:
Andreas Gampe9486a162017-02-16 15:17:47 -0800655 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700656 i, new_annotation_value.value_.GetD());
657 break;
658 case DexFile::kDexAnnotationBoolean:
Andreas Gampe9486a162017-02-16 15:17:47 -0800659 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
David Sehr9323e6e2016-09-13 08:58:35 -0700660 i, new_annotation_value.value_.GetZ());
661 break;
662 default:
663 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
664 return false;
665 }
666 }
667 }
668 element_object = new_array.Get();
669 set_object = true;
670 width = 0;
671 }
672 break;
673 case DexFile::kDexAnnotationAnnotation:
674 if (result_style == DexFile::kAllRaw) {
675 return false;
676 }
677 element_object = ProcessEncodedAnnotation(klass, &annotation);
678 if (element_object == nullptr) {
679 return false;
680 }
681 set_object = true;
682 width = 0;
683 break;
684 case DexFile::kDexAnnotationNull:
685 if (result_style == DexFile::kAllRaw) {
686 annotation_value->value_.SetI(0);
687 } else {
688 CHECK(element_object == nullptr);
689 set_object = true;
690 }
691 width = 0;
692 break;
693 default:
694 LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
695 return false;
696 }
697
698 annotation += width;
699 *annotation_ptr = annotation;
700
701 if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100702 element_object = BoxPrimitive(primitive_type, annotation_value->value_);
David Sehr9323e6e2016-09-13 08:58:35 -0700703 set_object = true;
704 }
705
706 if (set_object) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100707 annotation_value->value_.SetL(element_object);
David Sehr9323e6e2016-09-13 08:58:35 -0700708 }
709
710 return true;
711}
712
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100713ObjPtr<mirror::Object> CreateAnnotationMember(const ClassData& klass,
714 Handle<mirror::Class> annotation_class,
715 const uint8_t** annotation) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000716 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700717 Thread* self = Thread::Current();
718 ScopedObjectAccessUnchecked soa(self);
719 StackHandleScope<5> hs(self);
720 uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800721 const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
David Sehr9323e6e2016-09-13 08:58:35 -0700722 Handle<mirror::String> string_name(
723 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
724
725 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
726 ArtMethod* annotation_method =
727 annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
728 if (annotation_method == nullptr) {
729 return nullptr;
730 }
Vladimir Markob45528c2017-07-27 14:14:28 +0100731 Handle<mirror::Class> method_return(hs.NewHandle(annotation_method->ResolveReturnType()));
David Sehr9323e6e2016-09-13 08:58:35 -0700732
733 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800734 if (!ProcessAnnotationValue<false>(klass,
735 annotation,
736 &annotation_value,
737 method_return,
738 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -0700739 return nullptr;
740 }
741 Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
742
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700743 ObjPtr<mirror::Class> annotation_member_class =
David Sehr9323e6e2016-09-13 08:58:35 -0700744 WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
745 Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
Vladimir Markod93e3742018-07-18 10:58:13 +0100746 ObjPtr<mirror::Method> method_obj_ptr;
David Sehr9323e6e2016-09-13 08:58:35 -0700747 DCHECK(!Runtime::Current()->IsActiveTransaction());
748 if (pointer_size == PointerSize::k64) {
749 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
750 self, annotation_method);
751 } else {
752 method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
753 self, annotation_method);
754 }
755 Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
756
Andreas Gampefa4333d2017-02-14 11:10:34 -0800757 if (new_member == nullptr || string_name == nullptr ||
758 method_object == nullptr || method_return == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700759 LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
760 new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
761 return nullptr;
762 }
763
764 JValue result;
765 ArtMethod* annotation_member_init =
Andreas Gampe13b27842016-11-07 16:48:23 -0800766 jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
David Sehr9323e6e2016-09-13 08:58:35 -0700767 uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
768 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
769 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
770 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
771 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
772 };
773 annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
774 if (self->IsExceptionPending()) {
775 LOG(INFO) << "Exception in AnnotationMember.<init>";
776 return nullptr;
777 }
778
779 return new_member.Get();
780}
781
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800782const AnnotationItem* GetAnnotationItemFromAnnotationSet(const ClassData& klass,
783 const AnnotationSetItem* annotation_set,
784 uint32_t visibility,
785 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700786 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000787 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700788 for (uint32_t i = 0; i < annotation_set->size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800789 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700790 if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
791 continue;
792 }
793 const uint8_t* annotation = annotation_item->annotation_;
794 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
Vladimir Marko370f57e2017-07-27 16:36:59 +0100795 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
796 Thread* self = Thread::Current();
Vladimir Marko0db16e02017-11-08 14:32:33 +0000797 StackHandleScope<2> hs(self);
Vladimir Marko28e012a2017-12-07 11:22:59 +0000798 ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000799 dex::TypeIndex(type_index),
800 hs.NewHandle(klass.GetDexCache()),
801 hs.NewHandle(klass.GetClassLoader()));
802 if (resolved_class == nullptr) {
803 std::string temp;
804 LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
805 klass.GetRealClass()->GetDescriptor(&temp), type_index);
806 CHECK(self->IsExceptionPending());
807 self->ClearException();
808 continue;
David Sehr9323e6e2016-09-13 08:58:35 -0700809 }
810 if (resolved_class == annotation_class.Get()) {
811 return annotation_item;
812 }
813 }
814
815 return nullptr;
816}
817
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800818ObjPtr<mirror::Object> GetAnnotationObjectFromAnnotationSet(const ClassData& klass,
819 const AnnotationSetItem* annotation_set,
820 uint32_t visibility,
821 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -0700822 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800823 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +0000824 klass, annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700825 if (annotation_item == nullptr) {
826 return nullptr;
827 }
828 const uint8_t* annotation = annotation_item->annotation_;
829 return ProcessEncodedAnnotation(klass, &annotation);
830}
831
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100832ObjPtr<mirror::Object> GetAnnotationValue(const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800833 const AnnotationItem* annotation_item,
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100834 const char* annotation_name,
835 Handle<mirror::Class> array_class,
836 uint32_t expected_type)
David Sehr9323e6e2016-09-13 08:58:35 -0700837 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000838 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700839 const uint8_t* annotation =
840 SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
841 if (annotation == nullptr) {
842 return nullptr;
843 }
844 DexFile::AnnotationValue annotation_value;
Andreas Gampe9486a162017-02-16 15:17:47 -0800845 bool result = Runtime::Current()->IsActiveTransaction()
846 ? ProcessAnnotationValue<true>(klass,
847 &annotation,
848 &annotation_value,
849 array_class,
850 DexFile::kAllObjects)
851 : ProcessAnnotationValue<false>(klass,
852 &annotation,
853 &annotation_value,
854 array_class,
855 DexFile::kAllObjects);
856 if (!result) {
David Sehr9323e6e2016-09-13 08:58:35 -0700857 return nullptr;
858 }
859 if (annotation_value.type_ != expected_type) {
860 return nullptr;
861 }
862 return annotation_value.value_.GetL();
863}
864
Vladimir Markoacb906d2018-05-30 10:23:49 +0100865static ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureValue(
866 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800867 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700868 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000869 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700870 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800871 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700872 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
873 DexFile::kDexVisibilitySystem);
874 if (annotation_item == nullptr) {
875 return nullptr;
876 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100877 Handle<mirror::Class> string_array_class =
878 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>());
879 DCHECK(string_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100880 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700881 GetAnnotationValue(klass, annotation_item, "value", string_array_class,
882 DexFile::kDexAnnotationArray);
883 if (obj == nullptr) {
884 return nullptr;
885 }
886 return obj->AsObjectArray<mirror::String>();
887}
888
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800889ObjPtr<mirror::ObjectArray<mirror::Class>> GetThrowsValue(const ClassData& klass,
890 const AnnotationSetItem* annotation_set)
David Sehr9323e6e2016-09-13 08:58:35 -0700891 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000892 const DexFile& dex_file = klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800893 const AnnotationItem* annotation_item =
David Sehr9323e6e2016-09-13 08:58:35 -0700894 SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
895 DexFile::kDexVisibilitySystem);
896 if (annotation_item == nullptr) {
897 return nullptr;
898 }
Vladimir Markoacb906d2018-05-30 10:23:49 +0100899 StackHandleScope<1> hs(Thread::Current());
900 Handle<mirror::Class> class_array_class =
901 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
902 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100903 ObjPtr<mirror::Object> obj =
David Sehr9323e6e2016-09-13 08:58:35 -0700904 GetAnnotationValue(klass, annotation_item, "value", class_array_class,
905 DexFile::kDexAnnotationArray);
906 if (obj == nullptr) {
907 return nullptr;
908 }
909 return obj->AsObjectArray<mirror::Class>();
910}
911
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100912ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000913 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800914 const AnnotationSetItem* annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -0700915 uint32_t visibility)
916 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000917 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700918 Thread* self = Thread::Current();
919 ScopedObjectAccessUnchecked soa(self);
920 StackHandleScope<2> hs(self);
921 Handle<mirror::Class> annotation_array_class(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700922 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
David Sehr9323e6e2016-09-13 08:58:35 -0700923 if (annotation_set == nullptr) {
924 return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
925 }
926
927 uint32_t size = annotation_set->size_;
928 Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
929 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800930 if (result == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700931 return nullptr;
932 }
933
934 uint32_t dest_index = 0;
935 for (uint32_t i = 0; i < size; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800936 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
David Sehr9323e6e2016-09-13 08:58:35 -0700937 // Note that we do not use IsVisibilityCompatible here because older code
938 // was correct for this case.
939 if (annotation_item->visibility_ != visibility) {
940 continue;
941 }
942 const uint8_t* annotation = annotation_item->annotation_;
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100943 ObjPtr<mirror::Object> annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
David Sehr9323e6e2016-09-13 08:58:35 -0700944 if (annotation_obj != nullptr) {
945 result->SetWithoutChecks<false>(dest_index, annotation_obj);
946 ++dest_index;
947 } else if (self->IsExceptionPending()) {
948 return nullptr;
949 }
950 }
951
952 if (dest_index == size) {
953 return result.Get();
954 }
955
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100956 ObjPtr<mirror::ObjectArray<mirror::Object>> trimmed_result =
David Sehr9323e6e2016-09-13 08:58:35 -0700957 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
958 if (trimmed_result == nullptr) {
959 return nullptr;
960 }
961
962 for (uint32_t i = 0; i < dest_index; ++i) {
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100963 ObjPtr<mirror::Object> obj = result->GetWithoutChecks(i);
David Sehr9323e6e2016-09-13 08:58:35 -0700964 trimmed_result->SetWithoutChecks<false>(i, obj);
965 }
966
967 return trimmed_result;
968}
969
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100970ObjPtr<mirror::ObjectArray<mirror::Object>> ProcessAnnotationSetRefList(
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000971 const ClassData& klass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800972 const AnnotationSetRefList* set_ref_list,
David Sehr9323e6e2016-09-13 08:58:35 -0700973 uint32_t size)
974 REQUIRES_SHARED(Locks::mutator_lock_) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000975 const DexFile& dex_file = klass.GetDexFile();
David Sehr9323e6e2016-09-13 08:58:35 -0700976 Thread* self = Thread::Current();
977 ScopedObjectAccessUnchecked soa(self);
978 StackHandleScope<1> hs(self);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700979 ObjPtr<mirror::Class> annotation_array_class =
980 soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100981 ObjPtr<mirror::Class> annotation_array_array_class =
Vladimir Markobcf17522018-06-01 13:14:32 +0100982 Runtime::Current()->GetClassLinker()->FindArrayClass(self, annotation_array_class);
David Sehr9323e6e2016-09-13 08:58:35 -0700983 if (annotation_array_array_class == nullptr) {
984 return nullptr;
985 }
986 Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
987 mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800988 if (annotation_array_array == nullptr) {
David Sehr9323e6e2016-09-13 08:58:35 -0700989 LOG(ERROR) << "Annotation set ref array allocation failed";
990 return nullptr;
991 }
992 for (uint32_t index = 0; index < size; ++index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800993 const AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
994 const AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100995 ObjPtr<mirror::Object> annotation_set = ProcessAnnotationSet(klass,
996 set_item,
997 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -0700998 if (annotation_set == nullptr) {
999 return nullptr;
1000 }
1001 annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
1002 }
1003 return annotation_array_array.Get();
1004}
1005} // namespace
1006
1007namespace annotations {
1008
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001009ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
1010 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001011 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001012 if (annotation_set == nullptr) {
1013 return nullptr;
1014 }
1015 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001016 const ClassData field_class(hs, field);
1017 return GetAnnotationObjectFromAnnotationSet(field_class,
1018 annotation_set,
1019 DexFile::kDexVisibilityRuntime,
1020 annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001021}
1022
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001023ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001024 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001025 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001026 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001027 return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1028}
1029
Vladimir Markoacb906d2018-05-30 10:23:49 +01001030ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001031 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001032 if (annotation_set == nullptr) {
1033 return nullptr;
1034 }
1035 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001036 const ClassData field_class(hs, field);
David Sehr9323e6e2016-09-13 08:58:35 -07001037 return GetSignatureValue(field_class, annotation_set);
1038}
1039
1040bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001041 const AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
David Sehr9323e6e2016-09-13 08:58:35 -07001042 if (annotation_set == nullptr) {
1043 return false;
1044 }
1045 StackHandleScope<1> hs(Thread::Current());
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001046 const ClassData field_class(hs, field);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001047 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
David Sehr9323e6e2016-09-13 08:58:35 -07001048 field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1049 return annotation_item != nullptr;
1050}
1051
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001052ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001053 const ClassData klass(method);
1054 const DexFile* dex_file = &klass.GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001055 const AnnotationsDirectoryItem* annotations_dir =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001056 dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
David Sehr9323e6e2016-09-13 08:58:35 -07001057 if (annotations_dir == nullptr) {
1058 return nullptr;
1059 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001060 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001061 dex_file->GetClassAnnotationSet(annotations_dir);
1062 if (annotation_set == nullptr) {
1063 return nullptr;
1064 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001065 const AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001066 "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1067 if (annotation_item == nullptr) {
1068 return nullptr;
1069 }
1070 const uint8_t* annotation =
1071 SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1072 if (annotation == nullptr) {
1073 return nullptr;
1074 }
1075 uint8_t header_byte = *(annotation++);
1076 if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1077 return nullptr;
1078 }
1079 annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1080 if (annotation == nullptr) {
1081 return nullptr;
1082 }
1083 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001084 StackHandleScope<1> hs(Thread::Current());
Vladimir Markob45528c2017-07-27 14:14:28 +01001085 Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001086 if (!ProcessAnnotationValue<false>(klass,
Andreas Gampe9486a162017-02-16 15:17:47 -08001087 &annotation,
1088 &annotation_value,
1089 return_type,
1090 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001091 return nullptr;
1092 }
1093 return annotation_value.value_.GetL();
1094}
1095
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001096ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
1097 Handle<mirror::Class> annotation_class) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001098 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001099 if (annotation_set == nullptr) {
1100 return nullptr;
1101 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001102 return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
David Sehr9323e6e2016-09-13 08:58:35 -07001103 DexFile::kDexVisibilityRuntime, annotation_class);
1104}
1105
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001106ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001107 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001108 return ProcessAnnotationSet(ClassData(method),
1109 annotation_set,
1110 DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001111}
1112
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001113ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001114 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001115 if (annotation_set == nullptr) {
1116 return nullptr;
1117 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001118 return GetThrowsValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001119}
1120
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001121ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method) {
David Sehr9323e6e2016-09-13 08:58:35 -07001122 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001123 const ParameterAnnotationsItem* parameter_annotations =
David Sehr9323e6e2016-09-13 08:58:35 -07001124 FindAnnotationsItemForMethod(method);
1125 if (parameter_annotations == nullptr) {
1126 return nullptr;
1127 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001128 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001129 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1130 if (set_ref_list == nullptr) {
1131 return nullptr;
1132 }
1133 uint32_t size = set_ref_list->size_;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001134 return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
David Sehr9323e6e2016-09-13 08:58:35 -07001135}
1136
Orion Hodson58143d22018-02-20 08:44:20 +00001137uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1138 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001139 const ParameterAnnotationsItem* parameter_annotations =
Orion Hodson58143d22018-02-20 08:44:20 +00001140 FindAnnotationsItemForMethod(method);
1141 if (parameter_annotations == nullptr) {
1142 return 0u;
1143 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001144 const AnnotationSetRefList* set_ref_list =
Orion Hodson58143d22018-02-20 08:44:20 +00001145 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1146 if (set_ref_list == nullptr) {
1147 return 0u;
1148 }
1149 return set_ref_list->size_;
1150}
1151
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001152ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
1153 uint32_t parameter_idx,
1154 Handle<mirror::Class> annotation_class) {
David Sehr9323e6e2016-09-13 08:58:35 -07001155 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001156 const ParameterAnnotationsItem* parameter_annotations = FindAnnotationsItemForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001157 if (parameter_annotations == nullptr) {
1158 return nullptr;
1159 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001160 const AnnotationSetRefList* set_ref_list =
David Sehr9323e6e2016-09-13 08:58:35 -07001161 dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1162 if (set_ref_list == nullptr) {
1163 return nullptr;
1164 }
1165 if (parameter_idx >= set_ref_list->size_) {
1166 return nullptr;
1167 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001168 const AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1169 const AnnotationSetItem* annotation_set =
David Sehr9323e6e2016-09-13 08:58:35 -07001170 dex_file->GetSetRefItemItem(annotation_set_ref);
Orion Hodson58143d22018-02-20 08:44:20 +00001171 if (annotation_set == nullptr) {
1172 return nullptr;
1173 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001174 return GetAnnotationObjectFromAnnotationSet(ClassData(method),
David Sehr9323e6e2016-09-13 08:58:35 -07001175 annotation_set,
1176 DexFile::kDexVisibilityRuntime,
1177 annotation_class);
1178}
1179
Vladimir Markoacb906d2018-05-30 10:23:49 +01001180bool GetParametersMetadataForMethod(
1181 ArtMethod* method,
1182 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1183 /*out*/ MutableHandle<mirror::IntArray>* access_flags) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001184 const AnnotationSetItem* annotation_set =
Neil Fuller79a21e72016-09-09 14:24:51 +01001185 FindAnnotationSetForMethod(method);
1186 if (annotation_set == nullptr) {
1187 return false;
1188 }
1189
1190 const DexFile* dex_file = method->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001191 const AnnotationItem* annotation_item =
Neil Fuller79a21e72016-09-09 14:24:51 +01001192 SearchAnnotationSet(*dex_file,
1193 annotation_set,
1194 "Ldalvik/annotation/MethodParameters;",
1195 DexFile::kDexVisibilitySystem);
1196 if (annotation_item == nullptr) {
1197 return false;
1198 }
1199
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001200 StackHandleScope<4> hs(Thread::Current());
Neil Fuller79a21e72016-09-09 14:24:51 +01001201
1202 // Extract the parameters' names String[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001203 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1204 Handle<mirror::Class> string_array_class =
1205 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::String>>(class_linker));
1206 DCHECK(string_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001207
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001208 ClassData data(method);
Neil Fuller79a21e72016-09-09 14:24:51 +01001209 Handle<mirror::Object> names_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001210 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001211 annotation_item,
1212 "names",
1213 string_array_class,
1214 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001215 if (names_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001216 return false;
1217 }
1218
1219 // Extract the parameters' access flags int[].
Vladimir Markoacb906d2018-05-30 10:23:49 +01001220 Handle<mirror::Class> int_array_class(hs.NewHandle(GetClassRoot<mirror::IntArray>(class_linker)));
1221 DCHECK(int_array_class != nullptr);
Neil Fuller79a21e72016-09-09 14:24:51 +01001222 Handle<mirror::Object> access_flags_obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001223 hs.NewHandle(GetAnnotationValue(data,
Neil Fuller79a21e72016-09-09 14:24:51 +01001224 annotation_item,
1225 "accessFlags",
1226 int_array_class,
1227 DexFile::kDexAnnotationArray));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001228 if (access_flags_obj == nullptr) {
Neil Fuller79a21e72016-09-09 14:24:51 +01001229 return false;
1230 }
1231
Vladimir Markoacb906d2018-05-30 10:23:49 +01001232 names->Assign(names_obj->AsObjectArray<mirror::String>());
1233 access_flags->Assign(access_flags_obj->AsIntArray());
Neil Fuller79a21e72016-09-09 14:24:51 +01001234 return true;
1235}
1236
Vladimir Markoacb906d2018-05-30 10:23:49 +01001237ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001238 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001239 if (annotation_set == nullptr) {
1240 return nullptr;
1241 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001242 return GetSignatureValue(ClassData(method), annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001243}
1244
Roland Levillain35e42f02017-06-26 18:14:39 +01001245bool IsMethodAnnotationPresent(ArtMethod* method,
1246 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001247 uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001248 const AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
David Sehr9323e6e2016-09-13 08:58:35 -07001249 if (annotation_set == nullptr) {
1250 return false;
1251 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001252 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Vladimir Marko0db16e02017-11-08 14:32:33 +00001253 ClassData(method), annotation_set, visibility, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001254 return annotation_item != nullptr;
1255}
1256
Vladimir Marko0db16e02017-11-08 14:32:33 +00001257static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1258 if (kIsDebugBuild) {
1259 ScopedObjectAccess soa(Thread::Current());
1260 ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1261 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001262 // WellKnownClasses may not be initialized yet, so `klass` may be null.
1263 if (klass != nullptr) {
1264 // Lookup using the boot class path loader should yield the annotation class.
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001265 CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader= */ nullptr));
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001266 }
Vladimir Marko0db16e02017-11-08 14:32:33 +00001267 }
1268}
1269
1270// Check whether a method from the `dex_file` with the given `annotation_set`
1271// is annotated with `annotation_descriptor` with build visibility.
1272static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001273 const AnnotationSetItem& annotation_set,
Vladimir Marko0db16e02017-11-08 14:32:33 +00001274 const char* annotation_descriptor,
1275 jclass annotation_class) {
1276 for (uint32_t i = 0; i < annotation_set.size_; ++i) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001277 const AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
Vladimir Marko0db16e02017-11-08 14:32:33 +00001278 if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1279 continue;
1280 }
1281 const uint8_t* annotation = annotation_item->annotation_;
1282 uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1283 const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1284 if (strcmp(descriptor, annotation_descriptor) == 0) {
1285 DCheckNativeAnnotation(descriptor, annotation_class);
1286 return true;
1287 }
1288 }
1289 return false;
1290}
1291
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001292uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001293 const dex::ClassDef& class_def,
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001294 uint32_t method_index) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001295 const dex::AnnotationSetItem* annotation_set =
Vladimir Marko0db16e02017-11-08 14:32:33 +00001296 FindAnnotationSetForMethod(dex_file, class_def, method_index);
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001297 if (annotation_set == nullptr) {
1298 return 0u;
1299 }
1300 uint32_t access_flags = 0u;
1301 if (IsMethodBuildAnnotationPresent(
1302 dex_file,
1303 *annotation_set,
1304 "Ldalvik/annotation/optimization/FastNative;",
1305 WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1306 access_flags |= kAccFastNative;
1307 }
1308 if (IsMethodBuildAnnotationPresent(
1309 dex_file,
1310 *annotation_set,
1311 "Ldalvik/annotation/optimization/CriticalNative;",
1312 WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1313 access_flags |= kAccCriticalNative;
1314 }
1315 CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1316 return access_flags;
Vladimir Marko0db16e02017-11-08 14:32:33 +00001317}
1318
Hans Boehm206348c2018-12-05 11:11:33 -08001319bool FieldIsReachabilitySensitive(const DexFile& dex_file,
1320 const dex::ClassDef& class_def,
1321 uint32_t field_index)
1322 REQUIRES_SHARED(Locks::mutator_lock_) {
1323 const AnnotationSetItem* annotation_set =
1324 FindAnnotationSetForField(dex_file, class_def, field_index);
1325 if (annotation_set == nullptr) {
1326 return false;
1327 }
1328 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1329 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1330 // TODO: We're missing the equivalent of DCheckNativeAnnotation (not a DCHECK). Does it matter?
1331 return annotation_item != nullptr;
1332}
1333
1334bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1335 const dex::ClassDef& class_def,
1336 uint32_t method_index)
1337 REQUIRES_SHARED(Locks::mutator_lock_) {
1338 const AnnotationSetItem* annotation_set =
1339 FindAnnotationSetForMethod(dex_file, class_def, method_index);
1340 if (annotation_set == nullptr) {
1341 return false;
1342 }
1343 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1344 "Ldalvik/annotation/optimization/ReachabilitySensitive;", DexFile::kDexVisibilityRuntime);
1345 return annotation_item != nullptr;
1346}
1347
1348static bool MethodIsReachabilitySensitive(const DexFile& dex_file,
1349 uint32_t method_index)
1350 REQUIRES_SHARED(Locks::mutator_lock_) {
1351 DCHECK(method_index < dex_file.NumMethodIds());
1352 const dex::MethodId& method_id = dex_file.GetMethodId(method_index);
1353 dex::TypeIndex class_index = method_id.class_idx_;
1354 const dex::ClassDef * class_def = dex_file.FindClassDef(class_index);
1355 return class_def != nullptr
1356 && MethodIsReachabilitySensitive(dex_file, *class_def, method_index);
1357}
1358
1359bool MethodContainsRSensitiveAccess(const DexFile& dex_file,
1360 const dex::ClassDef& class_def,
1361 uint32_t method_index)
1362 REQUIRES_SHARED(Locks::mutator_lock_) {
1363 // TODO: This is too slow to run very regularly. Currently this is only invoked in the
1364 // presence of @DeadReferenceSafe, which will be rare. In the long run, we need to quickly
1365 // check once whether a class has any @ReachabilitySensitive annotations. If not, we can
1366 // immediately return false here for any method in that class.
1367 uint32_t code_item_offset = dex_file.FindCodeItemOffset(class_def, method_index);
1368 const dex::CodeItem* code_item = dex_file.GetCodeItem(code_item_offset);
1369 CodeItemInstructionAccessor accessor(dex_file, code_item);
1370 if (!accessor.HasCodeItem()) {
1371 return false;
1372 }
1373 ArrayRef<const uint8_t> quicken_data;
1374 const OatDexFile* oat_dex_file = dex_file.GetOatDexFile();
1375 if (oat_dex_file != nullptr) {
1376 quicken_data = oat_dex_file->GetQuickenedInfoOf(dex_file, method_index);
1377 }
1378 const QuickenInfoTable quicken_info(quicken_data);
1379 uint32_t quicken_index = 0;
1380 for (DexInstructionIterator iter = accessor.begin(); iter != accessor.end(); ++iter) {
1381 switch (iter->Opcode()) {
1382 case Instruction::IGET:
1383 case Instruction::IGET_QUICK:
1384 case Instruction::IGET_WIDE:
1385 case Instruction::IGET_WIDE_QUICK:
1386 case Instruction::IGET_OBJECT:
1387 case Instruction::IGET_OBJECT_QUICK:
1388 case Instruction::IGET_BOOLEAN:
1389 case Instruction::IGET_BOOLEAN_QUICK:
1390 case Instruction::IGET_BYTE:
1391 case Instruction::IGET_BYTE_QUICK:
1392 case Instruction::IGET_CHAR:
1393 case Instruction::IGET_CHAR_QUICK:
1394 case Instruction::IGET_SHORT:
1395 case Instruction::IGET_SHORT_QUICK:
1396 case Instruction::IPUT:
1397 case Instruction::IPUT_QUICK:
1398 case Instruction::IPUT_WIDE:
1399 case Instruction::IPUT_WIDE_QUICK:
1400 case Instruction::IPUT_OBJECT:
1401 case Instruction::IPUT_OBJECT_QUICK:
1402 case Instruction::IPUT_BOOLEAN:
1403 case Instruction::IPUT_BOOLEAN_QUICK:
1404 case Instruction::IPUT_BYTE:
1405 case Instruction::IPUT_BYTE_QUICK:
1406 case Instruction::IPUT_CHAR:
1407 case Instruction::IPUT_CHAR_QUICK:
1408 case Instruction::IPUT_SHORT:
1409 case Instruction::IPUT_SHORT_QUICK:
1410 {
1411 uint32_t field_index;
1412 if (iter->IsQuickened()) {
1413 field_index = quicken_info.GetData(quicken_index);
1414 } else {
1415 field_index = iter->VRegC_22c();
1416 }
1417 DCHECK(field_index < dex_file.NumFieldIds());
1418 // We only guarantee to pay attention to the annotation if it's in the same class,
1419 // or a containing class, but it's OK to do so in other cases.
1420 const dex::FieldId& field_id = dex_file.GetFieldId(field_index);
1421 dex::TypeIndex class_index = field_id.class_idx_;
1422 const dex::ClassDef * field_class_def = dex_file.FindClassDef(class_index);
1423 // We do not handle the case in which the field is declared in a superclass, and
1424 // don't claim to do so. The annotated field should normally be private.
1425 if (field_class_def != nullptr
1426 && FieldIsReachabilitySensitive(dex_file, *field_class_def, field_index)) {
1427 return true;
1428 }
1429 }
1430 break;
1431 case Instruction::INVOKE_SUPER:
1432 // Cannot call method in same class. TODO: Try an explicit superclass lookup for
1433 // better "best effort"?
1434 break;
1435 case Instruction::INVOKE_INTERFACE:
1436 // We handle an interface call just like a virtual call. We will find annotations
1437 // on interface methods/fields visible to us, but not of the annotation is in a
1438 // super-interface. Again, we could just ignore it.
1439 case Instruction::INVOKE_VIRTUAL:
1440 case Instruction::INVOKE_DIRECT:
1441 {
1442 uint32_t called_method_index = iter->VRegB_35c();
1443 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1444 return true;
1445 }
1446 }
1447 break;
1448 case Instruction::INVOKE_INTERFACE_RANGE:
1449 case Instruction::INVOKE_VIRTUAL_RANGE:
1450 case Instruction::INVOKE_DIRECT_RANGE:
1451 {
1452 uint32_t called_method_index = iter->VRegB_3rc();
1453 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1454 return true;
1455 }
1456 }
1457 break;
1458 case Instruction::INVOKE_VIRTUAL_QUICK:
1459 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
1460 {
1461 uint32_t called_method_index = quicken_info.GetData(quicken_index);
1462 if (MethodIsReachabilitySensitive(dex_file, called_method_index)) {
1463 return true;
1464 }
1465 }
1466 break;
1467 // We explicitly do not handle indirect ReachabilitySensitive accesses through VarHandles,
1468 // etc. Thus we ignore INVOKE_CUSTOM / INVOKE_CUSTOM_RANGE / INVOKE_POLYMORPHIC /
1469 // INVOKE_POLYMORPHIC_RANGE.
1470 default:
1471 // There is no way to add an annotation to array elements, and so far we've encountered no
1472 // need for that, so we ignore AGET and APUT.
1473 // It's impractical or impossible to garbage collect a class while one of its methods is
1474 // on the call stack. We allow ReachabilitySensitive annotations on static methods and
1475 // fields, but they can be safely ignored.
1476 break;
1477 }
1478 if (QuickenInfoTable::NeedsIndexForInstruction(&iter.Inst())) {
1479 ++quicken_index;
1480 }
1481 }
1482 return false;
1483}
1484
1485bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file,
1486 const dex::ClassDef& class_def)
1487 // TODO: This should check outer classes as well.
1488 // It's conservatively correct not to do so.
1489 REQUIRES_SHARED(Locks::mutator_lock_) {
1490 const AnnotationsDirectoryItem* annotations_dir =
1491 dex_file.GetAnnotationsDirectory(class_def);
1492 if (annotations_dir == nullptr) {
1493 return false;
1494 }
1495 const AnnotationSetItem* annotation_set = dex_file.GetClassAnnotationSet(annotations_dir);
1496 if (annotation_set == nullptr) {
1497 return false;
1498 }
1499 const AnnotationItem* annotation_item = SearchAnnotationSet(dex_file, annotation_set,
1500 "Ldalvik/annotation/optimization/DeadReferenceSafe;", DexFile::kDexVisibilityRuntime);
1501 return annotation_item != nullptr;
1502}
1503
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001504ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
1505 Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001506 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001507 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001508 if (annotation_set == nullptr) {
1509 return nullptr;
1510 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001511 return GetAnnotationObjectFromAnnotationSet(data,
1512 annotation_set,
1513 DexFile::kDexVisibilityRuntime,
David Sehr9323e6e2016-09-13 08:58:35 -07001514 annotation_class);
1515}
1516
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001517ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001518 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001519 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001520 return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
David Sehr9323e6e2016-09-13 08:58:35 -07001521}
1522
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001523ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001524 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001525 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001526 if (annotation_set == nullptr) {
1527 return nullptr;
1528 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001529 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001530 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
David Sehr9323e6e2016-09-13 08:58:35 -07001531 DexFile::kDexVisibilitySystem);
1532 if (annotation_item == nullptr) {
1533 return nullptr;
1534 }
1535 StackHandleScope<1> hs(Thread::Current());
Vladimir Markoacb906d2018-05-30 10:23:49 +01001536 Handle<mirror::Class> class_array_class =
1537 hs.NewHandle(GetClassRoot<mirror::ObjectArray<mirror::Class>>());
1538 DCHECK(class_array_class != nullptr);
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001539 ObjPtr<mirror::Object> obj =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001540 GetAnnotationValue(data, annotation_item, "value", class_array_class,
David Sehr9323e6e2016-09-13 08:58:35 -07001541 DexFile::kDexAnnotationArray);
1542 if (obj == nullptr) {
1543 return nullptr;
1544 }
1545 return obj->AsObjectArray<mirror::Class>();
1546}
1547
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001548ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001549 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001550 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001551 if (annotation_set == nullptr) {
1552 return nullptr;
1553 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001554 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001555 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001556 DexFile::kDexVisibilitySystem);
1557 if (annotation_item == nullptr) {
1558 return nullptr;
1559 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001560 ObjPtr<mirror::Object> obj = GetAnnotationValue(data,
1561 annotation_item,
1562 "value",
1563 ScopedNullHandle<mirror::Class>(),
1564 DexFile::kDexAnnotationType);
David Sehr9323e6e2016-09-13 08:58:35 -07001565 if (obj == nullptr) {
1566 return nullptr;
1567 }
1568 return obj->AsClass();
1569}
1570
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001571ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass) {
1572 ObjPtr<mirror::Class> declaring_class = GetDeclaringClass(klass);
David Sehr9323e6e2016-09-13 08:58:35 -07001573 if (declaring_class != nullptr) {
1574 return declaring_class;
1575 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001576 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001577 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001578 if (annotation_set == nullptr) {
1579 return nullptr;
1580 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001581 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001582 SearchAnnotationSet(data.GetDexFile(),
1583 annotation_set,
1584 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001585 DexFile::kDexVisibilitySystem);
1586 if (annotation_item == nullptr) {
1587 return nullptr;
1588 }
1589 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001590 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
David Sehr9323e6e2016-09-13 08:58:35 -07001591 if (annotation == nullptr) {
1592 return nullptr;
1593 }
1594 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001595 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001596 &annotation,
1597 &annotation_value,
1598 ScopedNullHandle<mirror::Class>(),
1599 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001600 return nullptr;
1601 }
1602 if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1603 return nullptr;
1604 }
1605 StackHandleScope<2> hs(Thread::Current());
David Sehr9323e6e2016-09-13 08:58:35 -07001606 ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001607 annotation_value.value_.GetI(),
1608 hs.NewHandle(data.GetDexCache()),
1609 hs.NewHandle(data.GetClassLoader()));
David Sehr9323e6e2016-09-13 08:58:35 -07001610 if (method == nullptr) {
1611 return nullptr;
1612 }
1613 return method->GetDeclaringClass();
1614}
1615
Vladimir Marko2d3065e2018-05-22 13:56:09 +01001616ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001617 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001618 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001619 if (annotation_set == nullptr) {
1620 return nullptr;
1621 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001622 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001623 SearchAnnotationSet(data.GetDexFile(),
1624 annotation_set,
1625 "Ldalvik/annotation/EnclosingMethod;",
David Sehr9323e6e2016-09-13 08:58:35 -07001626 DexFile::kDexVisibilitySystem);
1627 if (annotation_item == nullptr) {
1628 return nullptr;
1629 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001630 return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
David Sehr9323e6e2016-09-13 08:58:35 -07001631 DexFile::kDexAnnotationMethod);
1632}
1633
Vladimir Markoacb906d2018-05-30 10:23:49 +01001634bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001635 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001636 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001637 if (annotation_set == nullptr) {
1638 return false;
1639 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001640 const AnnotationItem* annotation_item = SearchAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001641 data.GetDexFile(),
1642 annotation_set,
1643 "Ldalvik/annotation/InnerClass;",
1644 DexFile::kDexVisibilitySystem);
David Sehr9323e6e2016-09-13 08:58:35 -07001645 if (annotation_item == nullptr) {
1646 return false;
1647 }
1648 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001649 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
David Sehr9323e6e2016-09-13 08:58:35 -07001650 if (annotation == nullptr) {
1651 return false;
1652 }
1653 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001654 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001655 &annotation,
1656 &annotation_value,
1657 ScopedNullHandle<mirror::Class>(),
1658 DexFile::kAllObjects)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001659 return false;
1660 }
1661 if (annotation_value.type_ != DexFile::kDexAnnotationNull &&
1662 annotation_value.type_ != DexFile::kDexAnnotationString) {
1663 return false;
1664 }
1665 *name = down_cast<mirror::String*>(annotation_value.value_.GetL());
1666 return true;
1667}
1668
1669bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001670 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001671 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001672 if (annotation_set == nullptr) {
1673 return false;
1674 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001675 const AnnotationItem* annotation_item =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001676 SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
David Sehr9323e6e2016-09-13 08:58:35 -07001677 DexFile::kDexVisibilitySystem);
1678 if (annotation_item == nullptr) {
1679 return false;
1680 }
1681 const uint8_t* annotation =
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001682 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
David Sehr9323e6e2016-09-13 08:58:35 -07001683 if (annotation == nullptr) {
1684 return false;
1685 }
1686 DexFile::AnnotationValue annotation_value;
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001687 if (!ProcessAnnotationValue<false>(data,
Andreas Gampe9486a162017-02-16 15:17:47 -08001688 &annotation,
1689 &annotation_value,
1690 ScopedNullHandle<mirror::Class>(),
1691 DexFile::kAllRaw)) {
David Sehr9323e6e2016-09-13 08:58:35 -07001692 return false;
1693 }
1694 if (annotation_value.type_ != DexFile::kDexAnnotationInt) {
1695 return false;
1696 }
1697 *flags = annotation_value.value_.GetI();
1698 return true;
1699}
1700
Vladimir Markoacb906d2018-05-30 10:23:49 +01001701ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
1702 Handle<mirror::Class> klass) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001703 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001704 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001705 if (annotation_set == nullptr) {
1706 return nullptr;
1707 }
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001708 return GetSignatureValue(data, annotation_set);
David Sehr9323e6e2016-09-13 08:58:35 -07001709}
1710
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001711const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001712 // Before instantiating ClassData, check that klass has a DexCache
1713 // assigned. The ClassData constructor indirectly dereferences it
1714 // when calling klass->GetDexFile().
1715 if (klass->GetDexCache() == nullptr) {
1716 DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1717 return nullptr;
1718 }
1719
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001720 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001721 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001722 if (annotation_set == nullptr) {
1723 return nullptr;
1724 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001725
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001726 const AnnotationItem* annotation_item = SearchAnnotationSet(
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001727 data.GetDexFile(),
1728 annotation_set,
1729 "Ldalvik/annotation/SourceDebugExtension;",
1730 DexFile::kDexVisibilitySystem);
1731 if (annotation_item == nullptr) {
1732 return nullptr;
1733 }
Orion Hodsoncf7127b2017-05-09 09:51:35 +01001734
Orion Hodson77d8a1c2017-04-24 14:53:19 +01001735 const uint8_t* annotation =
1736 SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1737 if (annotation == nullptr) {
1738 return nullptr;
1739 }
1740 DexFile::AnnotationValue annotation_value;
1741 if (!ProcessAnnotationValue<false>(data,
1742 &annotation,
1743 &annotation_value,
1744 ScopedNullHandle<mirror::Class>(),
1745 DexFile::kAllRaw)) {
1746 return nullptr;
1747 }
1748 if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1749 return nullptr;
1750 }
1751 dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1752 return data.GetDexFile().StringDataByIdx(index);
1753}
1754
David Sehr9323e6e2016-09-13 08:58:35 -07001755bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001756 ClassData data(klass);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001757 const AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
David Sehr9323e6e2016-09-13 08:58:35 -07001758 if (annotation_set == nullptr) {
1759 return false;
1760 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001761 const AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
Alex Lightf2f1c9d2017-03-15 15:35:46 +00001762 data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
David Sehr9323e6e2016-09-13 08:58:35 -07001763 return annotation_item != nullptr;
1764}
1765
1766int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1767 // For native method, lineno should be -2 to indicate it is native. Note that
1768 // "line number == -2" is how libcore tells from StackTraceElement.
1769 if (method->GetCodeItemOffset() == 0) {
1770 return -2;
1771 }
1772
David Sehr0225f8e2018-01-31 08:52:24 +00001773 CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -08001774 DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
David Sehr9323e6e2016-09-13 08:58:35 -07001775
1776 // A method with no line number info should return -1
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001777 uint32_t line_num = -1;
1778 accessor.GetLineNumForPc(rel_pc, &line_num);
1779 return line_num;
David Sehr9323e6e2016-09-13 08:58:35 -07001780}
1781
1782template<bool kTransactionActive>
1783void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1784 DCHECK(dex_cache_ != nullptr);
David Sehr9323e6e2016-09-13 08:58:35 -07001785 switch (type_) {
1786 case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1787 break;
1788 case kByte: field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1789 case kShort: field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1790 case kChar: field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1791 case kInt: field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1792 case kLong: field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1793 case kFloat: field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1794 case kDouble: field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1795 case kNull: field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1796 case kString: {
Vladimir Markoa64b52d2017-12-08 16:27:49 +00001797 ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001798 dex_cache_);
David Sehr9323e6e2016-09-13 08:58:35 -07001799 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1800 break;
1801 }
1802 case kType: {
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001803 ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
Vladimir Markoe11dd502017-12-08 14:09:45 +00001804 dex_cache_,
1805 class_loader_);
David Sehr9323e6e2016-09-13 08:58:35 -07001806 field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1807 break;
1808 }
1809 default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1810 }
1811}
1812template
1813void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1814template
1815void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1816
1817} // namespace annotations
1818
1819} // namespace art