1 /*
2  * Copyright (C) 2012 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_WELL_KNOWN_CLASSES_H_
18 #define ART_RUNTIME_WELL_KNOWN_CLASSES_H_
19 
20 #include "base/locks.h"
21 #include "base/macros.h"
22 #include "jni.h"
23 #include "obj_ptr.h"
24 #include "read_barrier_option.h"
25 
26 namespace art HIDDEN {
27 
28 class ArtField;
29 class ArtMethod;
30 
31 namespace mirror {
32 class Class;
33 }  // namespace mirror
34 
35 namespace detail {
36 
37 template <typename MemberType, MemberType** kMember>
38 struct ClassFromMember {
39   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
40   static ObjPtr<mirror::Class> Get() REQUIRES_SHARED(Locks::mutator_lock_);
41 
42   mirror::Class* operator->() const REQUIRES_SHARED(Locks::mutator_lock_);
43 };
44 
45 template <typename MemberType, MemberType** kMember>
46 bool operator==(const ClassFromMember<MemberType, kMember> lhs, ObjPtr<mirror::Class> rhs)
47     REQUIRES_SHARED(Locks::mutator_lock_);
48 
49 template <typename MemberType, MemberType** kMember>
50 bool operator==(ObjPtr<mirror::Class> lhs, const ClassFromMember<MemberType, kMember> rhs)
51     REQUIRES_SHARED(Locks::mutator_lock_);
52 
53 template <typename MemberType, MemberType** kMember>
54 bool operator!=(const ClassFromMember<MemberType, kMember> lhs, ObjPtr<mirror::Class> rhs)
55     REQUIRES_SHARED(Locks::mutator_lock_);
56 
57 template <typename MemberType, MemberType** kMember>
58 bool operator!=(ObjPtr<mirror::Class> lhs, const ClassFromMember<MemberType, kMember> rhs)
59     REQUIRES_SHARED(Locks::mutator_lock_);
60 
61 }  // namespace detail
62 
63 // Various classes used in JNI. We cache them so we don't have to keep looking them up.
64 
65 struct EXPORT WellKnownClasses {
66  public:
67   // Run before native methods are registered.
68   static void Init(JNIEnv* env);
69   // Run after native methods are registered.
70   static void LateInit(JNIEnv* env);
71 
72   static void Clear();
73 
74   static void HandleJniIdTypeChange(JNIEnv* env);
75 
76   static void InitStringInit(ObjPtr<mirror::Class> string_class,
77                              ObjPtr<mirror::Class> string_builder_class)
78       REQUIRES_SHARED(Locks::mutator_lock_);
79   static ArtMethod* StringInitToStringFactory(ArtMethod* method);
80   static uint32_t StringInitToEntryPoint(ArtMethod* method);
81 
82   static ObjPtr<mirror::Class> ToClass(jclass global_jclass) REQUIRES_SHARED(Locks::mutator_lock_);
83 
84  private:
85   static void InitFieldsAndMethodsOnly(JNIEnv* env);
86 
87   template <ArtMethod** kMethod>
88   using ClassFromMethod = detail::ClassFromMember<ArtMethod, kMethod>;
89 
90   template <ArtField** kField>
91   using ClassFromField = detail::ClassFromMember<ArtField, kField>;
92 
93  public:
94   static jclass dalvik_annotation_optimization_CriticalNative;
95   static jclass dalvik_annotation_optimization_FastNative;
96   static jclass dalvik_annotation_optimization_NeverCompile;
97   static jclass dalvik_annotation_optimization_NeverInline;
98   static jclass java_lang_annotation_Annotation__array;
99   static jclass java_lang_ClassValue;
100   static jclass java_lang_Record;
101   static jclass java_lang_reflect_Parameter__array;
102   static jclass java_lang_StringFactory;
103   static jclass java_lang_System;
104   static jclass java_lang_Void;
105   static jclass libcore_reflect_AnnotationMember__array;
106 
107   static ArtMethod* dalvik_system_BaseDexClassLoader_getLdLibraryPath;
108   static ArtMethod* dalvik_system_DelegateLastClassLoader_init;  // Only for the declaring class.
109   static ArtMethod* dalvik_system_DexClassLoader_init;  // Only for the declaring class.
110   static ArtMethod* dalvik_system_InMemoryDexClassLoader_init;  // Only for the declaring class.
111   static ArtMethod* dalvik_system_PathClassLoader_init;  // Only for the declaring class.
112   static ArtMethod* dalvik_system_VMRuntime_hiddenApiUsed;
113   static ArtMethod* java_lang_Boolean_valueOf;
114   static ArtMethod* java_lang_BootClassLoader_init;  // Only for the declaring class.
115   static ArtMethod* java_lang_Byte_valueOf;
116   static ArtMethod* java_lang_Character_valueOf;
117   static ArtMethod* java_lang_ClassLoader_loadClass;
118   static ArtMethod* java_lang_ClassNotFoundException_init;
119   static ArtMethod* java_lang_Daemons_start;
120   static ArtMethod* java_lang_Daemons_stop;
121   static ArtMethod* java_lang_Daemons_waitForDaemonStart;
122   static ArtMethod* java_lang_Double_doubleToRawLongBits;
123   static ArtMethod* java_lang_Double_valueOf;
124   static ArtMethod* java_lang_Error_init;  // Only for the declaring class.
125   static ArtMethod* java_lang_Float_floatToRawIntBits;
126   static ArtMethod* java_lang_Float_valueOf;
127   static ArtMethod* java_lang_IllegalAccessError_init;  // Only for the declaring class.
128   static ArtMethod* java_lang_Integer_valueOf;
129   static ArtMethod* java_lang_Long_valueOf;
130   static ArtMethod* java_lang_NoClassDefFoundError_init;  // Only for the declaring class.
131   static ArtMethod* java_lang_OutOfMemoryError_init;  // Only for the declaring class.
132   static ArtMethod* java_lang_Runtime_nativeLoad;
133   static ArtMethod* java_lang_RuntimeException_init;  // Only for the declaring class.
134   static ArtMethod* java_lang_Short_valueOf;
135   static ArtMethod* java_lang_StackOverflowError_init;  // Only for the declaring class.
136   static ArtMethod* java_lang_String_charAt;
137   static ArtMethod* java_lang_Thread_dispatchUncaughtException;
138   static ArtMethod* java_lang_Thread_init;
139   static ArtMethod* java_lang_Thread_run;
140   static ArtMethod* java_lang_ThreadGroup_add;
141   static ArtMethod* java_lang_ThreadGroup_threadTerminated;
142   static ArtMethod* java_lang_invoke_MethodHandle_asType;
143   static ArtMethod* java_lang_invoke_MethodHandle_invokeExact;
144   static ArtMethod* java_lang_invoke_MethodHandles_lookup;
145   static ArtMethod* java_lang_invoke_MethodHandles_makeIdentity;
146   static ArtMethod* java_lang_invoke_MethodHandles_Lookup_findConstructor;
147   static ArtMethod* java_lang_invoke_MethodType_makeImpl;
148   static ArtMethod* java_lang_ref_FinalizerReference_add;
149   static ArtMethod* java_lang_ref_ReferenceQueue_add;
150   static ArtMethod* java_lang_reflect_InvocationTargetException_init;
151   static ArtMethod* java_lang_reflect_Parameter_init;
152   static ArtMethod* java_lang_reflect_Proxy_init;
153   static ArtMethod* java_lang_reflect_Proxy_invoke;
154   static ArtMethod* java_nio_Buffer_isDirect;
155   static ArtMethod* java_nio_DirectByteBuffer_init;
156   static ArtMethod* java_util_function_Consumer_accept;
157   static ArtMethod* jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D;
158   static ArtMethod* jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_F;
159   static ArtMethod* jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_getChars;
160   static ArtMethod* libcore_reflect_AnnotationFactory_createAnnotation;
161   static ArtMethod* libcore_reflect_AnnotationMember_init;
162   static ArtMethod* org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
163   static ArtMethod* org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
164 
165   static ArtField* dalvik_system_BaseDexClassLoader_pathList;
166   static ArtField* dalvik_system_BaseDexClassLoader_sharedLibraryLoaders;
167   static ArtField* dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter;
168   static ArtField* dalvik_system_DexFile_cookie;
169   static ArtField* dalvik_system_DexFile_fileName;
170   static ArtField* dalvik_system_DexPathList_dexElements;
171   static ArtField* dalvik_system_DexPathList__Element_dexFile;
172   static ArtField* dalvik_system_VMRuntime_nonSdkApiUsageConsumer;
173   static ArtField* java_io_FileDescriptor_descriptor;
174   static ArtField* java_lang_ClassLoader_parent;
175   static ArtField* java_lang_String_EMPTY;
176   static ArtField* java_lang_Thread_parkBlocker;
177   static ArtField* java_lang_Thread_daemon;
178   static ArtField* java_lang_Thread_group;
179   static ArtField* java_lang_Thread_lock;
180   static ArtField* java_lang_Thread_name;
181   static ArtField* java_lang_Thread_priority;
182   static ArtField* java_lang_Thread_nativePeer;
183   static ArtField* java_lang_Thread_systemDaemon;
184   static ArtField* java_lang_Thread_unparkedBeforeStart;
185   static ArtField* java_lang_ThreadGroup_groups;
186   static ArtField* java_lang_ThreadGroup_ngroups;
187   static ArtField* java_lang_ThreadGroup_mainThreadGroup;
188   static ArtField* java_lang_ThreadGroup_name;
189   static ArtField* java_lang_ThreadGroup_parent;
190   static ArtField* java_lang_ThreadGroup_systemThreadGroup;
191   static ArtField* java_lang_Throwable_cause;
192   static ArtField* java_lang_Throwable_detailMessage;
193   static ArtField* java_lang_Throwable_stackTrace;
194   static ArtField* java_lang_Throwable_stackState;
195   static ArtField* java_lang_Throwable_suppressedExceptions;
196   static ArtField* java_nio_Buffer_address;
197   static ArtField* java_nio_Buffer_capacity;
198   static ArtField* java_nio_Buffer_elementSizeShift;
199   static ArtField* java_nio_Buffer_limit;
200   static ArtField* java_nio_Buffer_position;
201   static ArtField* java_nio_ByteBuffer_hb;
202   static ArtField* java_nio_ByteBuffer_isReadOnly;
203   static ArtField* java_nio_ByteBuffer_offset;
204   static ArtField* java_util_Collections_EMPTY_LIST;
205   static ArtField* java_util_concurrent_ThreadLocalRandom_seeder;
206   static ArtField* jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_buffer;
207   static ArtField* jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer_image;
208   static ArtField* libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
209   static ArtField* org_apache_harmony_dalvik_ddmc_Chunk_data;
210   static ArtField* org_apache_harmony_dalvik_ddmc_Chunk_length;
211   static ArtField* org_apache_harmony_dalvik_ddmc_Chunk_offset;
212   static ArtField* org_apache_harmony_dalvik_ddmc_Chunk_type;
213 
214   static ArtField* java_lang_Byte_ByteCache_cache;
215   static ArtField* java_lang_Character_CharacterCache_cache;
216   static ArtField* java_lang_Short_ShortCache_cache;
217   static ArtField* java_lang_Integer_IntegerCache_cache;
218   static ArtField* java_lang_Long_LongCache_cache;
219 
220   static ArtField* java_lang_Byte_value;
221   static ArtField* java_lang_Character_value;
222   static ArtField* java_lang_Short_value;
223   static ArtField* java_lang_Integer_value;
224   static ArtField* java_lang_Long_value;
225 
226   static constexpr ClassFromField<&dalvik_system_BaseDexClassLoader_pathList>
227       dalvik_system_BaseDexClassLoader;
228   static constexpr ClassFromMethod<&dalvik_system_DelegateLastClassLoader_init>
229       dalvik_system_DelegateLastClassLoader;
230   static constexpr ClassFromMethod<&dalvik_system_DexClassLoader_init>
231       dalvik_system_DexClassLoader;
232   static constexpr ClassFromField<&dalvik_system_DexFile_cookie> dalvik_system_DexFile;
233   static constexpr ClassFromField<&dalvik_system_DexPathList_dexElements> dalvik_system_DexPathList;
234   static constexpr ClassFromField<&dalvik_system_DexPathList__Element_dexFile>
235       dalvik_system_DexPathList__Element;
236   static constexpr ClassFromMethod<&dalvik_system_InMemoryDexClassLoader_init>
237       dalvik_system_InMemoryDexClassLoader;
238   static constexpr ClassFromMethod<&dalvik_system_PathClassLoader_init>
239       dalvik_system_PathClassLoader;
240   static constexpr ClassFromMethod<&java_lang_BootClassLoader_init> java_lang_BootClassLoader;
241   static constexpr ClassFromField<&java_lang_ClassLoader_parent> java_lang_ClassLoader;
242   static constexpr ClassFromMethod<&java_lang_Daemons_start> java_lang_Daemons;
243   static constexpr ClassFromMethod<&java_lang_Error_init> java_lang_Error;
244   static constexpr ClassFromMethod<&java_lang_IllegalAccessError_init>
245       java_lang_IllegalAccessError;
246   static constexpr ClassFromMethod<&java_lang_NoClassDefFoundError_init>
247       java_lang_NoClassDefFoundError;
248   static constexpr ClassFromMethod<&java_lang_OutOfMemoryError_init> java_lang_OutOfMemoryError;
249   static constexpr ClassFromMethod<&java_lang_RuntimeException_init> java_lang_RuntimeException;
250   static constexpr ClassFromMethod<&java_lang_StackOverflowError_init>
251       java_lang_StackOverflowError;
252   static constexpr ClassFromField<&java_lang_Thread_daemon> java_lang_Thread;
253   static constexpr ClassFromField<&java_lang_ThreadGroup_groups> java_lang_ThreadGroup;
254   static constexpr ClassFromMethod<&java_lang_reflect_InvocationTargetException_init>
255       java_lang_reflect_InvocationTargetException;
256   static constexpr ClassFromMethod<&java_lang_reflect_Parameter_init>
257       java_lang_reflect_Parameter;
258   static constexpr ClassFromField<&java_nio_Buffer_address> java_nio_Buffer;
259   static constexpr ClassFromField<&java_util_Collections_EMPTY_LIST> java_util_Collections;
260   static constexpr ClassFromField<&libcore_util_EmptyArray_STACK_TRACE_ELEMENT>
261       libcore_util_EmptyArray;
262 
263   static constexpr ClassFromField<&java_lang_Byte_ByteCache_cache> java_lang_Byte_ByteCache;
264   static constexpr ClassFromField<&java_lang_Character_CharacterCache_cache>
265       java_lang_Character_CharacterCache;
266   static constexpr ClassFromField<&java_lang_Short_ShortCache_cache> java_lang_Short_ShortCache;
267   static constexpr ClassFromField<&java_lang_Integer_IntegerCache_cache>
268       java_lang_Integer_IntegerCache;
269   static constexpr ClassFromField<&java_lang_Long_LongCache_cache> java_lang_Long_LongCache;
270 
271   static constexpr ClassFromMethod<&java_lang_Boolean_valueOf> java_lang_Boolean;
272   static constexpr ClassFromMethod<&java_lang_Byte_valueOf> java_lang_Byte;
273   static constexpr ClassFromMethod<&java_lang_Character_valueOf> java_lang_Character;
274   static constexpr ClassFromMethod<&java_lang_Short_valueOf> java_lang_Short;
275   static constexpr ClassFromMethod<&java_lang_Integer_valueOf> java_lang_Integer;
276   static constexpr ClassFromMethod<&java_lang_Float_valueOf> java_lang_Float;
277   static constexpr ClassFromMethod<&java_lang_Long_valueOf> java_lang_Long;
278   static constexpr ClassFromMethod<&java_lang_Double_valueOf> java_lang_Double;
279 };
280 
281 }  // namespace art
282 
283 #endif  // ART_RUNTIME_WELL_KNOWN_CLASSES_H_
284