1 /*
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 
21 #include "android-base/stringprintf.h"
22 
23 #include "art_field-inl.h"
24 #include "art_method-inl.h"
25 #include "class_linker-inl.h"
26 #include "dex/dex_file-inl.h"
27 #include "jni_internal.h"
28 #include "jvalue-inl.h"
29 #include "mirror/field.h"
30 #include "mirror/method.h"
31 #include "oat_file.h"
32 #include "reflection.h"
33 #include "thread.h"
34 #include "well_known_classes.h"
35 
36 namespace art {
37 
38 using android::base::StringPrintf;
39 
40 struct DexFile::AnnotationValue {
41   JValue value_;
42   uint8_t type_;
43 };
44 
45 namespace {
46 
47 // A helper class that contains all the data needed to do annotation lookup.
48 class ClassData {
49  public:
REQUIRES_SHARED(Locks::mutator_lock_)50   explicit ClassData(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_)
51     : ClassData(ScopedNullHandle<mirror::Class>(),  // klass
52                 method,
53                 *method->GetDexFile(),
54                 &method->GetClassDef()) {}
55 
56   // Requires Scope to be able to create at least 1 handles.
57   template <typename Scope>
ClassData(Scope & hs,ArtField * field)58   ClassData(Scope& hs, ArtField* field) REQUIRES_SHARED(Locks::mutator_lock_)
59     : ClassData(hs.NewHandle(field->GetDeclaringClass())) { }
60 
REQUIRES_SHARED(art::Locks::mutator_lock_)61   explicit ClassData(Handle<mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_)
62     : ClassData(klass,  // klass
63                 nullptr,  // method
64                 klass->GetDexFile(),
65                 klass->GetClassDef()) {}
66 
GetDexFile() const67   const DexFile& GetDexFile() const REQUIRES_SHARED(Locks::mutator_lock_) {
68     return dex_file_;
69   }
70 
GetClassDef() const71   const DexFile::ClassDef* GetClassDef() const REQUIRES_SHARED(Locks::mutator_lock_) {
72     return class_def_;
73   }
74 
GetDexCache() const75   ObjPtr<mirror::DexCache> GetDexCache() const REQUIRES_SHARED(Locks::mutator_lock_) {
76     if (method_ != nullptr) {
77       return method_->GetDexCache();
78     } else {
79       return real_klass_->GetDexCache();
80     }
81   }
82 
GetClassLoader() const83   ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) {
84     if (method_ != nullptr) {
85       return method_->GetDeclaringClass()->GetClassLoader();
86     } else {
87       return real_klass_->GetClassLoader();
88     }
89   }
90 
GetRealClass() const91   ObjPtr<mirror::Class> GetRealClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
92     if (method_ != nullptr) {
93       return method_->GetDeclaringClass();
94     } else {
95       return real_klass_.Get();
96     }
97   }
98 
99  private:
ClassData(Handle<mirror::Class> klass,ArtMethod * method,const DexFile & dex_file,const DexFile::ClassDef * class_def)100   ClassData(Handle<mirror::Class> klass,
101             ArtMethod* method,
102             const DexFile& dex_file,
103             const DexFile::ClassDef* class_def) REQUIRES_SHARED(Locks::mutator_lock_)
104       : real_klass_(klass),
105         method_(method),
106         dex_file_(dex_file),
107         class_def_(class_def) {
108     DCHECK((method_ == nullptr) || real_klass_.IsNull());
109   }
110 
111   Handle<mirror::Class> real_klass_;
112   ArtMethod* method_;
113   const DexFile& dex_file_;
114   const DexFile::ClassDef* class_def_;
115 
116   DISALLOW_COPY_AND_ASSIGN(ClassData);
117 };
118 
119 mirror::Object* CreateAnnotationMember(const ClassData& klass,
120                                        Handle<mirror::Class> annotation_class,
121                                        const uint8_t** annotation)
122     REQUIRES_SHARED(Locks::mutator_lock_);
123 
IsVisibilityCompatible(uint32_t actual,uint32_t expected)124 bool IsVisibilityCompatible(uint32_t actual, uint32_t expected) {
125   if (expected == DexFile::kDexVisibilityRuntime) {
126     int32_t sdk_version = Runtime::Current()->GetTargetSdkVersion();
127     if (sdk_version > 0 && sdk_version <= 23) {
128       return actual == DexFile::kDexVisibilityRuntime || actual == DexFile::kDexVisibilityBuild;
129     }
130   }
131   return actual == expected;
132 }
133 
FindAnnotationSetForField(ArtField * field)134 const DexFile::AnnotationSetItem* FindAnnotationSetForField(ArtField* field)
135     REQUIRES_SHARED(Locks::mutator_lock_) {
136   const DexFile* dex_file = field->GetDexFile();
137   ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
138   const DexFile::ClassDef* class_def = klass->GetClassDef();
139   if (class_def == nullptr) {
140     DCHECK(klass->IsProxyClass());
141     return nullptr;
142   }
143   const DexFile::AnnotationsDirectoryItem* annotations_dir =
144       dex_file->GetAnnotationsDirectory(*class_def);
145   if (annotations_dir == nullptr) {
146     return nullptr;
147   }
148   const DexFile::FieldAnnotationsItem* field_annotations =
149       dex_file->GetFieldAnnotations(annotations_dir);
150   if (field_annotations == nullptr) {
151     return nullptr;
152   }
153   uint32_t field_index = field->GetDexFieldIndex();
154   uint32_t field_count = annotations_dir->fields_size_;
155   for (uint32_t i = 0; i < field_count; ++i) {
156     if (field_annotations[i].field_idx_ == field_index) {
157       return dex_file->GetFieldAnnotationSetItem(field_annotations[i]);
158     }
159   }
160   return nullptr;
161 }
162 
SearchAnnotationSet(const DexFile & dex_file,const DexFile::AnnotationSetItem * annotation_set,const char * descriptor,uint32_t visibility)163 const DexFile::AnnotationItem* SearchAnnotationSet(const DexFile& dex_file,
164                                                    const DexFile::AnnotationSetItem* annotation_set,
165                                                    const char* descriptor,
166                                                    uint32_t visibility)
167     REQUIRES_SHARED(Locks::mutator_lock_) {
168   const DexFile::AnnotationItem* result = nullptr;
169   for (uint32_t i = 0; i < annotation_set->size_; ++i) {
170     const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
171     if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
172       continue;
173     }
174     const uint8_t* annotation = annotation_item->annotation_;
175     uint32_t type_index = DecodeUnsignedLeb128(&annotation);
176 
177     if (strcmp(descriptor, dex_file.StringByTypeIdx(dex::TypeIndex(type_index))) == 0) {
178       result = annotation_item;
179       break;
180     }
181   }
182   return result;
183 }
184 
SkipAnnotationValue(const DexFile & dex_file,const uint8_t ** annotation_ptr)185 bool SkipAnnotationValue(const DexFile& dex_file, const uint8_t** annotation_ptr)
186     REQUIRES_SHARED(Locks::mutator_lock_) {
187   const uint8_t* annotation = *annotation_ptr;
188   uint8_t header_byte = *(annotation++);
189   uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
190   uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
191   int32_t width = value_arg + 1;
192 
193   switch (value_type) {
194     case DexFile::kDexAnnotationByte:
195     case DexFile::kDexAnnotationShort:
196     case DexFile::kDexAnnotationChar:
197     case DexFile::kDexAnnotationInt:
198     case DexFile::kDexAnnotationLong:
199     case DexFile::kDexAnnotationFloat:
200     case DexFile::kDexAnnotationDouble:
201     case DexFile::kDexAnnotationString:
202     case DexFile::kDexAnnotationType:
203     case DexFile::kDexAnnotationMethod:
204     case DexFile::kDexAnnotationField:
205     case DexFile::kDexAnnotationEnum:
206       break;
207     case DexFile::kDexAnnotationArray:
208     {
209       uint32_t size = DecodeUnsignedLeb128(&annotation);
210       while (size--) {
211         if (!SkipAnnotationValue(dex_file, &annotation)) {
212           return false;
213         }
214       }
215       width = 0;
216       break;
217     }
218     case DexFile::kDexAnnotationAnnotation:
219     {
220       DecodeUnsignedLeb128(&annotation);  // unused type_index
221       uint32_t size = DecodeUnsignedLeb128(&annotation);
222       while (size--) {
223         DecodeUnsignedLeb128(&annotation);  // unused element_name_index
224         if (!SkipAnnotationValue(dex_file, &annotation)) {
225           return false;
226         }
227       }
228       width = 0;
229       break;
230     }
231     case DexFile::kDexAnnotationBoolean:
232     case DexFile::kDexAnnotationNull:
233       width = 0;
234       break;
235     default:
236       LOG(FATAL) << StringPrintf("Bad annotation element value byte 0x%02x", value_type);
237       return false;
238   }
239 
240   annotation += width;
241   *annotation_ptr = annotation;
242   return true;
243 }
244 
SearchEncodedAnnotation(const DexFile & dex_file,const uint8_t * annotation,const char * name)245 const uint8_t* SearchEncodedAnnotation(const DexFile& dex_file,
246                                        const uint8_t* annotation,
247                                        const char* name)
248     REQUIRES_SHARED(Locks::mutator_lock_) {
249   DecodeUnsignedLeb128(&annotation);  // unused type_index
250   uint32_t size = DecodeUnsignedLeb128(&annotation);
251 
252   while (size != 0) {
253     uint32_t element_name_index = DecodeUnsignedLeb128(&annotation);
254     const char* element_name =
255         dex_file.GetStringData(dex_file.GetStringId(dex::StringIndex(element_name_index)));
256     if (strcmp(name, element_name) == 0) {
257       return annotation;
258     }
259     SkipAnnotationValue(dex_file, &annotation);
260     size--;
261   }
262   return nullptr;
263 }
264 
FindAnnotationSetForMethod(const DexFile & dex_file,const DexFile::ClassDef & class_def,uint32_t method_index)265 const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(const DexFile& dex_file,
266                                                              const DexFile::ClassDef& class_def,
267                                                              uint32_t method_index) {
268   const DexFile::AnnotationsDirectoryItem* annotations_dir =
269       dex_file.GetAnnotationsDirectory(class_def);
270   if (annotations_dir == nullptr) {
271     return nullptr;
272   }
273   const DexFile::MethodAnnotationsItem* method_annotations =
274       dex_file.GetMethodAnnotations(annotations_dir);
275   if (method_annotations == nullptr) {
276     return nullptr;
277   }
278   uint32_t method_count = annotations_dir->methods_size_;
279   for (uint32_t i = 0; i < method_count; ++i) {
280     if (method_annotations[i].method_idx_ == method_index) {
281       return dex_file.GetMethodAnnotationSetItem(method_annotations[i]);
282     }
283   }
284   return nullptr;
285 }
286 
FindAnnotationSetForMethod(ArtMethod * method)287 inline const DexFile::AnnotationSetItem* FindAnnotationSetForMethod(ArtMethod* method)
288     REQUIRES_SHARED(Locks::mutator_lock_) {
289   if (method->IsProxyMethod()) {
290     return nullptr;
291   }
292   return FindAnnotationSetForMethod(*method->GetDexFile(),
293                                     method->GetClassDef(),
294                                     method->GetDexMethodIndex());
295 }
296 
FindAnnotationsItemForMethod(ArtMethod * method)297 const DexFile::ParameterAnnotationsItem* FindAnnotationsItemForMethod(ArtMethod* method)
298     REQUIRES_SHARED(Locks::mutator_lock_) {
299   const DexFile* dex_file = method->GetDexFile();
300   const DexFile::AnnotationsDirectoryItem* annotations_dir =
301       dex_file->GetAnnotationsDirectory(method->GetClassDef());
302   if (annotations_dir == nullptr) {
303     return nullptr;
304   }
305   const DexFile::ParameterAnnotationsItem* parameter_annotations =
306       dex_file->GetParameterAnnotations(annotations_dir);
307   if (parameter_annotations == nullptr) {
308     return nullptr;
309   }
310   uint32_t method_index = method->GetDexMethodIndex();
311   uint32_t parameter_count = annotations_dir->parameters_size_;
312   for (uint32_t i = 0; i < parameter_count; ++i) {
313     if (parameter_annotations[i].method_idx_ == method_index) {
314       return &parameter_annotations[i];
315     }
316   }
317   return nullptr;
318 }
319 
FindAnnotationSetForClass(const ClassData & klass)320 const DexFile::AnnotationSetItem* FindAnnotationSetForClass(const ClassData& klass)
321     REQUIRES_SHARED(Locks::mutator_lock_) {
322   const DexFile& dex_file = klass.GetDexFile();
323   const DexFile::ClassDef* class_def = klass.GetClassDef();
324   if (class_def == nullptr) {
325     DCHECK(klass.GetRealClass()->IsProxyClass());
326     return nullptr;
327   }
328   const DexFile::AnnotationsDirectoryItem* annotations_dir =
329       dex_file.GetAnnotationsDirectory(*class_def);
330   if (annotations_dir == nullptr) {
331     return nullptr;
332   }
333   return dex_file.GetClassAnnotationSet(annotations_dir);
334 }
335 
ProcessEncodedAnnotation(const ClassData & klass,const uint8_t ** annotation)336 mirror::Object* ProcessEncodedAnnotation(const ClassData& klass, const uint8_t** annotation)
337     REQUIRES_SHARED(Locks::mutator_lock_) {
338   uint32_t type_index = DecodeUnsignedLeb128(annotation);
339   uint32_t size = DecodeUnsignedLeb128(annotation);
340 
341   Thread* self = Thread::Current();
342   ScopedObjectAccessUnchecked soa(self);
343   StackHandleScope<4> hs(self);
344   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
345   Handle<mirror::Class> annotation_class(hs.NewHandle(
346       class_linker->ResolveType(dex::TypeIndex(type_index),
347                                 hs.NewHandle(klass.GetDexCache()),
348                                 hs.NewHandle(klass.GetClassLoader()))));
349   if (annotation_class == nullptr) {
350     LOG(INFO) << "Unable to resolve " << klass.GetRealClass()->PrettyClass()
351               << " annotation class " << type_index;
352     DCHECK(Thread::Current()->IsExceptionPending());
353     Thread::Current()->ClearException();
354     return nullptr;
355   }
356 
357   ObjPtr<mirror::Class> annotation_member_class =
358       soa.Decode<mirror::Class>(WellKnownClasses::libcore_reflect_AnnotationMember).Ptr();
359   mirror::Class* annotation_member_array_class =
360       class_linker->FindArrayClass(self, &annotation_member_class);
361   if (annotation_member_array_class == nullptr) {
362     return nullptr;
363   }
364   mirror::ObjectArray<mirror::Object>* element_array = nullptr;
365   if (size > 0) {
366     element_array =
367         mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_member_array_class, size);
368     if (element_array == nullptr) {
369       LOG(ERROR) << "Failed to allocate annotation member array (" << size << " elements)";
370       return nullptr;
371     }
372   }
373 
374   Handle<mirror::ObjectArray<mirror::Object>> h_element_array(hs.NewHandle(element_array));
375   for (uint32_t i = 0; i < size; ++i) {
376     mirror::Object* new_member = CreateAnnotationMember(klass, annotation_class, annotation);
377     if (new_member == nullptr) {
378       return nullptr;
379     }
380     h_element_array->SetWithoutChecks<false>(i, new_member);
381   }
382 
383   JValue result;
384   ArtMethod* create_annotation_method =
385       jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation);
386   uint32_t args[2] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(annotation_class.Get())),
387                        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_element_array.Get())) };
388   create_annotation_method->Invoke(self, args, sizeof(args), &result, "LLL");
389   if (self->IsExceptionPending()) {
390     LOG(INFO) << "Exception in AnnotationFactory.createAnnotation";
391     return nullptr;
392   }
393 
394   return result.GetL();
395 }
396 
397 template <bool kTransactionActive>
ProcessAnnotationValue(const ClassData & klass,const uint8_t ** annotation_ptr,DexFile::AnnotationValue * annotation_value,Handle<mirror::Class> array_class,DexFile::AnnotationResultStyle result_style)398 bool ProcessAnnotationValue(const ClassData& klass,
399                             const uint8_t** annotation_ptr,
400                             DexFile::AnnotationValue* annotation_value,
401                             Handle<mirror::Class> array_class,
402                             DexFile::AnnotationResultStyle result_style)
403     REQUIRES_SHARED(Locks::mutator_lock_) {
404   const DexFile& dex_file = klass.GetDexFile();
405   Thread* self = Thread::Current();
406   ObjPtr<mirror::Object> element_object = nullptr;
407   bool set_object = false;
408   Primitive::Type primitive_type = Primitive::kPrimVoid;
409   const uint8_t* annotation = *annotation_ptr;
410   uint8_t header_byte = *(annotation++);
411   uint8_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
412   uint8_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
413   int32_t width = value_arg + 1;
414   annotation_value->type_ = value_type;
415 
416   switch (value_type) {
417     case DexFile::kDexAnnotationByte:
418       annotation_value->value_.SetB(
419           static_cast<int8_t>(DexFile::ReadSignedInt(annotation, value_arg)));
420       primitive_type = Primitive::kPrimByte;
421       break;
422     case DexFile::kDexAnnotationShort:
423       annotation_value->value_.SetS(
424           static_cast<int16_t>(DexFile::ReadSignedInt(annotation, value_arg)));
425       primitive_type = Primitive::kPrimShort;
426       break;
427     case DexFile::kDexAnnotationChar:
428       annotation_value->value_.SetC(
429           static_cast<uint16_t>(DexFile::ReadUnsignedInt(annotation, value_arg, false)));
430       primitive_type = Primitive::kPrimChar;
431       break;
432     case DexFile::kDexAnnotationInt:
433       annotation_value->value_.SetI(DexFile::ReadSignedInt(annotation, value_arg));
434       primitive_type = Primitive::kPrimInt;
435       break;
436     case DexFile::kDexAnnotationLong:
437       annotation_value->value_.SetJ(DexFile::ReadSignedLong(annotation, value_arg));
438       primitive_type = Primitive::kPrimLong;
439       break;
440     case DexFile::kDexAnnotationFloat:
441       annotation_value->value_.SetI(DexFile::ReadUnsignedInt(annotation, value_arg, true));
442       primitive_type = Primitive::kPrimFloat;
443       break;
444     case DexFile::kDexAnnotationDouble:
445       annotation_value->value_.SetJ(DexFile::ReadUnsignedLong(annotation, value_arg, true));
446       primitive_type = Primitive::kPrimDouble;
447       break;
448     case DexFile::kDexAnnotationBoolean:
449       annotation_value->value_.SetZ(value_arg != 0);
450       primitive_type = Primitive::kPrimBoolean;
451       width = 0;
452       break;
453     case DexFile::kDexAnnotationString: {
454       uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
455       if (result_style == DexFile::kAllRaw) {
456         annotation_value->value_.SetI(index);
457       } else {
458         StackHandleScope<1> hs(self);
459         element_object = Runtime::Current()->GetClassLinker()->ResolveString(
460             dex::StringIndex(index), hs.NewHandle(klass.GetDexCache()));
461         set_object = true;
462         if (element_object == nullptr) {
463           return false;
464         }
465       }
466       break;
467     }
468     case DexFile::kDexAnnotationType: {
469       uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
470       if (result_style == DexFile::kAllRaw) {
471         annotation_value->value_.SetI(index);
472       } else {
473         dex::TypeIndex type_index(index);
474         StackHandleScope<2> hs(self);
475         element_object = Runtime::Current()->GetClassLinker()->ResolveType(
476             type_index,
477             hs.NewHandle(klass.GetDexCache()),
478             hs.NewHandle(klass.GetClassLoader()));
479         set_object = true;
480         if (element_object == nullptr) {
481           CHECK(self->IsExceptionPending());
482           if (result_style == DexFile::kAllObjects) {
483             const char* msg = dex_file.StringByTypeIdx(type_index);
484             self->ThrowNewWrappedException("Ljava/lang/TypeNotPresentException;", msg);
485             element_object = self->GetException();
486             self->ClearException();
487           } else {
488             return false;
489           }
490         }
491       }
492       break;
493     }
494     case DexFile::kDexAnnotationMethod: {
495       uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
496       if (result_style == DexFile::kAllRaw) {
497         annotation_value->value_.SetI(index);
498       } else {
499         ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
500         StackHandleScope<2> hs(self);
501         ArtMethod* method = class_linker->ResolveMethodWithoutInvokeType(
502             index,
503             hs.NewHandle(klass.GetDexCache()),
504             hs.NewHandle(klass.GetClassLoader()));
505         if (method == nullptr) {
506           return false;
507         }
508         PointerSize pointer_size = class_linker->GetImagePointerSize();
509         set_object = true;
510         if (method->IsConstructor()) {
511           if (pointer_size == PointerSize::k64) {
512             element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k64,
513                 kTransactionActive>(self, method);
514           } else {
515             element_object = mirror::Constructor::CreateFromArtMethod<PointerSize::k32,
516                 kTransactionActive>(self, method);
517           }
518         } else {
519           if (pointer_size == PointerSize::k64) {
520             element_object = mirror::Method::CreateFromArtMethod<PointerSize::k64,
521                 kTransactionActive>(self, method);
522           } else {
523             element_object = mirror::Method::CreateFromArtMethod<PointerSize::k32,
524                 kTransactionActive>(self, method);
525           }
526         }
527         if (element_object == nullptr) {
528           return false;
529         }
530       }
531       break;
532     }
533     case DexFile::kDexAnnotationField: {
534       uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
535       if (result_style == DexFile::kAllRaw) {
536         annotation_value->value_.SetI(index);
537       } else {
538         StackHandleScope<2> hs(self);
539         ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(
540             index,
541             hs.NewHandle(klass.GetDexCache()),
542             hs.NewHandle(klass.GetClassLoader()));
543         if (field == nullptr) {
544           return false;
545         }
546         set_object = true;
547         PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
548         if (pointer_size == PointerSize::k64) {
549           element_object = mirror::Field::CreateFromArtField<PointerSize::k64,
550               kTransactionActive>(self, field, true);
551         } else {
552           element_object = mirror::Field::CreateFromArtField<PointerSize::k32,
553               kTransactionActive>(self, field, true);
554         }
555         if (element_object == nullptr) {
556           return false;
557         }
558       }
559       break;
560     }
561     case DexFile::kDexAnnotationEnum: {
562       uint32_t index = DexFile::ReadUnsignedInt(annotation, value_arg, false);
563       if (result_style == DexFile::kAllRaw) {
564         annotation_value->value_.SetI(index);
565       } else {
566         StackHandleScope<3> hs(self);
567         ArtField* enum_field = Runtime::Current()->GetClassLinker()->ResolveField(
568             index,
569             hs.NewHandle(klass.GetDexCache()),
570             hs.NewHandle(klass.GetClassLoader()),
571             true);
572         if (enum_field == nullptr) {
573           return false;
574         } else {
575           Handle<mirror::Class> field_class(hs.NewHandle(enum_field->GetDeclaringClass()));
576           Runtime::Current()->GetClassLinker()->EnsureInitialized(self, field_class, true, true);
577           element_object = enum_field->GetObject(field_class.Get());
578           set_object = true;
579         }
580       }
581       break;
582     }
583     case DexFile::kDexAnnotationArray:
584       if (result_style == DexFile::kAllRaw || array_class == nullptr) {
585         return false;
586       } else {
587         ScopedObjectAccessUnchecked soa(self);
588         StackHandleScope<2> hs(self);
589         uint32_t size = DecodeUnsignedLeb128(&annotation);
590         Handle<mirror::Class> component_type(hs.NewHandle(array_class->GetComponentType()));
591         Handle<mirror::Array> new_array(hs.NewHandle(mirror::Array::Alloc<true>(
592             self, array_class.Get(), size, array_class->GetComponentSizeShift(),
593             Runtime::Current()->GetHeap()->GetCurrentAllocator())));
594         if (new_array == nullptr) {
595           LOG(ERROR) << "Annotation element array allocation failed with size " << size;
596           return false;
597         }
598         DexFile::AnnotationValue new_annotation_value;
599         for (uint32_t i = 0; i < size; ++i) {
600           if (!ProcessAnnotationValue<kTransactionActive>(klass,
601                                                           &annotation,
602                                                           &new_annotation_value,
603                                                           component_type,
604                                                           DexFile::kPrimitivesOrObjects)) {
605             return false;
606           }
607           if (!component_type->IsPrimitive()) {
608             mirror::Object* obj = new_annotation_value.value_.GetL();
609             new_array->AsObjectArray<mirror::Object>()->
610                 SetWithoutChecks<kTransactionActive>(i, obj);
611           } else {
612             switch (new_annotation_value.type_) {
613               case DexFile::kDexAnnotationByte:
614                 new_array->AsByteArray()->SetWithoutChecks<kTransactionActive>(
615                     i, new_annotation_value.value_.GetB());
616                 break;
617               case DexFile::kDexAnnotationShort:
618                 new_array->AsShortArray()->SetWithoutChecks<kTransactionActive>(
619                     i, new_annotation_value.value_.GetS());
620                 break;
621               case DexFile::kDexAnnotationChar:
622                 new_array->AsCharArray()->SetWithoutChecks<kTransactionActive>(
623                     i, new_annotation_value.value_.GetC());
624                 break;
625               case DexFile::kDexAnnotationInt:
626                 new_array->AsIntArray()->SetWithoutChecks<kTransactionActive>(
627                     i, new_annotation_value.value_.GetI());
628                 break;
629               case DexFile::kDexAnnotationLong:
630                 new_array->AsLongArray()->SetWithoutChecks<kTransactionActive>(
631                     i, new_annotation_value.value_.GetJ());
632                 break;
633               case DexFile::kDexAnnotationFloat:
634                 new_array->AsFloatArray()->SetWithoutChecks<kTransactionActive>(
635                     i, new_annotation_value.value_.GetF());
636                 break;
637               case DexFile::kDexAnnotationDouble:
638                 new_array->AsDoubleArray()->SetWithoutChecks<kTransactionActive>(
639                     i, new_annotation_value.value_.GetD());
640                 break;
641               case DexFile::kDexAnnotationBoolean:
642                 new_array->AsBooleanArray()->SetWithoutChecks<kTransactionActive>(
643                     i, new_annotation_value.value_.GetZ());
644                 break;
645               default:
646                 LOG(FATAL) << "Found invalid annotation value type while building annotation array";
647                 return false;
648             }
649           }
650         }
651         element_object = new_array.Get();
652         set_object = true;
653         width = 0;
654       }
655       break;
656     case DexFile::kDexAnnotationAnnotation:
657       if (result_style == DexFile::kAllRaw) {
658         return false;
659       }
660       element_object = ProcessEncodedAnnotation(klass, &annotation);
661       if (element_object == nullptr) {
662         return false;
663       }
664       set_object = true;
665       width = 0;
666       break;
667     case DexFile::kDexAnnotationNull:
668       if (result_style == DexFile::kAllRaw) {
669         annotation_value->value_.SetI(0);
670       } else {
671         CHECK(element_object == nullptr);
672         set_object = true;
673       }
674       width = 0;
675       break;
676     default:
677       LOG(ERROR) << StringPrintf("Bad annotation element value type 0x%02x", value_type);
678       return false;
679   }
680 
681   annotation += width;
682   *annotation_ptr = annotation;
683 
684   if (result_style == DexFile::kAllObjects && primitive_type != Primitive::kPrimVoid) {
685     element_object = BoxPrimitive(primitive_type, annotation_value->value_).Ptr();
686     set_object = true;
687   }
688 
689   if (set_object) {
690     annotation_value->value_.SetL(element_object.Ptr());
691   }
692 
693   return true;
694 }
695 
CreateAnnotationMember(const ClassData & klass,Handle<mirror::Class> annotation_class,const uint8_t ** annotation)696 mirror::Object* CreateAnnotationMember(const ClassData& klass,
697                                        Handle<mirror::Class> annotation_class,
698                                        const uint8_t** annotation) {
699   const DexFile& dex_file = klass.GetDexFile();
700   Thread* self = Thread::Current();
701   ScopedObjectAccessUnchecked soa(self);
702   StackHandleScope<5> hs(self);
703   uint32_t element_name_index = DecodeUnsignedLeb128(annotation);
704   const char* name = dex_file.StringDataByIdx(dex::StringIndex(element_name_index));
705   Handle<mirror::String> string_name(
706       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(self, name)));
707 
708   PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
709   ArtMethod* annotation_method =
710       annotation_class->FindDeclaredVirtualMethodByName(name, pointer_size);
711   if (annotation_method == nullptr) {
712     return nullptr;
713   }
714   Handle<mirror::Class> method_return(hs.NewHandle(annotation_method->ResolveReturnType()));
715 
716   DexFile::AnnotationValue annotation_value;
717   if (!ProcessAnnotationValue<false>(klass,
718                                      annotation,
719                                      &annotation_value,
720                                      method_return,
721                                      DexFile::kAllObjects)) {
722     return nullptr;
723   }
724   Handle<mirror::Object> value_object(hs.NewHandle(annotation_value.value_.GetL()));
725 
726   ObjPtr<mirror::Class> annotation_member_class =
727       WellKnownClasses::ToClass(WellKnownClasses::libcore_reflect_AnnotationMember);
728   Handle<mirror::Object> new_member(hs.NewHandle(annotation_member_class->AllocObject(self)));
729   mirror::Method* method_obj_ptr;
730   DCHECK(!Runtime::Current()->IsActiveTransaction());
731   if (pointer_size == PointerSize::k64) {
732     method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k64, false>(
733         self, annotation_method);
734   } else {
735     method_obj_ptr = mirror::Method::CreateFromArtMethod<PointerSize::k32, false>(
736         self, annotation_method);
737   }
738   Handle<mirror::Method> method_object(hs.NewHandle(method_obj_ptr));
739 
740   if (new_member == nullptr || string_name == nullptr ||
741       method_object == nullptr || method_return == nullptr) {
742     LOG(ERROR) << StringPrintf("Failed creating annotation element (m=%p n=%p a=%p r=%p",
743         new_member.Get(), string_name.Get(), method_object.Get(), method_return.Get());
744     return nullptr;
745   }
746 
747   JValue result;
748   ArtMethod* annotation_member_init =
749       jni::DecodeArtMethod(WellKnownClasses::libcore_reflect_AnnotationMember_init);
750   uint32_t args[5] = { static_cast<uint32_t>(reinterpret_cast<uintptr_t>(new_member.Get())),
751                        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(string_name.Get())),
752                        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(value_object.Get())),
753                        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_return.Get())),
754                        static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_object.Get()))
755   };
756   annotation_member_init->Invoke(self, args, sizeof(args), &result, "VLLLL");
757   if (self->IsExceptionPending()) {
758     LOG(INFO) << "Exception in AnnotationMember.<init>";
759     return nullptr;
760   }
761 
762   return new_member.Get();
763 }
764 
GetAnnotationItemFromAnnotationSet(const ClassData & klass,const DexFile::AnnotationSetItem * annotation_set,uint32_t visibility,Handle<mirror::Class> annotation_class)765 const DexFile::AnnotationItem* GetAnnotationItemFromAnnotationSet(
766     const ClassData& klass,
767     const DexFile::AnnotationSetItem* annotation_set,
768     uint32_t visibility,
769     Handle<mirror::Class> annotation_class)
770     REQUIRES_SHARED(Locks::mutator_lock_) {
771   const DexFile& dex_file = klass.GetDexFile();
772   for (uint32_t i = 0; i < annotation_set->size_; ++i) {
773     const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
774     if (!IsVisibilityCompatible(annotation_item->visibility_, visibility)) {
775       continue;
776     }
777     const uint8_t* annotation = annotation_item->annotation_;
778     uint32_t type_index = DecodeUnsignedLeb128(&annotation);
779     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
780     Thread* self = Thread::Current();
781     StackHandleScope<2> hs(self);
782     ObjPtr<mirror::Class> resolved_class = class_linker->ResolveType(
783         dex::TypeIndex(type_index),
784         hs.NewHandle(klass.GetDexCache()),
785         hs.NewHandle(klass.GetClassLoader()));
786     if (resolved_class == nullptr) {
787       std::string temp;
788       LOG(WARNING) << StringPrintf("Unable to resolve %s annotation class %d",
789                                    klass.GetRealClass()->GetDescriptor(&temp), type_index);
790       CHECK(self->IsExceptionPending());
791       self->ClearException();
792       continue;
793     }
794     if (resolved_class == annotation_class.Get()) {
795       return annotation_item;
796     }
797   }
798 
799   return nullptr;
800 }
801 
GetAnnotationObjectFromAnnotationSet(const ClassData & klass,const DexFile::AnnotationSetItem * annotation_set,uint32_t visibility,Handle<mirror::Class> annotation_class)802 mirror::Object* GetAnnotationObjectFromAnnotationSet(
803     const ClassData& klass,
804     const DexFile::AnnotationSetItem* annotation_set,
805     uint32_t visibility,
806     Handle<mirror::Class> annotation_class)
807     REQUIRES_SHARED(Locks::mutator_lock_) {
808   const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
809       klass, annotation_set, visibility, annotation_class);
810   if (annotation_item == nullptr) {
811     return nullptr;
812   }
813   const uint8_t* annotation = annotation_item->annotation_;
814   return ProcessEncodedAnnotation(klass, &annotation);
815 }
816 
GetAnnotationValue(const ClassData & klass,const DexFile::AnnotationItem * annotation_item,const char * annotation_name,Handle<mirror::Class> array_class,uint32_t expected_type)817 mirror::Object* GetAnnotationValue(const ClassData& klass,
818                                    const DexFile::AnnotationItem* annotation_item,
819                                    const char* annotation_name,
820                                    Handle<mirror::Class> array_class,
821                                    uint32_t expected_type)
822     REQUIRES_SHARED(Locks::mutator_lock_) {
823   const DexFile& dex_file = klass.GetDexFile();
824   const uint8_t* annotation =
825       SearchEncodedAnnotation(dex_file, annotation_item->annotation_, annotation_name);
826   if (annotation == nullptr) {
827     return nullptr;
828   }
829   DexFile::AnnotationValue annotation_value;
830   bool result = Runtime::Current()->IsActiveTransaction()
831       ? ProcessAnnotationValue<true>(klass,
832                                      &annotation,
833                                      &annotation_value,
834                                      array_class,
835                                      DexFile::kAllObjects)
836       : ProcessAnnotationValue<false>(klass,
837                                       &annotation,
838                                       &annotation_value,
839                                       array_class,
840                                       DexFile::kAllObjects);
841   if (!result) {
842     return nullptr;
843   }
844   if (annotation_value.type_ != expected_type) {
845     return nullptr;
846   }
847   return annotation_value.value_.GetL();
848 }
849 
GetSignatureValue(const ClassData & klass,const DexFile::AnnotationSetItem * annotation_set)850 mirror::ObjectArray<mirror::String>* GetSignatureValue(const ClassData& klass,
851     const DexFile::AnnotationSetItem* annotation_set)
852     REQUIRES_SHARED(Locks::mutator_lock_) {
853   const DexFile& dex_file = klass.GetDexFile();
854   StackHandleScope<1> hs(Thread::Current());
855   const DexFile::AnnotationItem* annotation_item =
856       SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Signature;",
857                           DexFile::kDexVisibilitySystem);
858   if (annotation_item == nullptr) {
859     return nullptr;
860   }
861   ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
862   Handle<mirror::Class> string_array_class(hs.NewHandle(
863       Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
864   if (string_array_class == nullptr) {
865     return nullptr;
866   }
867   mirror::Object* obj =
868       GetAnnotationValue(klass, annotation_item, "value", string_array_class,
869                          DexFile::kDexAnnotationArray);
870   if (obj == nullptr) {
871     return nullptr;
872   }
873   return obj->AsObjectArray<mirror::String>();
874 }
875 
GetThrowsValue(const ClassData & klass,const DexFile::AnnotationSetItem * annotation_set)876 mirror::ObjectArray<mirror::Class>* GetThrowsValue(const ClassData& klass,
877                                                    const DexFile::AnnotationSetItem* annotation_set)
878     REQUIRES_SHARED(Locks::mutator_lock_) {
879   const DexFile& dex_file = klass.GetDexFile();
880   StackHandleScope<1> hs(Thread::Current());
881   const DexFile::AnnotationItem* annotation_item =
882       SearchAnnotationSet(dex_file, annotation_set, "Ldalvik/annotation/Throws;",
883                           DexFile::kDexVisibilitySystem);
884   if (annotation_item == nullptr) {
885     return nullptr;
886   }
887   ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
888   Handle<mirror::Class> class_array_class(hs.NewHandle(
889       Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &class_class)));
890   if (class_array_class == nullptr) {
891     return nullptr;
892   }
893   mirror::Object* obj =
894       GetAnnotationValue(klass, annotation_item, "value", class_array_class,
895                          DexFile::kDexAnnotationArray);
896   if (obj == nullptr) {
897     return nullptr;
898   }
899   return obj->AsObjectArray<mirror::Class>();
900 }
901 
ProcessAnnotationSet(const ClassData & klass,const DexFile::AnnotationSetItem * annotation_set,uint32_t visibility)902 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSet(
903     const ClassData& klass,
904     const DexFile::AnnotationSetItem* annotation_set,
905     uint32_t visibility)
906     REQUIRES_SHARED(Locks::mutator_lock_) {
907   const DexFile& dex_file = klass.GetDexFile();
908   Thread* self = Thread::Current();
909   ScopedObjectAccessUnchecked soa(self);
910   StackHandleScope<2> hs(self);
911   Handle<mirror::Class> annotation_array_class(hs.NewHandle(
912       soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array)));
913   if (annotation_set == nullptr) {
914     return mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), 0);
915   }
916 
917   uint32_t size = annotation_set->size_;
918   Handle<mirror::ObjectArray<mirror::Object>> result(hs.NewHandle(
919       mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), size)));
920   if (result == nullptr) {
921     return nullptr;
922   }
923 
924   uint32_t dest_index = 0;
925   for (uint32_t i = 0; i < size; ++i) {
926     const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(annotation_set, i);
927     // Note that we do not use IsVisibilityCompatible here because older code
928     // was correct for this case.
929     if (annotation_item->visibility_ != visibility) {
930       continue;
931     }
932     const uint8_t* annotation = annotation_item->annotation_;
933     mirror::Object* annotation_obj = ProcessEncodedAnnotation(klass, &annotation);
934     if (annotation_obj != nullptr) {
935       result->SetWithoutChecks<false>(dest_index, annotation_obj);
936       ++dest_index;
937     } else if (self->IsExceptionPending()) {
938       return nullptr;
939     }
940   }
941 
942   if (dest_index == size) {
943     return result.Get();
944   }
945 
946   mirror::ObjectArray<mirror::Object>* trimmed_result =
947       mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_class.Get(), dest_index);
948   if (trimmed_result == nullptr) {
949     return nullptr;
950   }
951 
952   for (uint32_t i = 0; i < dest_index; ++i) {
953     mirror::Object* obj = result->GetWithoutChecks(i);
954     trimmed_result->SetWithoutChecks<false>(i, obj);
955   }
956 
957   return trimmed_result;
958 }
959 
ProcessAnnotationSetRefList(const ClassData & klass,const DexFile::AnnotationSetRefList * set_ref_list,uint32_t size)960 mirror::ObjectArray<mirror::Object>* ProcessAnnotationSetRefList(
961     const ClassData& klass,
962     const DexFile::AnnotationSetRefList* set_ref_list,
963     uint32_t size)
964     REQUIRES_SHARED(Locks::mutator_lock_) {
965   const DexFile& dex_file = klass.GetDexFile();
966   Thread* self = Thread::Current();
967   ScopedObjectAccessUnchecked soa(self);
968   StackHandleScope<1> hs(self);
969   ObjPtr<mirror::Class> annotation_array_class =
970       soa.Decode<mirror::Class>(WellKnownClasses::java_lang_annotation_Annotation__array);
971   mirror::Class* annotation_array_array_class =
972       Runtime::Current()->GetClassLinker()->FindArrayClass(self, &annotation_array_class);
973   if (annotation_array_array_class == nullptr) {
974     return nullptr;
975   }
976   Handle<mirror::ObjectArray<mirror::Object>> annotation_array_array(hs.NewHandle(
977       mirror::ObjectArray<mirror::Object>::Alloc(self, annotation_array_array_class, size)));
978   if (annotation_array_array == nullptr) {
979     LOG(ERROR) << "Annotation set ref array allocation failed";
980     return nullptr;
981   }
982   for (uint32_t index = 0; index < size; ++index) {
983     const DexFile::AnnotationSetRefItem* set_ref_item = &set_ref_list->list_[index];
984     const DexFile::AnnotationSetItem* set_item = dex_file.GetSetRefItemItem(set_ref_item);
985     mirror::Object* annotation_set = ProcessAnnotationSet(klass, set_item,
986                                                           DexFile::kDexVisibilityRuntime);
987     if (annotation_set == nullptr) {
988       return nullptr;
989     }
990     annotation_array_array->SetWithoutChecks<false>(index, annotation_set);
991   }
992   return annotation_array_array.Get();
993 }
994 }  // namespace
995 
996 namespace annotations {
997 
GetAnnotationForField(ArtField * field,Handle<mirror::Class> annotation_class)998 mirror::Object* GetAnnotationForField(ArtField* field, Handle<mirror::Class> annotation_class) {
999   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1000   if (annotation_set == nullptr) {
1001     return nullptr;
1002   }
1003   StackHandleScope<1> hs(Thread::Current());
1004   const ClassData field_class(hs, field);
1005   return GetAnnotationObjectFromAnnotationSet(field_class,
1006                                               annotation_set,
1007                                               DexFile::kDexVisibilityRuntime,
1008                                               annotation_class);
1009 }
1010 
GetAnnotationsForField(ArtField * field)1011 mirror::ObjectArray<mirror::Object>* GetAnnotationsForField(ArtField* field) {
1012   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1013   StackHandleScope<1> hs(Thread::Current());
1014   const ClassData field_class(hs, field);
1015   return ProcessAnnotationSet(field_class, annotation_set, DexFile::kDexVisibilityRuntime);
1016 }
1017 
GetSignatureAnnotationForField(ArtField * field)1018 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForField(ArtField* field) {
1019   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1020   if (annotation_set == nullptr) {
1021     return nullptr;
1022   }
1023   StackHandleScope<1> hs(Thread::Current());
1024   const ClassData field_class(hs, field);
1025   return GetSignatureValue(field_class, annotation_set);
1026 }
1027 
IsFieldAnnotationPresent(ArtField * field,Handle<mirror::Class> annotation_class)1028 bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class) {
1029   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForField(field);
1030   if (annotation_set == nullptr) {
1031     return false;
1032   }
1033   StackHandleScope<1> hs(Thread::Current());
1034   const ClassData field_class(hs, field);
1035   const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1036       field_class, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1037   return annotation_item != nullptr;
1038 }
1039 
GetAnnotationDefaultValue(ArtMethod * method)1040 mirror::Object* GetAnnotationDefaultValue(ArtMethod* method) {
1041   const ClassData klass(method);
1042   const DexFile* dex_file = &klass.GetDexFile();
1043   const DexFile::AnnotationsDirectoryItem* annotations_dir =
1044       dex_file->GetAnnotationsDirectory(*klass.GetClassDef());
1045   if (annotations_dir == nullptr) {
1046     return nullptr;
1047   }
1048   const DexFile::AnnotationSetItem* annotation_set =
1049       dex_file->GetClassAnnotationSet(annotations_dir);
1050   if (annotation_set == nullptr) {
1051     return nullptr;
1052   }
1053   const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(*dex_file, annotation_set,
1054       "Ldalvik/annotation/AnnotationDefault;", DexFile::kDexVisibilitySystem);
1055   if (annotation_item == nullptr) {
1056     return nullptr;
1057   }
1058   const uint8_t* annotation =
1059       SearchEncodedAnnotation(*dex_file, annotation_item->annotation_, "value");
1060   if (annotation == nullptr) {
1061     return nullptr;
1062   }
1063   uint8_t header_byte = *(annotation++);
1064   if ((header_byte & DexFile::kDexAnnotationValueTypeMask) != DexFile::kDexAnnotationAnnotation) {
1065     return nullptr;
1066   }
1067   annotation = SearchEncodedAnnotation(*dex_file, annotation, method->GetName());
1068   if (annotation == nullptr) {
1069     return nullptr;
1070   }
1071   DexFile::AnnotationValue annotation_value;
1072   StackHandleScope<1> hs(Thread::Current());
1073   Handle<mirror::Class> return_type(hs.NewHandle(method->ResolveReturnType()));
1074   if (!ProcessAnnotationValue<false>(klass,
1075                                      &annotation,
1076                                      &annotation_value,
1077                                      return_type,
1078                                      DexFile::kAllObjects)) {
1079     return nullptr;
1080   }
1081   return annotation_value.value_.GetL();
1082 }
1083 
GetAnnotationForMethod(ArtMethod * method,Handle<mirror::Class> annotation_class)1084 mirror::Object* GetAnnotationForMethod(ArtMethod* method, Handle<mirror::Class> annotation_class) {
1085   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1086   if (annotation_set == nullptr) {
1087     return nullptr;
1088   }
1089   return GetAnnotationObjectFromAnnotationSet(ClassData(method), annotation_set,
1090                                               DexFile::kDexVisibilityRuntime, annotation_class);
1091 }
1092 
GetAnnotationsForMethod(ArtMethod * method)1093 mirror::ObjectArray<mirror::Object>* GetAnnotationsForMethod(ArtMethod* method) {
1094   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1095   return ProcessAnnotationSet(ClassData(method),
1096                               annotation_set,
1097                               DexFile::kDexVisibilityRuntime);
1098 }
1099 
GetExceptionTypesForMethod(ArtMethod * method)1100 mirror::ObjectArray<mirror::Class>* GetExceptionTypesForMethod(ArtMethod* method) {
1101   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1102   if (annotation_set == nullptr) {
1103     return nullptr;
1104   }
1105   return GetThrowsValue(ClassData(method), annotation_set);
1106 }
1107 
GetParameterAnnotations(ArtMethod * method)1108 mirror::ObjectArray<mirror::Object>* GetParameterAnnotations(ArtMethod* method) {
1109   const DexFile* dex_file = method->GetDexFile();
1110   const DexFile::ParameterAnnotationsItem* parameter_annotations =
1111       FindAnnotationsItemForMethod(method);
1112   if (parameter_annotations == nullptr) {
1113     return nullptr;
1114   }
1115   const DexFile::AnnotationSetRefList* set_ref_list =
1116       dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1117   if (set_ref_list == nullptr) {
1118     return nullptr;
1119   }
1120   uint32_t size = set_ref_list->size_;
1121   return ProcessAnnotationSetRefList(ClassData(method), set_ref_list, size);
1122 }
1123 
GetNumberOfAnnotatedMethodParameters(ArtMethod * method)1124 uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method) {
1125   const DexFile* dex_file = method->GetDexFile();
1126   const DexFile::ParameterAnnotationsItem* parameter_annotations =
1127       FindAnnotationsItemForMethod(method);
1128   if (parameter_annotations == nullptr) {
1129     return 0u;
1130   }
1131   const DexFile::AnnotationSetRefList* set_ref_list =
1132       dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1133   if (set_ref_list == nullptr) {
1134     return 0u;
1135   }
1136   return set_ref_list->size_;
1137 }
1138 
GetAnnotationForMethodParameter(ArtMethod * method,uint32_t parameter_idx,Handle<mirror::Class> annotation_class)1139 mirror::Object* GetAnnotationForMethodParameter(ArtMethod* method,
1140                                                 uint32_t parameter_idx,
1141                                                 Handle<mirror::Class> annotation_class) {
1142   const DexFile* dex_file = method->GetDexFile();
1143   const DexFile::ParameterAnnotationsItem* parameter_annotations =
1144       FindAnnotationsItemForMethod(method);
1145   if (parameter_annotations == nullptr) {
1146     return nullptr;
1147   }
1148   const DexFile::AnnotationSetRefList* set_ref_list =
1149       dex_file->GetParameterAnnotationSetRefList(parameter_annotations);
1150   if (set_ref_list == nullptr) {
1151     return nullptr;
1152   }
1153   if (parameter_idx >= set_ref_list->size_) {
1154     return nullptr;
1155   }
1156   const DexFile::AnnotationSetRefItem* annotation_set_ref = &set_ref_list->list_[parameter_idx];
1157   const DexFile::AnnotationSetItem* annotation_set =
1158      dex_file->GetSetRefItemItem(annotation_set_ref);
1159   if (annotation_set == nullptr) {
1160     return nullptr;
1161   }
1162   return GetAnnotationObjectFromAnnotationSet(ClassData(method),
1163                                               annotation_set,
1164                                               DexFile::kDexVisibilityRuntime,
1165                                               annotation_class);
1166 }
1167 
GetParametersMetadataForMethod(ArtMethod * method,MutableHandle<mirror::ObjectArray<mirror::String>> * names,MutableHandle<mirror::IntArray> * access_flags)1168 bool GetParametersMetadataForMethod(ArtMethod* method,
1169                                     MutableHandle<mirror::ObjectArray<mirror::String>>* names,
1170                                     MutableHandle<mirror::IntArray>* access_flags) {
1171   const DexFile::AnnotationSetItem* annotation_set =
1172       FindAnnotationSetForMethod(method);
1173   if (annotation_set == nullptr) {
1174     return false;
1175   }
1176 
1177   const DexFile* dex_file = method->GetDexFile();
1178   const DexFile::AnnotationItem* annotation_item =
1179       SearchAnnotationSet(*dex_file,
1180                           annotation_set,
1181                           "Ldalvik/annotation/MethodParameters;",
1182                           DexFile::kDexVisibilitySystem);
1183   if (annotation_item == nullptr) {
1184     return false;
1185   }
1186 
1187   StackHandleScope<4> hs(Thread::Current());
1188 
1189   // Extract the parameters' names String[].
1190   ObjPtr<mirror::Class> string_class = mirror::String::GetJavaLangString();
1191   Handle<mirror::Class> string_array_class(hs.NewHandle(
1192       Runtime::Current()->GetClassLinker()->FindArrayClass(Thread::Current(), &string_class)));
1193   if (UNLIKELY(string_array_class == nullptr)) {
1194     return false;
1195   }
1196 
1197   ClassData data(method);
1198   Handle<mirror::Object> names_obj =
1199       hs.NewHandle(GetAnnotationValue(data,
1200                                       annotation_item,
1201                                       "names",
1202                                       string_array_class,
1203                                       DexFile::kDexAnnotationArray));
1204   if (names_obj == nullptr) {
1205     return false;
1206   }
1207 
1208   // Extract the parameters' access flags int[].
1209   Handle<mirror::Class> int_array_class(hs.NewHandle(mirror::IntArray::GetArrayClass()));
1210   if (UNLIKELY(int_array_class == nullptr)) {
1211     return false;
1212   }
1213   Handle<mirror::Object> access_flags_obj =
1214       hs.NewHandle(GetAnnotationValue(data,
1215                                       annotation_item,
1216                                       "accessFlags",
1217                                       int_array_class,
1218                                       DexFile::kDexAnnotationArray));
1219   if (access_flags_obj == nullptr) {
1220     return false;
1221   }
1222 
1223   names->Assign(names_obj.Get()->AsObjectArray<mirror::String>());
1224   access_flags->Assign(access_flags_obj.Get()->AsIntArray());
1225   return true;
1226 }
1227 
GetSignatureAnnotationForMethod(ArtMethod * method)1228 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForMethod(ArtMethod* method) {
1229   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1230   if (annotation_set == nullptr) {
1231     return nullptr;
1232   }
1233   return GetSignatureValue(ClassData(method), annotation_set);
1234 }
1235 
IsMethodAnnotationPresent(ArtMethod * method,Handle<mirror::Class> annotation_class,uint32_t visibility)1236 bool IsMethodAnnotationPresent(ArtMethod* method,
1237                                Handle<mirror::Class> annotation_class,
1238                                uint32_t visibility /* = DexFile::kDexVisibilityRuntime */) {
1239   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForMethod(method);
1240   if (annotation_set == nullptr) {
1241     return false;
1242   }
1243   const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1244       ClassData(method), annotation_set, visibility, annotation_class);
1245   return annotation_item != nullptr;
1246 }
1247 
DCheckNativeAnnotation(const char * descriptor,jclass cls)1248 static void DCheckNativeAnnotation(const char* descriptor, jclass cls) {
1249   if (kIsDebugBuild) {
1250     ScopedObjectAccess soa(Thread::Current());
1251     ObjPtr<mirror::Class> klass = soa.Decode<mirror::Class>(cls);
1252     ClassLinker* linker = Runtime::Current()->GetClassLinker();
1253     // WellKnownClasses may not be initialized yet, so `klass` may be null.
1254     if (klass != nullptr) {
1255       // Lookup using the boot class path loader should yield the annotation class.
1256       CHECK_EQ(klass, linker->LookupClass(soa.Self(), descriptor, /* class_loader */ nullptr));
1257     }
1258   }
1259 }
1260 
1261 // Check whether a method from the `dex_file` with the given `annotation_set`
1262 // is annotated with `annotation_descriptor` with build visibility.
IsMethodBuildAnnotationPresent(const DexFile & dex_file,const DexFile::AnnotationSetItem & annotation_set,const char * annotation_descriptor,jclass annotation_class)1263 static bool IsMethodBuildAnnotationPresent(const DexFile& dex_file,
1264                                            const DexFile::AnnotationSetItem& annotation_set,
1265                                            const char* annotation_descriptor,
1266                                            jclass annotation_class) {
1267   for (uint32_t i = 0; i < annotation_set.size_; ++i) {
1268     const DexFile::AnnotationItem* annotation_item = dex_file.GetAnnotationItem(&annotation_set, i);
1269     if (!IsVisibilityCompatible(annotation_item->visibility_, DexFile::kDexVisibilityBuild)) {
1270       continue;
1271     }
1272     const uint8_t* annotation = annotation_item->annotation_;
1273     uint32_t type_index = DecodeUnsignedLeb128(&annotation);
1274     const char* descriptor = dex_file.StringByTypeIdx(dex::TypeIndex(type_index));
1275     if (strcmp(descriptor, annotation_descriptor) == 0) {
1276       DCheckNativeAnnotation(descriptor, annotation_class);
1277       return true;
1278     }
1279   }
1280   return false;
1281 }
1282 
GetNativeMethodAnnotationAccessFlags(const DexFile & dex_file,const DexFile::ClassDef & class_def,uint32_t method_index)1283 uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
1284                                               const DexFile::ClassDef& class_def,
1285                                               uint32_t method_index) {
1286   const DexFile::AnnotationSetItem* annotation_set =
1287       FindAnnotationSetForMethod(dex_file, class_def, method_index);
1288   if (annotation_set == nullptr) {
1289     return 0u;
1290   }
1291   uint32_t access_flags = 0u;
1292   if (IsMethodBuildAnnotationPresent(
1293           dex_file,
1294           *annotation_set,
1295           "Ldalvik/annotation/optimization/FastNative;",
1296           WellKnownClasses::dalvik_annotation_optimization_FastNative)) {
1297     access_flags |= kAccFastNative;
1298   }
1299   if (IsMethodBuildAnnotationPresent(
1300           dex_file,
1301           *annotation_set,
1302           "Ldalvik/annotation/optimization/CriticalNative;",
1303           WellKnownClasses::dalvik_annotation_optimization_CriticalNative)) {
1304     access_flags |= kAccCriticalNative;
1305   }
1306   CHECK_NE(access_flags, kAccFastNative | kAccCriticalNative);
1307   return access_flags;
1308 }
1309 
GetAnnotationForClass(Handle<mirror::Class> klass,Handle<mirror::Class> annotation_class)1310 mirror::Object* GetAnnotationForClass(Handle<mirror::Class> klass,
1311                                       Handle<mirror::Class> annotation_class) {
1312   ClassData data(klass);
1313   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1314   if (annotation_set == nullptr) {
1315     return nullptr;
1316   }
1317   return GetAnnotationObjectFromAnnotationSet(data,
1318                                               annotation_set,
1319                                               DexFile::kDexVisibilityRuntime,
1320                                               annotation_class);
1321 }
1322 
GetAnnotationsForClass(Handle<mirror::Class> klass)1323 mirror::ObjectArray<mirror::Object>* GetAnnotationsForClass(Handle<mirror::Class> klass) {
1324   ClassData data(klass);
1325   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1326   return ProcessAnnotationSet(data, annotation_set, DexFile::kDexVisibilityRuntime);
1327 }
1328 
GetDeclaredClasses(Handle<mirror::Class> klass)1329 mirror::ObjectArray<mirror::Class>* GetDeclaredClasses(Handle<mirror::Class> klass) {
1330   ClassData data(klass);
1331   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1332   if (annotation_set == nullptr) {
1333     return nullptr;
1334   }
1335   const DexFile::AnnotationItem* annotation_item =
1336       SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/MemberClasses;",
1337                           DexFile::kDexVisibilitySystem);
1338   if (annotation_item == nullptr) {
1339     return nullptr;
1340   }
1341   StackHandleScope<1> hs(Thread::Current());
1342   ObjPtr<mirror::Class> class_class = mirror::Class::GetJavaLangClass();
1343   Handle<mirror::Class> class_array_class(hs.NewHandle(
1344       Runtime::Current()->GetClassLinker()->FindArrayClass(hs.Self(), &class_class)));
1345   if (class_array_class == nullptr) {
1346     return nullptr;
1347   }
1348   mirror::Object* obj =
1349       GetAnnotationValue(data, annotation_item, "value", class_array_class,
1350                          DexFile::kDexAnnotationArray);
1351   if (obj == nullptr) {
1352     return nullptr;
1353   }
1354   return obj->AsObjectArray<mirror::Class>();
1355 }
1356 
GetDeclaringClass(Handle<mirror::Class> klass)1357 mirror::Class* GetDeclaringClass(Handle<mirror::Class> klass) {
1358   ClassData data(klass);
1359   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1360   if (annotation_set == nullptr) {
1361     return nullptr;
1362   }
1363   const DexFile::AnnotationItem* annotation_item =
1364       SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/EnclosingClass;",
1365                           DexFile::kDexVisibilitySystem);
1366   if (annotation_item == nullptr) {
1367     return nullptr;
1368   }
1369   mirror::Object* obj = GetAnnotationValue(data, annotation_item, "value",
1370                                            ScopedNullHandle<mirror::Class>(),
1371                                            DexFile::kDexAnnotationType);
1372   if (obj == nullptr) {
1373     return nullptr;
1374   }
1375   return obj->AsClass();
1376 }
1377 
GetEnclosingClass(Handle<mirror::Class> klass)1378 mirror::Class* GetEnclosingClass(Handle<mirror::Class> klass) {
1379   mirror::Class* declaring_class = GetDeclaringClass(klass);
1380   if (declaring_class != nullptr) {
1381     return declaring_class;
1382   }
1383   ClassData data(klass);
1384   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1385   if (annotation_set == nullptr) {
1386     return nullptr;
1387   }
1388   const DexFile::AnnotationItem* annotation_item =
1389       SearchAnnotationSet(data.GetDexFile(),
1390                           annotation_set,
1391                           "Ldalvik/annotation/EnclosingMethod;",
1392                           DexFile::kDexVisibilitySystem);
1393   if (annotation_item == nullptr) {
1394     return nullptr;
1395   }
1396   const uint8_t* annotation =
1397       SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1398   if (annotation == nullptr) {
1399     return nullptr;
1400   }
1401   DexFile::AnnotationValue annotation_value;
1402   if (!ProcessAnnotationValue<false>(data,
1403                                      &annotation,
1404                                      &annotation_value,
1405                                      ScopedNullHandle<mirror::Class>(),
1406                                      DexFile::kAllRaw)) {
1407     return nullptr;
1408   }
1409   if (annotation_value.type_ != DexFile::kDexAnnotationMethod) {
1410     return nullptr;
1411   }
1412   StackHandleScope<2> hs(Thread::Current());
1413   ArtMethod* method = Runtime::Current()->GetClassLinker()->ResolveMethodWithoutInvokeType(
1414       annotation_value.value_.GetI(),
1415       hs.NewHandle(data.GetDexCache()),
1416       hs.NewHandle(data.GetClassLoader()));
1417   if (method == nullptr) {
1418     return nullptr;
1419   }
1420   return method->GetDeclaringClass();
1421 }
1422 
GetEnclosingMethod(Handle<mirror::Class> klass)1423 mirror::Object* GetEnclosingMethod(Handle<mirror::Class> klass) {
1424   ClassData data(klass);
1425   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1426   if (annotation_set == nullptr) {
1427     return nullptr;
1428   }
1429   const DexFile::AnnotationItem* annotation_item =
1430       SearchAnnotationSet(data.GetDexFile(),
1431                           annotation_set,
1432                           "Ldalvik/annotation/EnclosingMethod;",
1433                           DexFile::kDexVisibilitySystem);
1434   if (annotation_item == nullptr) {
1435     return nullptr;
1436   }
1437   return GetAnnotationValue(data, annotation_item, "value", ScopedNullHandle<mirror::Class>(),
1438       DexFile::kDexAnnotationMethod);
1439 }
1440 
GetInnerClass(Handle<mirror::Class> klass,mirror::String ** name)1441 bool GetInnerClass(Handle<mirror::Class> klass, mirror::String** name) {
1442   ClassData data(klass);
1443   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1444   if (annotation_set == nullptr) {
1445     return false;
1446   }
1447   const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1448       data.GetDexFile(),
1449       annotation_set,
1450       "Ldalvik/annotation/InnerClass;",
1451       DexFile::kDexVisibilitySystem);
1452   if (annotation_item == nullptr) {
1453     return false;
1454   }
1455   const uint8_t* annotation =
1456       SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "name");
1457   if (annotation == nullptr) {
1458     return false;
1459   }
1460   DexFile::AnnotationValue annotation_value;
1461   if (!ProcessAnnotationValue<false>(data,
1462                                      &annotation,
1463                                      &annotation_value,
1464                                      ScopedNullHandle<mirror::Class>(),
1465                                      DexFile::kAllObjects)) {
1466     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 
GetInnerClassFlags(Handle<mirror::Class> klass,uint32_t * flags)1476 bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags) {
1477   ClassData data(klass);
1478   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1479   if (annotation_set == nullptr) {
1480     return false;
1481   }
1482   const DexFile::AnnotationItem* annotation_item =
1483       SearchAnnotationSet(data.GetDexFile(), annotation_set, "Ldalvik/annotation/InnerClass;",
1484                           DexFile::kDexVisibilitySystem);
1485   if (annotation_item == nullptr) {
1486     return false;
1487   }
1488   const uint8_t* annotation =
1489       SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "accessFlags");
1490   if (annotation == nullptr) {
1491     return false;
1492   }
1493   DexFile::AnnotationValue annotation_value;
1494   if (!ProcessAnnotationValue<false>(data,
1495                                      &annotation,
1496                                      &annotation_value,
1497                                      ScopedNullHandle<mirror::Class>(),
1498                                      DexFile::kAllRaw)) {
1499     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 
GetSignatureAnnotationForClass(Handle<mirror::Class> klass)1508 mirror::ObjectArray<mirror::String>* GetSignatureAnnotationForClass(Handle<mirror::Class> klass) {
1509   ClassData data(klass);
1510   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1511   if (annotation_set == nullptr) {
1512     return nullptr;
1513   }
1514   return GetSignatureValue(data, annotation_set);
1515 }
1516 
GetSourceDebugExtension(Handle<mirror::Class> klass)1517 const char* GetSourceDebugExtension(Handle<mirror::Class> klass) {
1518   // Before instantiating ClassData, check that klass has a DexCache
1519   // assigned.  The ClassData constructor indirectly dereferences it
1520   // when calling klass->GetDexFile().
1521   if (klass->GetDexCache() == nullptr) {
1522     DCHECK(klass->IsPrimitive() || klass->IsArrayClass());
1523     return nullptr;
1524   }
1525 
1526   ClassData data(klass);
1527   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1528   if (annotation_set == nullptr) {
1529     return nullptr;
1530   }
1531 
1532   const DexFile::AnnotationItem* annotation_item = SearchAnnotationSet(
1533       data.GetDexFile(),
1534       annotation_set,
1535       "Ldalvik/annotation/SourceDebugExtension;",
1536       DexFile::kDexVisibilitySystem);
1537   if (annotation_item == nullptr) {
1538     return nullptr;
1539   }
1540 
1541   const uint8_t* annotation =
1542       SearchEncodedAnnotation(data.GetDexFile(), annotation_item->annotation_, "value");
1543   if (annotation == nullptr) {
1544     return nullptr;
1545   }
1546   DexFile::AnnotationValue annotation_value;
1547   if (!ProcessAnnotationValue<false>(data,
1548                                      &annotation,
1549                                      &annotation_value,
1550                                      ScopedNullHandle<mirror::Class>(),
1551                                      DexFile::kAllRaw)) {
1552     return nullptr;
1553   }
1554   if (annotation_value.type_ != DexFile::kDexAnnotationString) {
1555     return nullptr;
1556   }
1557   dex::StringIndex index(static_cast<uint32_t>(annotation_value.value_.GetI()));
1558   return data.GetDexFile().StringDataByIdx(index);
1559 }
1560 
IsClassAnnotationPresent(Handle<mirror::Class> klass,Handle<mirror::Class> annotation_class)1561 bool IsClassAnnotationPresent(Handle<mirror::Class> klass, Handle<mirror::Class> annotation_class) {
1562   ClassData data(klass);
1563   const DexFile::AnnotationSetItem* annotation_set = FindAnnotationSetForClass(data);
1564   if (annotation_set == nullptr) {
1565     return false;
1566   }
1567   const DexFile::AnnotationItem* annotation_item = GetAnnotationItemFromAnnotationSet(
1568       data, annotation_set, DexFile::kDexVisibilityRuntime, annotation_class);
1569   return annotation_item != nullptr;
1570 }
1571 
GetLineNumFromPC(const DexFile * dex_file,ArtMethod * method,uint32_t rel_pc)1572 int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc) {
1573   // For native method, lineno should be -2 to indicate it is native. Note that
1574   // "line number == -2" is how libcore tells from StackTraceElement.
1575   if (method->GetCodeItemOffset() == 0) {
1576     return -2;
1577   }
1578 
1579   CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
1580   DCHECK(accessor.HasCodeItem()) << method->PrettyMethod() << " " << dex_file->GetLocation();
1581 
1582   // A method with no line number info should return -1
1583   DexFile::LineNumFromPcContext context(rel_pc, -1);
1584   dex_file->DecodeDebugPositionInfo(accessor.DebugInfoOffset(), DexFile::LineNumForPcCb, &context);
1585   return context.line_num_;
1586 }
1587 
1588 template<bool kTransactionActive>
ReadValueToField(ArtField * field) const1589 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField(ArtField* field) const {
1590   DCHECK(dex_cache_ != nullptr);
1591   switch (type_) {
1592     case kBoolean: field->SetBoolean<kTransactionActive>(field->GetDeclaringClass(), jval_.z);
1593         break;
1594     case kByte:    field->SetByte<kTransactionActive>(field->GetDeclaringClass(), jval_.b); break;
1595     case kShort:   field->SetShort<kTransactionActive>(field->GetDeclaringClass(), jval_.s); break;
1596     case kChar:    field->SetChar<kTransactionActive>(field->GetDeclaringClass(), jval_.c); break;
1597     case kInt:     field->SetInt<kTransactionActive>(field->GetDeclaringClass(), jval_.i); break;
1598     case kLong:    field->SetLong<kTransactionActive>(field->GetDeclaringClass(), jval_.j); break;
1599     case kFloat:   field->SetFloat<kTransactionActive>(field->GetDeclaringClass(), jval_.f); break;
1600     case kDouble:  field->SetDouble<kTransactionActive>(field->GetDeclaringClass(), jval_.d); break;
1601     case kNull:    field->SetObject<kTransactionActive>(field->GetDeclaringClass(), nullptr); break;
1602     case kString: {
1603       ObjPtr<mirror::String> resolved = linker_->ResolveString(dex::StringIndex(jval_.i),
1604                                                                dex_cache_);
1605       field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1606       break;
1607     }
1608     case kType: {
1609       ObjPtr<mirror::Class> resolved = linker_->ResolveType(dex::TypeIndex(jval_.i),
1610                                                             dex_cache_,
1611                                                             class_loader_);
1612       field->SetObject<kTransactionActive>(field->GetDeclaringClass(), resolved);
1613       break;
1614     }
1615     default: UNIMPLEMENTED(FATAL) << ": type " << type_;
1616   }
1617 }
1618 template
1619 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<true>(ArtField* field) const;
1620 template
1621 void RuntimeEncodedStaticFieldValueIterator::ReadValueToField<false>(ArtField* field) const;
1622 
1623 }  // namespace annotations
1624 
1625 }  // namespace art
1626