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 #ifndef ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_
18 #define ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_
19 
20 #include "dex/dex_file.h"
21 #include "handle.h"
22 #include "mirror/dex_cache.h"
23 #include "mirror/object_array.h"
24 #include "obj_ptr.h"
25 
26 namespace art HIDDEN {
27 
28 namespace mirror {
29 class ClassLoader;
30 }  // namespace mirror
31 class ArtField;
32 class ArtMethod;
33 class ClassLinker;
34 
35 namespace annotations {
36 
37 // Field annotations.
38 ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
39                                              Handle<mirror::Class> annotation_class)
40     REQUIRES_SHARED(Locks::mutator_lock_);
41 ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field)
42     REQUIRES_SHARED(Locks::mutator_lock_);
43 EXPORT ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field)
44     REQUIRES_SHARED(Locks::mutator_lock_);
45 bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class)
46     REQUIRES_SHARED(Locks::mutator_lock_);
47 
48 // Method annotations.
49 ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method)
50     REQUIRES_SHARED(Locks::mutator_lock_);
51 ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
52                                               Handle<mirror::Class> annotation_class)
53     REQUIRES_SHARED(Locks::mutator_lock_);
54 ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method)
55     REQUIRES_SHARED(Locks::mutator_lock_);
56 ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method)
57     REQUIRES_SHARED(Locks::mutator_lock_);
58 ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method)
59     REQUIRES_SHARED(Locks::mutator_lock_);
60 uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method)
61     REQUIRES_SHARED(Locks::mutator_lock_);
62 ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
63                                                        uint32_t parameter_idx,
64                                                        Handle<mirror::Class> annotation_class)
65     REQUIRES_SHARED(Locks::mutator_lock_);
66 bool GetParametersMetadataForMethod(
67     ArtMethod* method,
68     /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
69     /*out*/ MutableHandle<mirror::IntArray>* access_flags) REQUIRES_SHARED(Locks::mutator_lock_);
70 EXPORT ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(
71     ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
72 // Check whether `method` is annotated with `annotation_class`.
73 // If `lookup_in_resolved_boot_classes` is true, look up any of the
74 // method's annotations' classes in the bootstrap class loader's
75 // resolved types; if it is false (default value), resolve them as a
76 // side effect.
77 bool IsMethodAnnotationPresent(ArtMethod* method,
78                                Handle<mirror::Class> annotation_class,
79                                uint32_t visibility = DexFile::kDexVisibilityRuntime)
80     REQUIRES_SHARED(Locks::mutator_lock_);
81 
82 // Check whether a method from the `dex_file` with the given `method_index`
83 // is annotated with @dalvik.annotation.optimization.FastNative or
84 // @dalvik.annotation.optimization.CriticalNative with build visibility.
85 // If yes, return the associated access flags, i.e. kAccFastNative or kAccCriticalNative.
86 EXPORT uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
87                                                      const dex::ClassDef& class_def,
88                                                      uint32_t method_index);
89 // An overload of `GetNativeMethodAnnotationAccessFlags()` that takes a `MethodAnnotationsItem`.
90 uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
91                                               const dex::MethodAnnotationsItem& method_annotations);
92 // Is the method from the `dex_file` with the given `field_index`
93 // annotated with @dalvik.annotation.optimization.NeverCompile?
94 EXPORT bool MethodIsNeverCompile(const DexFile& dex_file,
95                                  const dex::ClassDef& class_def,
96                                  uint32_t method_index);
97 // An overload of `MethodIsNeverCompile()` that takes a `MethodAnnotationsItem`.
98 bool MethodIsNeverCompile(const DexFile& dex_file,
99                           const dex::MethodAnnotationsItem& method_annotations);
100 // Is the method from the `dex_file` with the given `field_index`
101 // annotated with @dalvik.annotation.optimization.NeverInline?
102 bool MethodIsNeverInline(const DexFile& dex_file,
103                          const dex::ClassDef& class_def,
104                          uint32_t method_index);
105 // Is the field from the `dex_file` with the given `field_index`
106 // annotated with @dalvik.annotation.optimization.ReachabilitySensitive?
107 bool FieldIsReachabilitySensitive(const DexFile& dex_file,
108                                   const dex::ClassDef& class_def,
109                                   uint32_t field_index);
110 // Is the method from the `dex_file` with the given `method_index`
111 // annotated with @dalvik.annotation.optimization.ReachabilitySensitive?
112 bool MethodIsReachabilitySensitive(const DexFile& dex_file,
113                                    const dex::ClassDef& class_def,
114                                    uint32_t method_index);
115 // Does the method from the `dex_file` with the given `method_index` contain an access to a field
116 // annotated with @dalvik.annotation.optimization.ReachabilitySensitive, or a call to a method
117 // with that annotation?
118 // Class_def is the class defining the method. We consider only accessses to classes or methods
119 // declared in the static type of the corresponding object. We may overlook accesses to annotated
120 // fields or methods that are in neither class_def nor a containing (outer) class.
121 bool MethodContainsRSensitiveAccess(const DexFile& dex_file,
122                                     const dex::ClassDef& class_def,
123                                     uint32_t method_index);
124 // Is the given class annotated with @dalvik.annotation.optimization.DeadReferenceSafe?
125 bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file,
126                                     const dex::ClassDef& class_def);
127 
128 // Class annotations.
129 ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
130                                       Handle<mirror::Class> annotation_class)
131     REQUIRES_SHARED(Locks::mutator_lock_);
132 ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass)
133     REQUIRES_SHARED(Locks::mutator_lock_);
134 ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass)
135     REQUIRES_SHARED(Locks::mutator_lock_);
136 ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass)
137     REQUIRES_SHARED(Locks::mutator_lock_);
138 ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass)
139     REQUIRES_SHARED(Locks::mutator_lock_);
140 ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass)
141     REQUIRES_SHARED(Locks::mutator_lock_);
142 bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name)
143     REQUIRES_SHARED(Locks::mutator_lock_);
144 bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags)
145     REQUIRES_SHARED(Locks::mutator_lock_);
146 EXPORT ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
147     Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
148 EXPORT const char* GetSourceDebugExtension(Handle<mirror::Class> klass)
149     REQUIRES_SHARED(Locks::mutator_lock_);
150 ObjPtr<mirror::Class> GetNestHost(Handle<mirror::Class> klass)
151     REQUIRES_SHARED(Locks::mutator_lock_);
152 ObjPtr<mirror::ObjectArray<mirror::Class>> GetNestMembers(Handle<mirror::Class> klass)
153     REQUIRES_SHARED(Locks::mutator_lock_);
154 ObjPtr<mirror::Object> getRecordAnnotationElement(Handle<mirror::Class> klass,
155                                                   Handle<mirror::Class> array_class,
156                                                   const char* element_name)
157     REQUIRES_SHARED(Locks::mutator_lock_);
158 ObjPtr<mirror::ObjectArray<mirror::Class>> GetPermittedSubclasses(Handle<mirror::Class> klass)
159     REQUIRES_SHARED(Locks::mutator_lock_);
160 bool IsClassAnnotationPresent(Handle<mirror::Class> klass,
161                               Handle<mirror::Class> annotation_class)
162     REQUIRES_SHARED(Locks::mutator_lock_);
163 
164 // Map back from a PC to the line number in a method.
165 int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc)
166     REQUIRES_SHARED(Locks::mutator_lock_);
167 
168 // Annotations iterator.
169 class RuntimeEncodedStaticFieldValueIterator : public EncodedStaticFieldValueIterator {
170  public:
171   // A constructor meant to be called from runtime code.
RuntimeEncodedStaticFieldValueIterator(Handle<mirror::DexCache> dex_cache,Handle<mirror::ClassLoader> class_loader,ClassLinker * linker,const dex::ClassDef & class_def)172   RuntimeEncodedStaticFieldValueIterator(Handle<mirror::DexCache> dex_cache,
173                                          Handle<mirror::ClassLoader> class_loader,
174                                          ClassLinker* linker,
175                                          const dex::ClassDef& class_def)
176       REQUIRES_SHARED(Locks::mutator_lock_)
177       : EncodedStaticFieldValueIterator(*dex_cache->GetDexFile(), class_def),
178         dex_cache_(dex_cache),
179         class_loader_(class_loader),
180         linker_(linker) {
181   }
182 
183   template<bool kTransactionActive>
184   void ReadValueToField(ArtField* field) const REQUIRES_SHARED(Locks::mutator_lock_);
185 
186  private:
187   const Handle<mirror::DexCache> dex_cache_;  // Dex cache to resolve literal objects.
188   const Handle<mirror::ClassLoader> class_loader_;  // ClassLoader to resolve types.
189   ClassLinker* const linker_;  // Linker to resolve literal objects.
190   DISALLOW_IMPLICIT_CONSTRUCTORS(RuntimeEncodedStaticFieldValueIterator);
191 };
192 
193 enum class VisitorStatus : uint8_t { kVisitBreak, kVisitNext, kVisitInner };
194 
195 class AnnotationVisitor {
196  public:
~AnnotationVisitor()197   virtual ~AnnotationVisitor() {}
198   virtual VisitorStatus VisitAnnotation(const char* annotation_descriptor, uint8_t visibility) = 0;
199   virtual VisitorStatus VisitAnnotationElement(const char* element_name,
200                                                uint8_t type,
201                                                const JValue& value) = 0;
202   virtual VisitorStatus VisitArrayElement(uint8_t depth,
203                                           uint32_t index,
204                                           uint8_t type,
205                                           const JValue& value) = 0;
206 };
207 
208 // Visit all annotation elements and array elements without creating
209 // Arrays or Objects in the managed heap.
210 void VisitClassAnnotations(Handle<mirror::Class> klass, AnnotationVisitor* visitor)
211     REQUIRES_SHARED(Locks::mutator_lock_);
212 
213 }  // namespace annotations
214 
215 }  // namespace art
216 
217 #endif  // ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_
218