blob: 75951a7cefb4547f098f49622e4874724ccfc1af [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
David Sehr9e734c72018-01-04 17:56:19 -080017#ifndef ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_
18#define ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_
David Sehr9323e6e2016-09-13 08:58:35 -070019
David Sehr334b9d72018-02-12 18:27:56 -080020#include "dex/dex_file.h"
Vladimir Markoe11dd502017-12-08 14:09:45 +000021#include "handle.h"
22#include "mirror/dex_cache.h"
David Sehr9323e6e2016-09-13 08:58:35 -070023#include "mirror/object_array.h"
Vladimir Marko2d3065e2018-05-22 13:56:09 +010024#include "obj_ptr.h"
David Sehr9323e6e2016-09-13 08:58:35 -070025
26namespace art {
27
28namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080029class ClassLoader;
David Sehr9323e6e2016-09-13 08:58:35 -070030} // namespace mirror
31class ArtField;
32class ArtMethod;
33class ClassLinker;
34
35namespace annotations {
36
37// Field annotations.
Vladimir Marko2d3065e2018-05-22 13:56:09 +010038ObjPtr<mirror::Object> GetAnnotationForField(ArtField* field,
39 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -070040 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010041ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForField(ArtField* field)
David Sehr9323e6e2016-09-13 08:58:35 -070042 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoacb906d2018-05-30 10:23:49 +010043ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForField(ArtField* field)
David Sehr9323e6e2016-09-13 08:58:35 -070044 REQUIRES_SHARED(Locks::mutator_lock_);
45bool IsFieldAnnotationPresent(ArtField* field, Handle<mirror::Class> annotation_class)
46 REQUIRES_SHARED(Locks::mutator_lock_);
47
48// Method annotations.
Vladimir Marko2d3065e2018-05-22 13:56:09 +010049ObjPtr<mirror::Object> GetAnnotationDefaultValue(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -070050 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010051ObjPtr<mirror::Object> GetAnnotationForMethod(ArtMethod* method,
52 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -070053 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010054ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -070055 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010056ObjPtr<mirror::ObjectArray<mirror::Class>> GetExceptionTypesForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -070057 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010058ObjPtr<mirror::ObjectArray<mirror::Object>> GetParameterAnnotations(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -070059 REQUIRES_SHARED(Locks::mutator_lock_);
Orion Hodson58143d22018-02-20 08:44:20 +000060uint32_t GetNumberOfAnnotatedMethodParameters(ArtMethod* method)
61 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +010062ObjPtr<mirror::Object> GetAnnotationForMethodParameter(ArtMethod* method,
63 uint32_t parameter_idx,
64 Handle<mirror::Class> annotation_class)
David Sehr9323e6e2016-09-13 08:58:35 -070065 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoacb906d2018-05-30 10:23:49 +010066bool GetParametersMetadataForMethod(
67 ArtMethod* method,
68 /*out*/ MutableHandle<mirror::ObjectArray<mirror::String>>* names,
69 /*out*/ MutableHandle<mirror::IntArray>* access_flags) REQUIRES_SHARED(Locks::mutator_lock_);
70ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForMethod(ArtMethod* method)
David Sehr9323e6e2016-09-13 08:58:35 -070071 REQUIRES_SHARED(Locks::mutator_lock_);
Roland Levillain35e42f02017-06-26 18:14:39 +010072// 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.
77bool IsMethodAnnotationPresent(ArtMethod* method,
78 Handle<mirror::Class> annotation_class,
Vladimir Marko0db16e02017-11-08 14:32:33 +000079 uint32_t visibility = DexFile::kDexVisibilityRuntime)
David Sehr9323e6e2016-09-13 08:58:35 -070080 REQUIRES_SHARED(Locks::mutator_lock_);
Hans Boehm206348c2018-12-05 11:11:33 -080081
Vladimir Marko0db16e02017-11-08 14:32:33 +000082// Check whether a method from the `dex_file` with the given `method_index`
Vladimir Markob0a6aee2017-10-27 10:34:04 +010083// 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.
86uint32_t GetNativeMethodAnnotationAccessFlags(const DexFile& dex_file,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080087 const dex::ClassDef& class_def,
Vladimir Markob0a6aee2017-10-27 10:34:04 +010088 uint32_t method_index);
Kevin Jeon55737562021-11-19 20:39:30 +000089// Is the method from the `dex_file` with the given `field_index`
90// annotated with @dalvik.annotation.optimization.NeverCompile?
91bool MethodIsNeverCompile(const DexFile& dex_file,
92 const dex::ClassDef& class_def,
93 uint32_t method_index);
Hans Boehm206348c2018-12-05 11:11:33 -080094// Is the field from the `dex_file` with the given `field_index`
95// annotated with @dalvik.annotation.optimization.ReachabilitySensitive?
96bool FieldIsReachabilitySensitive(const DexFile& dex_file,
97 const dex::ClassDef& class_def,
98 uint32_t field_index);
99// Is the method from the `dex_file` with the given `method_index`
100// annotated with @dalvik.annotation.optimization.ReachabilitySensitive?
101bool MethodIsReachabilitySensitive(const DexFile& dex_file,
102 const dex::ClassDef& class_def,
103 uint32_t method_index);
104// Does the method from the `dex_file` with the given `method_index` contain an access to a field
105// annotated with @dalvik.annotation.optimization.ReachabilitySensitive, or a call to a method
106// with that annotation?
107// Class_def is the class defining the method. We consider only accessses to classes or methods
108// declared in the static type of the corresponding object. We may overlook accesses to annotated
109// fields or methods that are in neither class_def nor a containing (outer) class.
110bool MethodContainsRSensitiveAccess(const DexFile& dex_file,
111 const dex::ClassDef& class_def,
112 uint32_t method_index);
113// Is the given class annotated with @dalvik.annotation.optimization.DeadReferenceSafe?
114bool HasDeadReferenceSafeAnnotation(const DexFile& dex_file,
115 const dex::ClassDef& class_def);
David Sehr9323e6e2016-09-13 08:58:35 -0700116
117// Class annotations.
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100118ObjPtr<mirror::Object> GetAnnotationForClass(Handle<mirror::Class> klass,
David Sehr9323e6e2016-09-13 08:58:35 -0700119 Handle<mirror::Class> annotation_class)
120 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100121ObjPtr<mirror::ObjectArray<mirror::Object>> GetAnnotationsForClass(Handle<mirror::Class> klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700122 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100123ObjPtr<mirror::ObjectArray<mirror::Class>> GetDeclaredClasses(Handle<mirror::Class> klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700124 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100125ObjPtr<mirror::Class> GetDeclaringClass(Handle<mirror::Class> klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700126 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100127ObjPtr<mirror::Class> GetEnclosingClass(Handle<mirror::Class> klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700128 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100129ObjPtr<mirror::Object> GetEnclosingMethod(Handle<mirror::Class> klass)
David Sehr9323e6e2016-09-13 08:58:35 -0700130 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoacb906d2018-05-30 10:23:49 +0100131bool GetInnerClass(Handle<mirror::Class> klass, /*out*/ ObjPtr<mirror::String>* name)
David Sehr9323e6e2016-09-13 08:58:35 -0700132 REQUIRES_SHARED(Locks::mutator_lock_);
133bool GetInnerClassFlags(Handle<mirror::Class> klass, uint32_t* flags)
134 REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Markoacb906d2018-05-30 10:23:49 +0100135ObjPtr<mirror::ObjectArray<mirror::String>> GetSignatureAnnotationForClass(
136 Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_);
Orion Hodson77d8a1c2017-04-24 14:53:19 +0100137const char* GetSourceDebugExtension(Handle<mirror::Class> klass)
138 REQUIRES_SHARED(Locks::mutator_lock_);
Sorin Basca08b3f872022-05-31 17:04:55 +0100139ObjPtr<mirror::Class> GetNestHost(Handle<mirror::Class> klass)
140 REQUIRES_SHARED(Locks::mutator_lock_);
141ObjPtr<mirror::ObjectArray<mirror::Class>> GetNestMembers(Handle<mirror::Class> klass)
142 REQUIRES_SHARED(Locks::mutator_lock_);
Sorin Bascaa0191bc2022-06-15 16:53:48 +0100143ObjPtr<mirror::ObjectArray<mirror::Class>> GetPermittedSubclasses(Handle<mirror::Class> klass)
144 REQUIRES_SHARED(Locks::mutator_lock_);
David Sehr9323e6e2016-09-13 08:58:35 -0700145bool IsClassAnnotationPresent(Handle<mirror::Class> klass,
146 Handle<mirror::Class> annotation_class)
147 REQUIRES_SHARED(Locks::mutator_lock_);
148
149// Map back from a PC to the line number in a method.
150int32_t GetLineNumFromPC(const DexFile* dex_file, ArtMethod* method, uint32_t rel_pc)
151 REQUIRES_SHARED(Locks::mutator_lock_);
152
153// Annotations iterator.
154class RuntimeEncodedStaticFieldValueIterator : public EncodedStaticFieldValueIterator {
155 public:
156 // A constructor meant to be called from runtime code.
Vladimir Markoe11dd502017-12-08 14:09:45 +0000157 RuntimeEncodedStaticFieldValueIterator(Handle<mirror::DexCache> dex_cache,
158 Handle<mirror::ClassLoader> class_loader,
David Sehr9323e6e2016-09-13 08:58:35 -0700159 ClassLinker* linker,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800160 const dex::ClassDef& class_def)
David Sehr9323e6e2016-09-13 08:58:35 -0700161 REQUIRES_SHARED(Locks::mutator_lock_)
Vladimir Markoe11dd502017-12-08 14:09:45 +0000162 : EncodedStaticFieldValueIterator(*dex_cache->GetDexFile(), class_def),
David Sehr9323e6e2016-09-13 08:58:35 -0700163 dex_cache_(dex_cache),
164 class_loader_(class_loader),
165 linker_(linker) {
166 }
167
168 template<bool kTransactionActive>
169 void ReadValueToField(ArtField* field) const REQUIRES_SHARED(Locks::mutator_lock_);
170
171 private:
Vladimir Markoe11dd502017-12-08 14:09:45 +0000172 const Handle<mirror::DexCache> dex_cache_; // Dex cache to resolve literal objects.
173 const Handle<mirror::ClassLoader> class_loader_; // ClassLoader to resolve types.
174 ClassLinker* const linker_; // Linker to resolve literal objects.
David Sehr9323e6e2016-09-13 08:58:35 -0700175 DISALLOW_IMPLICIT_CONSTRUCTORS(RuntimeEncodedStaticFieldValueIterator);
176};
177
178} // namespace annotations
179
180} // namespace art
181
David Sehr9e734c72018-01-04 17:56:19 -0800182#endif // ART_RUNTIME_DEX_DEX_FILE_ANNOTATIONS_H_