1 /*
2  * Copyright (C) 2018 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_CLASS_ROOT_H_
18 #define ART_RUNTIME_CLASS_ROOT_H_
19 
20 #include <stdint.h>
21 
22 #include "base/locks.h"
23 #include "read_barrier_option.h"
24 
25 namespace art {
26 
27 class ClassLinker;
28 template<class MirrorType> class ObjPtr;
29 
30 namespace mirror {
31 class ArrayElementVarHandle;
32 class ByteArrayViewVarHandle;
33 class ByteBufferViewVarHandle;
34 class CallSite;
35 class Class;
36 class ClassExt;
37 class ClassLoader;
38 class Constructor;
39 class DexCache;
40 class EmulatedStackFrame;
41 class Field;
42 class FieldVarHandle;
43 class Method;
44 class MethodHandleImpl;
45 class MethodHandlesLookup;
46 class MethodType;
47 class Object;
48 template<class T> class ObjectArray;
49 class Proxy;
50 template<typename T> class PrimitiveArray;
51 class Reference;
52 class StackTraceElement;
53 class String;
54 class Throwable;
55 class VarHandle;
56 }  // namespace mirror
57 
58 #define CLASS_MIRROR_ROOT_LIST(M)                                                                                                         \
59   M(kJavaLangClass,                         "Ljava/lang/Class;",                          mirror::Class)                                  \
60   M(kJavaLangObject,                        "Ljava/lang/Object;",                         mirror::Object)                                 \
61   M(kClassArrayClass,                       "[Ljava/lang/Class;",                         mirror::ObjectArray<mirror::Class>)             \
62   M(kObjectArrayClass,                      "[Ljava/lang/Object;",                        mirror::ObjectArray<mirror::Object>)            \
63   M(kJavaLangString,                        "Ljava/lang/String;",                         mirror::String)                                 \
64   M(kJavaLangDexCache,                      "Ljava/lang/DexCache;",                       mirror::DexCache)                               \
65   M(kJavaLangRefReference,                  "Ljava/lang/ref/Reference;",                  mirror::Reference)                              \
66   M(kJavaLangReflectConstructor,            "Ljava/lang/reflect/Constructor;",            mirror::Constructor)                            \
67   M(kJavaLangReflectField,                  "Ljava/lang/reflect/Field;",                  mirror::Field)                                  \
68   M(kJavaLangReflectMethod,                 "Ljava/lang/reflect/Method;",                 mirror::Method)                                 \
69   M(kJavaLangReflectProxy,                  "Ljava/lang/reflect/Proxy;",                  mirror::Proxy)                                  \
70   M(kJavaLangStringArrayClass,              "[Ljava/lang/String;",                        mirror::ObjectArray<mirror::String>)            \
71   M(kJavaLangReflectConstructorArrayClass,  "[Ljava/lang/reflect/Constructor;",           mirror::ObjectArray<mirror::Constructor>)       \
72   M(kJavaLangReflectFieldArrayClass,        "[Ljava/lang/reflect/Field;",                 mirror::ObjectArray<mirror::Field>)             \
73   M(kJavaLangReflectMethodArrayClass,       "[Ljava/lang/reflect/Method;",                mirror::ObjectArray<mirror::Method>)            \
74   M(kJavaLangInvokeCallSite,                "Ljava/lang/invoke/CallSite;",                mirror::CallSite)                               \
75   M(kJavaLangInvokeMethodHandle,            "Ljava/lang/invoke/MethodHandle;",            mirror::MethodHandle)                           \
76   M(kJavaLangInvokeMethodHandleImpl,        "Ljava/lang/invoke/MethodHandleImpl;",        mirror::MethodHandleImpl)                       \
77   M(kJavaLangInvokeMethodHandlesLookup,     "Ljava/lang/invoke/MethodHandles$Lookup;",    mirror::MethodHandlesLookup)                    \
78   M(kJavaLangInvokeMethodType,              "Ljava/lang/invoke/MethodType;",              mirror::MethodType)                             \
79   M(kJavaLangInvokeVarHandle,               "Ljava/lang/invoke/VarHandle;",               mirror::VarHandle)                              \
80   M(kJavaLangInvokeFieldVarHandle,          "Ljava/lang/invoke/FieldVarHandle;",          mirror::FieldVarHandle)                         \
81   M(kJavaLangInvokeArrayElementVarHandle,   "Ljava/lang/invoke/ArrayElementVarHandle;",   mirror::ArrayElementVarHandle)                  \
82   M(kJavaLangInvokeByteArrayViewVarHandle,  "Ljava/lang/invoke/ByteArrayViewVarHandle;",  mirror::ByteArrayViewVarHandle)                 \
83   M(kJavaLangInvokeByteBufferViewVarHandle, "Ljava/lang/invoke/ByteBufferViewVarHandle;", mirror::ByteBufferViewVarHandle)                \
84   M(kJavaLangClassLoader,                   "Ljava/lang/ClassLoader;",                    mirror::ClassLoader)                            \
85   M(kJavaLangThrowable,                     "Ljava/lang/Throwable;",                      mirror::Throwable)                              \
86   M(kJavaLangStackTraceElement,             "Ljava/lang/StackTraceElement;",              mirror::StackTraceElement)                      \
87   M(kDalvikSystemEmulatedStackFrame,        "Ldalvik/system/EmulatedStackFrame;",         mirror::EmulatedStackFrame)                     \
88   M(kBooleanArrayClass,                     "[Z",                                         mirror::PrimitiveArray<uint8_t>)                \
89   M(kByteArrayClass,                        "[B",                                         mirror::PrimitiveArray<int8_t>)                 \
90   M(kCharArrayClass,                        "[C",                                         mirror::PrimitiveArray<uint16_t>)               \
91   M(kDoubleArrayClass,                      "[D",                                         mirror::PrimitiveArray<double>)                 \
92   M(kFloatArrayClass,                       "[F",                                         mirror::PrimitiveArray<float>)                  \
93   M(kIntArrayClass,                         "[I",                                         mirror::PrimitiveArray<int32_t>)                \
94   M(kLongArrayClass,                        "[J",                                         mirror::PrimitiveArray<int64_t>)                \
95   M(kShortArrayClass,                       "[S",                                         mirror::PrimitiveArray<int16_t>)                \
96   M(kJavaLangStackTraceElementArrayClass,   "[Ljava/lang/StackTraceElement;",             mirror::ObjectArray<mirror::StackTraceElement>) \
97   M(kJavaLangClassLoaderArrayClass,         "[Ljava/lang/ClassLoader;",                   mirror::ObjectArray<mirror::ClassLoader>)       \
98   M(kDalvikSystemClassExt,                  "Ldalvik/system/ClassExt;",                   mirror::ClassExt)
99 
100 #define CLASS_NO_MIRROR_ROOT_LIST(M)                                                                                                                \
101   M(kJavaLangClassNotFoundException,        "Ljava/lang/ClassNotFoundException;",         detail::NoMirrorType<detail::ClassNotFoundExceptionTag>)  \
102   M(kPrimitiveBoolean,                      "Z",                                          detail::NoMirrorType<uint8_t>)                            \
103   M(kPrimitiveByte,                         "B",                                          detail::NoMirrorType<int8_t>)                             \
104   M(kPrimitiveChar,                         "C",                                          detail::NoMirrorType<uint16_t>)                           \
105   M(kPrimitiveDouble,                       "D",                                          detail::NoMirrorType<double>)                             \
106   M(kPrimitiveFloat,                        "F",                                          detail::NoMirrorType<float>)                              \
107   M(kPrimitiveInt,                          "I",                                          detail::NoMirrorType<int32_t>)                            \
108   M(kPrimitiveLong,                         "J",                                          detail::NoMirrorType<int64_t>)                            \
109   M(kPrimitiveShort,                        "S",                                          detail::NoMirrorType<int16_t>)                            \
110   M(kPrimitiveVoid,                         "V",                                          detail::NoMirrorType<void>)
111 
112 #define CLASS_ROOT_LIST(M)     \
113   CLASS_MIRROR_ROOT_LIST(M)    \
114   CLASS_NO_MIRROR_ROOT_LIST(M)
115 
116 // Well known mirror::Class roots accessed via ClassLinker::GetClassRoots().
117 enum class ClassRoot : uint32_t {
118 #define CLASS_ROOT_ENUMERATOR(name, descriptor, mirror_type) name,
119   CLASS_ROOT_LIST(CLASS_ROOT_ENUMERATOR)
120 #undef CLASS_ROOT_ENUMERATOR
121   kMax,
122 };
123 
124 const char* GetClassRootDescriptor(ClassRoot class_root);
125 
126 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
127 ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root,
128                                    ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots)
129     REQUIRES_SHARED(Locks::mutator_lock_);
130 
131 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
132 ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root, ClassLinker* linker)
133     REQUIRES_SHARED(Locks::mutator_lock_);
134 
135 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
136 ObjPtr<mirror::Class> GetClassRoot(ClassRoot class_root) REQUIRES_SHARED(Locks::mutator_lock_);
137 
138 template <class MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
139 ObjPtr<mirror::Class> GetClassRoot(ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots)
140     REQUIRES_SHARED(Locks::mutator_lock_);
141 
142 template <class MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
143 ObjPtr<mirror::Class> GetClassRoot(ClassLinker* linker) REQUIRES_SHARED(Locks::mutator_lock_);
144 
145 template <class MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
146 ObjPtr<mirror::Class> GetClassRoot() REQUIRES_SHARED(Locks::mutator_lock_);
147 
148 }  // namespace art
149 
150 #endif  // ART_RUNTIME_CLASS_ROOT_H_
151