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 #include "well_known_classes.h"
18 
19 #include <stdlib.h>
20 
21 #include <sstream>
22 
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25 
26 #include "art_method-inl.h"
27 #include "base/casts.h"
28 #include "base/pointer_size.h"
29 #include "class_linker.h"
30 #include "class_root-inl.h"
31 #include "entrypoints/quick/quick_entrypoints_enum.h"
32 #include "handle_scope-inl.h"
33 #include "hidden_api.h"
34 #include "jni/java_vm_ext.h"
35 #include "jni/jni_internal.h"
36 #include "jni_id_type.h"
37 #include "mirror/class.h"
38 #include "mirror/throwable.h"
39 #include "nativehelper/scoped_local_ref.h"
40 #include "obj_ptr-inl.h"
41 #include "runtime.h"
42 #include "scoped_thread_state_change-inl.h"
43 #include "scoped_thread_state_change.h"
44 #include "thread-current-inl.h"
45 
46 namespace art HIDDEN {
47 
48 jclass WellKnownClasses::dalvik_annotation_optimization_CriticalNative;
49 jclass WellKnownClasses::dalvik_annotation_optimization_FastNative;
50 jclass WellKnownClasses::dalvik_annotation_optimization_NeverCompile;
51 jclass WellKnownClasses::dalvik_annotation_optimization_NeverInline;
52 jclass WellKnownClasses::java_lang_annotation_Annotation__array;
53 jclass WellKnownClasses::java_lang_ClassValue;
54 jclass WellKnownClasses::java_lang_Record;
55 jclass WellKnownClasses::java_lang_reflect_Parameter__array;
56 jclass WellKnownClasses::java_lang_StringFactory;
57 jclass WellKnownClasses::java_lang_System;
58 jclass WellKnownClasses::java_lang_Void;
59 jclass WellKnownClasses::libcore_reflect_AnnotationMember__array;
60 
61 ArtMethod* WellKnownClasses::dalvik_system_BaseDexClassLoader_getLdLibraryPath;
62 ArtMethod* WellKnownClasses::dalvik_system_DelegateLastClassLoader_init;
63 ArtMethod* WellKnownClasses::dalvik_system_DexClassLoader_init;
64 ArtMethod* WellKnownClasses::dalvik_system_InMemoryDexClassLoader_init;
65 ArtMethod* WellKnownClasses::dalvik_system_PathClassLoader_init;
66 ArtMethod* WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed;
67 ArtMethod* WellKnownClasses::java_lang_Boolean_valueOf;
68 ArtMethod* WellKnownClasses::java_lang_BootClassLoader_init;
69 ArtMethod* WellKnownClasses::java_lang_Byte_valueOf;
70 ArtMethod* WellKnownClasses::java_lang_Character_valueOf;
71 ArtMethod* WellKnownClasses::java_lang_ClassLoader_loadClass;
72 ArtMethod* WellKnownClasses::java_lang_ClassNotFoundException_init;
73 ArtMethod* WellKnownClasses::java_lang_Daemons_start;
74 ArtMethod* WellKnownClasses::java_lang_Daemons_stop;
75 ArtMethod* WellKnownClasses::java_lang_Daemons_waitForDaemonStart;
76 ArtMethod* WellKnownClasses::java_lang_Double_doubleToRawLongBits;
77 ArtMethod* WellKnownClasses::java_lang_Double_valueOf;
78 ArtMethod* WellKnownClasses::java_lang_Error_init;
79 ArtMethod* WellKnownClasses::java_lang_Float_floatToRawIntBits;
80 ArtMethod* WellKnownClasses::java_lang_Float_valueOf;
81 ArtMethod* WellKnownClasses::java_lang_IllegalAccessError_init;
82 ArtMethod* WellKnownClasses::java_lang_Integer_valueOf;
83 ArtMethod* WellKnownClasses::java_lang_Long_valueOf;
84 ArtMethod* WellKnownClasses::java_lang_NoClassDefFoundError_init;
85 ArtMethod* WellKnownClasses::java_lang_OutOfMemoryError_init;
86 ArtMethod* WellKnownClasses::java_lang_Runtime_nativeLoad;
87 ArtMethod* WellKnownClasses::java_lang_RuntimeException_init;
88 ArtMethod* WellKnownClasses::java_lang_Short_valueOf;
89 ArtMethod* WellKnownClasses::java_lang_StackOverflowError_init;
90 ArtMethod* WellKnownClasses::java_lang_String_charAt;
91 ArtMethod* WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
92 ArtMethod* WellKnownClasses::java_lang_Thread_init;
93 ArtMethod* WellKnownClasses::java_lang_Thread_run;
94 ArtMethod* WellKnownClasses::java_lang_ThreadGroup_add;
95 ArtMethod* WellKnownClasses::java_lang_ThreadGroup_threadTerminated;
96 ArtMethod* WellKnownClasses::java_lang_invoke_MethodHandle_asType;
97 ArtMethod* WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact;
98 ArtMethod* WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
99 ArtMethod* WellKnownClasses::java_lang_invoke_MethodHandles_makeIdentity;
100 ArtMethod* WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor;
101 ArtMethod* WellKnownClasses::java_lang_invoke_MethodType_makeImpl;
102 ArtMethod* WellKnownClasses::java_lang_ref_FinalizerReference_add;
103 ArtMethod* WellKnownClasses::java_lang_ref_ReferenceQueue_add;
104 ArtMethod* WellKnownClasses::java_lang_reflect_InvocationTargetException_init;
105 ArtMethod* WellKnownClasses::java_lang_reflect_Parameter_init;
106 ArtMethod* WellKnownClasses::java_lang_reflect_Proxy_init;
107 ArtMethod* WellKnownClasses::java_lang_reflect_Proxy_invoke;
108 ArtMethod* WellKnownClasses::java_nio_Buffer_isDirect;
109 ArtMethod* WellKnownClasses::java_nio_DirectByteBuffer_init;
110 ArtMethod* WellKnownClasses::java_util_function_Consumer_accept;
111 ArtMethod* WellKnownClasses::jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D;
112 ArtMethod* WellKnownClasses::jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_F;
113 ArtMethod* WellKnownClasses::jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_getChars;
114 ArtMethod* WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
115 ArtMethod* WellKnownClasses::libcore_reflect_AnnotationMember_init;
116 ArtMethod* WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
117 ArtMethod* WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
118 
119 ArtField* WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
120 ArtField* WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders;
121 ArtField* WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter;
122 ArtField* WellKnownClasses::dalvik_system_DexFile_cookie;
123 ArtField* WellKnownClasses::dalvik_system_DexFile_fileName;
124 ArtField* WellKnownClasses::dalvik_system_DexPathList_dexElements;
125 ArtField* WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
126 ArtField* WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer;
127 ArtField* WellKnownClasses::java_io_FileDescriptor_descriptor;
128 ArtField* WellKnownClasses::java_lang_ClassLoader_parent;
129 ArtField* WellKnownClasses::java_lang_String_EMPTY;
130 ArtField* WellKnownClasses::java_lang_Thread_parkBlocker;
131 ArtField* WellKnownClasses::java_lang_Thread_daemon;
132 ArtField* WellKnownClasses::java_lang_Thread_group;
133 ArtField* WellKnownClasses::java_lang_Thread_lock;
134 ArtField* WellKnownClasses::java_lang_Thread_name;
135 ArtField* WellKnownClasses::java_lang_Thread_priority;
136 ArtField* WellKnownClasses::java_lang_Thread_nativePeer;
137 ArtField* WellKnownClasses::java_lang_Thread_systemDaemon;
138 ArtField* WellKnownClasses::java_lang_Thread_unparkedBeforeStart;
139 ArtField* WellKnownClasses::java_lang_ThreadGroup_groups;
140 ArtField* WellKnownClasses::java_lang_ThreadGroup_ngroups;
141 ArtField* WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
142 ArtField* WellKnownClasses::java_lang_ThreadGroup_name;
143 ArtField* WellKnownClasses::java_lang_ThreadGroup_parent;
144 ArtField* WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
145 ArtField* WellKnownClasses::java_lang_Throwable_cause;
146 ArtField* WellKnownClasses::java_lang_Throwable_detailMessage;
147 ArtField* WellKnownClasses::java_lang_Throwable_stackTrace;
148 ArtField* WellKnownClasses::java_lang_Throwable_stackState;
149 ArtField* WellKnownClasses::java_lang_Throwable_suppressedExceptions;
150 ArtField* WellKnownClasses::java_nio_Buffer_address;
151 ArtField* WellKnownClasses::java_nio_Buffer_capacity;
152 ArtField* WellKnownClasses::java_nio_Buffer_elementSizeShift;
153 ArtField* WellKnownClasses::java_nio_Buffer_limit;
154 ArtField* WellKnownClasses::java_nio_Buffer_position;
155 ArtField* WellKnownClasses::java_nio_ByteBuffer_hb;
156 ArtField* WellKnownClasses::java_nio_ByteBuffer_isReadOnly;
157 ArtField* WellKnownClasses::java_nio_ByteBuffer_offset;
158 ArtField* WellKnownClasses::java_util_Collections_EMPTY_LIST;
159 ArtField* WellKnownClasses::java_util_concurrent_ThreadLocalRandom_seeder;
160 ArtField* WellKnownClasses::jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_buffer;
161 ArtField* WellKnownClasses::jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer_image;
162 ArtField* WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
163 ArtField* WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
164 ArtField* WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
165 ArtField* WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
166 ArtField* WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
167 
168 ArtField* WellKnownClasses::java_lang_Byte_ByteCache_cache;
169 ArtField* WellKnownClasses::java_lang_Character_CharacterCache_cache;
170 ArtField* WellKnownClasses::java_lang_Short_ShortCache_cache;
171 ArtField* WellKnownClasses::java_lang_Integer_IntegerCache_cache;
172 ArtField* WellKnownClasses::java_lang_Long_LongCache_cache;
173 
174 ArtField* WellKnownClasses::java_lang_Byte_value;
175 ArtField* WellKnownClasses::java_lang_Character_value;
176 ArtField* WellKnownClasses::java_lang_Short_value;
177 ArtField* WellKnownClasses::java_lang_Integer_value;
178 ArtField* WellKnownClasses::java_lang_Long_value;
179 
FindSystemClass(ClassLinker * class_linker,Thread * self,const char * descriptor)180 static ObjPtr<mirror::Class> FindSystemClass(ClassLinker* class_linker,
181                                              Thread* self,
182                                              const char* descriptor)
183     REQUIRES_SHARED(Locks::mutator_lock_) {
184   ObjPtr<mirror::Class> klass = class_linker->FindSystemClass(self, descriptor);
185   CHECK(klass != nullptr) << "Couldn't find system class: " << descriptor;
186   return klass;
187 }
188 
CacheClass(JNIEnv * env,const char * jni_class_name)189 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
190   ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
191   if (c.get() == nullptr) {
192     LOG(FATAL) << "Couldn't find class: " << jni_class_name;
193   }
194   return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
195 }
196 
CacheField(ObjPtr<mirror::Class> klass,bool is_static,const char * name,const char * signature)197 static ArtField* CacheField(ObjPtr<mirror::Class> klass,
198                             bool is_static,
199                             const char* name,
200                             const char* signature) REQUIRES_SHARED(Locks::mutator_lock_) {
201   ArtField* field = is_static
202       ? klass->FindDeclaredStaticField(name, signature)
203       : klass->FindDeclaredInstanceField(name, signature);
204   if (UNLIKELY(field == nullptr)) {
205     std::ostringstream os;
206     klass->DumpClass(os, mirror::Class::kDumpClassFullDetail);
207     LOG(FATAL) << "Couldn't find " << (is_static ? "static" : "instance") << " field \""
208                << name << "\" with signature \"" << signature << "\": " << os.str();
209     UNREACHABLE();
210   }
211   return field;
212 }
213 
CacheMethod(ObjPtr<mirror::Class> klass,bool is_static,const char * name,const char * signature,PointerSize pointer_size)214 static ArtMethod* CacheMethod(ObjPtr<mirror::Class> klass,
215                               bool is_static,
216                               const char* name,
217                               const char* signature,
218                               PointerSize pointer_size) REQUIRES_SHARED(Locks::mutator_lock_) {
219   ArtMethod* method = klass->IsInterface()
220       ? klass->FindInterfaceMethod(name, signature, pointer_size)
221       : klass->FindClassMethod(name, signature, pointer_size);
222   if (UNLIKELY(method == nullptr) || UNLIKELY(is_static != method->IsStatic())) {
223     std::ostringstream os;
224     klass->DumpClass(os, mirror::Class::kDumpClassFullDetail);
225     LOG(FATAL) << "Couldn't find " << (is_static ? "static" : "instance") << " method \""
226                << name << "\" with signature \"" << signature << "\": " << os.str();
227     UNREACHABLE();
228   }
229   DCHECK(method->GetDeclaringClass() == klass);
230   return method;
231 }
232 
CachePrimitiveBoxingMethod(ClassLinker * class_linker,Thread * self,char prim_name,const char * boxed_name)233 static ArtMethod* CachePrimitiveBoxingMethod(ClassLinker* class_linker,
234                                              Thread* self,
235                                              char prim_name,
236                                              const char* boxed_name)
237     REQUIRES_SHARED(Locks::mutator_lock_) {
238   ObjPtr<mirror::Class> boxed_class = FindSystemClass(class_linker, self, boxed_name);
239   PointerSize pointer_size = class_linker->GetImagePointerSize();
240   std::string signature = android::base::StringPrintf("(%c)%s", prim_name, boxed_name);
241   return CacheMethod(boxed_class, /*is_static=*/ true, "valueOf", signature.c_str(), pointer_size);
242 }
243 
CacheBoxingCacheField(ClassLinker * class_linker,Thread * self,const char * class_name,const char * cache_type)244 static ArtField* CacheBoxingCacheField(ClassLinker* class_linker,
245                                        Thread* self,
246                                        const char* class_name,
247                                        const char* cache_type)
248     REQUIRES_SHARED(Locks::mutator_lock_) {
249   ObjPtr<mirror::Class> boxed_class = FindSystemClass(class_linker, self, class_name);
250   return CacheField(boxed_class, /*is_static=*/ true, "cache", cache_type);
251 }
252 
CacheValueInBoxField(ClassLinker * class_linker,Thread * self,const char * class_name,const char * cache_type)253 static ArtField* CacheValueInBoxField(ClassLinker* class_linker,
254                                       Thread* self,
255                                       const char* class_name,
256                                       const char* cache_type)
257     REQUIRES_SHARED(Locks::mutator_lock_) {
258   ObjPtr<mirror::Class> boxed_class = FindSystemClass(class_linker, self, class_name);
259   return CacheField(boxed_class, /*is_static=*/ false, "value", cache_type);
260 }
261 
262 #define STRING_INIT_LIST(V) \
263   V(java_lang_String_init, "()V", newEmptyString, "newEmptyString", "()Ljava/lang/String;", NewEmptyString) \
264   V(java_lang_String_init_B, "([B)V", newStringFromBytes_B, "newStringFromBytes", "([B)Ljava/lang/String;", NewStringFromBytes_B) \
265   V(java_lang_String_init_BB, "([BB)V", newStringFromBytes_BB, "newStringFromBytes", "([BB)Ljava/lang/String;", NewStringFromBytes_BB) \
266   V(java_lang_String_init_BI, "([BI)V", newStringFromBytes_BI, "newStringFromBytes", "([BI)Ljava/lang/String;", NewStringFromBytes_BI) \
267   V(java_lang_String_init_BII, "([BII)V", newStringFromBytes_BII, "newStringFromBytes", "([BII)Ljava/lang/String;", NewStringFromBytes_BII) \
268   V(java_lang_String_init_BIII, "([BIII)V", newStringFromBytes_BIII, "newStringFromBytes", "([BIII)Ljava/lang/String;", NewStringFromBytes_BIII) \
269   V(java_lang_String_init_BIIString, "([BIILjava/lang/String;)V", newStringFromBytes_BIIString, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BIIString) \
270   V(java_lang_String_init_BString, "([BLjava/lang/String;)V", newStringFromBytes_BString, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BString) \
271   V(java_lang_String_init_BIICharset, "([BIILjava/nio/charset/Charset;)V", newStringFromBytes_BIICharset, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BIICharset) \
272   V(java_lang_String_init_BCharset, "([BLjava/nio/charset/Charset;)V", newStringFromBytes_BCharset, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BCharset) \
273   V(java_lang_String_init_C, "([C)V", newStringFromChars_C, "newStringFromChars", "([C)Ljava/lang/String;", NewStringFromChars_C) \
274   V(java_lang_String_init_CII, "([CII)V", newStringFromChars_CII, "newStringFromChars", "([CII)Ljava/lang/String;", NewStringFromChars_CII) \
275   V(java_lang_String_init_IIC, "(II[C)V", newStringFromChars_IIC, "newStringFromChars", "(II[C)Ljava/lang/String;", NewStringFromChars_IIC) \
276   V(java_lang_String_init_String, "(Ljava/lang/String;)V", newStringFromString, "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;", NewStringFromString) \
277   V(java_lang_String_init_StringBuffer, "(Ljava/lang/StringBuffer;)V", newStringFromStringBuffer, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;", NewStringFromStringBuffer) \
278   V(java_lang_String_init_III, "([III)V", newStringFromCodePoints, "newStringFromCodePoints", "([III)Ljava/lang/String;", NewStringFromCodePoints) \
279   V(java_lang_String_init_StringBuilder, "(Ljava/lang/StringBuilder;)V", newStringFromStringBuilder, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;", NewStringFromStringBuilder) \
280 
281 #define STATIC_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, ...) \
282     static ArtMethod* init_runtime_name = nullptr; \
283     static ArtMethod* new_runtime_name = nullptr;
STRING_INIT_LIST(STATIC_STRING_INIT)284     STRING_INIT_LIST(STATIC_STRING_INIT)
285 #undef STATIC_STRING_INIT
286 
287 void WellKnownClasses::InitStringInit(ObjPtr<mirror::Class> string_class,
288                                       ObjPtr<mirror::Class> string_builder_class) {
289   PointerSize p_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
290   auto find_method = [p_size](ObjPtr<mirror::Class> klass,
291                               const char* name,
292                               const char* sig,
293                               bool expext_static) REQUIRES_SHARED(Locks::mutator_lock_) {
294     ArtMethod* ret = klass->FindClassMethod(name, sig, p_size);
295     CHECK(ret != nullptr);
296     CHECK_EQ(expext_static, ret->IsStatic());
297     return ret;
298   };
299 
300   #define LOAD_STRING_INIT(init_runtime_name, init_signature, new_runtime_name,                  \
301                            new_java_name, new_signature, ...)                                    \
302       init_runtime_name = find_method(string_class, "<init>", init_signature, false);            \
303       new_runtime_name = find_method(string_builder_class, new_java_name, new_signature, true);
304       STRING_INIT_LIST(LOAD_STRING_INIT)
305   #undef LOAD_STRING_INIT
306 }
307 
InitStringEntryPoints()308 void Thread::InitStringEntryPoints() {
309   QuickEntryPoints* qpoints = &tlsPtr_.quick_entrypoints;
310 #define SET_ENTRY_POINT(init_runtime_name,                                        \
311                         init_signature,                                           \
312                         new_runtime_name,                                         \
313                         new_java_name,                                            \
314                         new_signature,                                            \
315                         entry_point_name)                                         \
316   DCHECK_IMPLIES(Runtime::Current()->IsStarted(), (new_runtime_name) != nullptr); \
317   qpoints->p##entry_point_name = reinterpret_cast<void*>(new_runtime_name);
318   STRING_INIT_LIST(SET_ENTRY_POINT)
319 #undef SET_ENTRY_POINT
320 }
321 
StringInitToStringFactory(ArtMethod * string_init)322 ArtMethod* WellKnownClasses::StringInitToStringFactory(ArtMethod* string_init) {
323   #define TO_STRING_FACTORY(init_runtime_name, init_signature, new_runtime_name,            \
324                             new_java_name, new_signature, entry_point_name)                 \
325       DCHECK((init_runtime_name) != nullptr);                                               \
326       if (string_init == (init_runtime_name)) {                                             \
327         DCHECK((new_runtime_name) != nullptr);                                              \
328         return (new_runtime_name);                                                          \
329       }
330       STRING_INIT_LIST(TO_STRING_FACTORY)
331   #undef TO_STRING_FACTORY
332   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
333   UNREACHABLE();
334 }
335 
StringInitToEntryPoint(ArtMethod * string_init)336 uint32_t WellKnownClasses::StringInitToEntryPoint(ArtMethod* string_init) {
337   #define TO_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name,               \
338                          new_java_name, new_signature, entry_point_name)                    \
339       if (string_init == (init_runtime_name)) {                                             \
340         return kQuick ## entry_point_name;                                                  \
341       }
342       STRING_INIT_LIST(TO_ENTRY_POINT)
343   #undef TO_ENTRY_POINT
344   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
345   UNREACHABLE();
346 }
347 #undef STRING_INIT_LIST
348 
Init(JNIEnv * env)349 void WellKnownClasses::Init(JNIEnv* env) {
350   hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
351       hiddenapi::EnforcementPolicy::kDisabled);
352 
353   dalvik_annotation_optimization_CriticalNative =
354       CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
355   dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");
356   dalvik_annotation_optimization_NeverCompile =
357       CacheClass(env, "dalvik/annotation/optimization/NeverCompile");
358   dalvik_annotation_optimization_NeverInline =
359       CacheClass(env, "dalvik/annotation/optimization/NeverInline");
360 
361   java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
362   java_lang_ClassValue = CacheClass(env, "java/lang/ClassValue");
363   java_lang_Record = CacheClass(env, "java/lang/Record");
364   java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
365   java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
366   java_lang_System = CacheClass(env, "java/lang/System");
367   java_lang_Void = CacheClass(env, "java/lang/Void");
368   libcore_reflect_AnnotationMember__array = CacheClass(env, "[Llibcore/reflect/AnnotationMember;");
369 
370   InitFieldsAndMethodsOnly(env);
371 }
372 
InitFieldsAndMethodsOnly(JNIEnv * env)373 void WellKnownClasses::InitFieldsAndMethodsOnly(JNIEnv* env) {
374   hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
375       hiddenapi::EnforcementPolicy::kDisabled);
376 
377   Thread* self = Thread::ForEnv(env);
378   ScopedObjectAccess soa(self);
379   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
380 
381   java_lang_Boolean_valueOf =
382       CachePrimitiveBoxingMethod(class_linker, self, 'Z', "Ljava/lang/Boolean;");
383   java_lang_Byte_valueOf =
384       CachePrimitiveBoxingMethod(class_linker, self, 'B', "Ljava/lang/Byte;");
385   java_lang_Character_valueOf =
386       CachePrimitiveBoxingMethod(class_linker, self, 'C', "Ljava/lang/Character;");
387   java_lang_Double_valueOf =
388       CachePrimitiveBoxingMethod(class_linker, self, 'D', "Ljava/lang/Double;");
389   java_lang_Float_valueOf =
390       CachePrimitiveBoxingMethod(class_linker, self, 'F', "Ljava/lang/Float;");
391   java_lang_Integer_valueOf =
392       CachePrimitiveBoxingMethod(class_linker, self, 'I', "Ljava/lang/Integer;");
393   java_lang_Long_valueOf =
394       CachePrimitiveBoxingMethod(class_linker, self, 'J', "Ljava/lang/Long;");
395   java_lang_Short_valueOf =
396       CachePrimitiveBoxingMethod(class_linker, self, 'S', "Ljava/lang/Short;");
397 
398   java_lang_Byte_ByteCache_cache = CacheBoxingCacheField(
399       class_linker, self, "Ljava/lang/Byte$ByteCache;", "[Ljava/lang/Byte;");
400   java_lang_Character_CharacterCache_cache = CacheBoxingCacheField(
401       class_linker, self, "Ljava/lang/Character$CharacterCache;", "[Ljava/lang/Character;");
402   java_lang_Short_ShortCache_cache = CacheBoxingCacheField(
403       class_linker, self, "Ljava/lang/Short$ShortCache;", "[Ljava/lang/Short;");
404   java_lang_Integer_IntegerCache_cache = CacheBoxingCacheField(
405       class_linker, self, "Ljava/lang/Integer$IntegerCache;", "[Ljava/lang/Integer;");
406   java_lang_Long_LongCache_cache = CacheBoxingCacheField(
407       class_linker, self, "Ljava/lang/Long$LongCache;", "[Ljava/lang/Long;");
408 
409   java_lang_Byte_value = CacheValueInBoxField(
410       class_linker, self, "Ljava/lang/Byte;", "B");
411   java_lang_Character_value = CacheValueInBoxField(
412       class_linker, self, "Ljava/lang/Character;", "C");
413   java_lang_Short_value = CacheValueInBoxField(
414       class_linker, self, "Ljava/lang/Short;", "S");
415   java_lang_Integer_value = CacheValueInBoxField(
416       class_linker, self, "Ljava/lang/Integer;", "I");
417   java_lang_Long_value = CacheValueInBoxField(
418       class_linker, self, "Ljava/lang/Long;", "J");
419 
420   StackHandleScope<44u> hs(self);
421   Handle<mirror::Class> d_s_bdcl =
422       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/BaseDexClassLoader;"));
423   Handle<mirror::Class> d_s_dlcl =
424       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/DelegateLastClassLoader;"));
425   Handle<mirror::Class> d_s_dcl =
426       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/DexClassLoader;"));
427   Handle<mirror::Class> d_s_df =
428       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/DexFile;"));
429   Handle<mirror::Class> d_s_dpl =
430       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/DexPathList;"));
431   Handle<mirror::Class> d_s_dpl_e =
432       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/DexPathList$Element;"));
433   Handle<mirror::Class> d_s_imdcl =
434       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/InMemoryDexClassLoader;"));
435   Handle<mirror::Class> d_s_pcl =
436       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/PathClassLoader;"));
437   Handle<mirror::Class> d_s_vmr =
438       hs.NewHandle(FindSystemClass(class_linker, self, "Ldalvik/system/VMRuntime;"));
439   Handle<mirror::Class> j_i_fd =
440       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/io/FileDescriptor;"));
441   Handle<mirror::Class> j_l_bcl =
442       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/BootClassLoader;"));
443   Handle<mirror::Class> j_l_cl =
444       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/ClassLoader;"));
445   Handle<mirror::Class> j_l_cnfe =
446       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/ClassNotFoundException;"));
447   Handle<mirror::Class> j_l_Daemons =
448       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/Daemons;"));
449   Handle<mirror::Class> j_l_Error =
450       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/Error;"));
451   Handle<mirror::Class> j_l_IllegalAccessError =
452       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/IllegalAccessError;"));
453   Handle<mirror::Class> j_l_NoClassDefFoundError =
454       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/NoClassDefFoundError;"));
455   Handle<mirror::Class> j_l_OutOfMemoryError =
456       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/OutOfMemoryError;"));
457   Handle<mirror::Class> j_l_RuntimeException =
458       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/RuntimeException;"));
459   Handle<mirror::Class> j_l_StackOverflowError =
460       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/StackOverflowError;"));
461   Handle<mirror::Class> j_l_Thread =
462       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/Thread;"));
463   Handle<mirror::Class> j_l_tg =
464       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/ThreadGroup;"));
465   Handle<mirror::Class> j_l_i_MethodHandle =
466       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/invoke/MethodHandle;"));
467   Handle<mirror::Class> j_l_i_MethodHandles =
468       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/invoke/MethodHandles;"));
469   Handle<mirror::Class> j_l_i_MethodHandles_Lookup =
470       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/invoke/MethodHandles$Lookup;"));
471   Handle<mirror::Class> j_l_i_MethodType =
472       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/invoke/MethodType;"));
473   Handle<mirror::Class> j_l_r_fr =
474       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/ref/FinalizerReference;"));
475   Handle<mirror::Class> j_l_r_rq =
476       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/ref/ReferenceQueue;"));
477   Handle<mirror::Class> j_l_rl_ite = hs.NewHandle(
478       FindSystemClass(class_linker, self, "Ljava/lang/reflect/InvocationTargetException;"));
479   Handle<mirror::Class> j_l_rl_Parameter =
480       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/reflect/Parameter;"));
481   Handle<mirror::Class> j_n_b =
482       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/nio/Buffer;"));
483   Handle<mirror::Class> j_n_bb =
484       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/nio/ByteBuffer;"));
485   Handle<mirror::Class> j_n_dbb =
486       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/nio/DirectByteBuffer;"));
487   Handle<mirror::Class> j_u_c =
488       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/util/Collections;"));
489   Handle<mirror::Class> j_u_c_tlr =
490       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/util/concurrent/ThreadLocalRandom;"));
491   Handle<mirror::Class> j_u_f_c =
492       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/util/function/Consumer;"));
493   Handle<mirror::Class> j_i_m_fd =
494       hs.NewHandle(FindSystemClass(class_linker, self, "Ljdk/internal/math/FloatingDecimal;"));
495   Handle<mirror::Class> j_i_m_fd_btab = hs.NewHandle(FindSystemClass(
496       class_linker, self, "Ljdk/internal/math/FloatingDecimal$BinaryToASCIIBuffer;"));
497   Handle<mirror::Class> j_i_m_fd_ebtab = hs.NewHandle(FindSystemClass(
498       class_linker, self, "Ljdk/internal/math/FloatingDecimal$ExceptionalBinaryToASCIIBuffer;"));
499   Handle<mirror::Class> l_r_af =
500       hs.NewHandle(FindSystemClass(class_linker, self, "Llibcore/reflect/AnnotationFactory;"));
501   Handle<mirror::Class> l_r_am =
502       hs.NewHandle(FindSystemClass(class_linker, self, "Llibcore/reflect/AnnotationMember;"));
503   Handle<mirror::Class> l_u_ea =
504       hs.NewHandle(FindSystemClass(class_linker, self, "Llibcore/util/EmptyArray;"));
505   Handle<mirror::Class> o_a_h_d_c =
506       hs.NewHandle(FindSystemClass(class_linker, self, "Lorg/apache/harmony/dalvik/ddmc/Chunk;"));
507   Handle<mirror::Class> o_a_h_d_d_ds =
508       hs.NewHandle(FindSystemClass(class_linker, self, "Lorg/apache/harmony/dalvik/ddmc/DdmServer;"));
509 
510   ScopedAssertNoThreadSuspension sants(__FUNCTION__);
511   PointerSize pointer_size = class_linker->GetImagePointerSize();
512 
513   dalvik_system_BaseDexClassLoader_getLdLibraryPath = CacheMethod(
514       d_s_bdcl.Get(),
515       /*is_static=*/ false,
516       "getLdLibraryPath",
517       "()Ljava/lang/String;",
518       pointer_size);
519   dalvik_system_DelegateLastClassLoader_init = CacheMethod(
520       d_s_dlcl.Get(),
521       /*is_static=*/ false,
522       "<init>",
523       "(Ljava/lang/String;Ljava/lang/ClassLoader;)V",
524       pointer_size);
525   dalvik_system_DexClassLoader_init = CacheMethod(
526       d_s_dcl.Get(),
527       /*is_static=*/ false,
528       "<init>",
529       "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)V",
530       pointer_size);
531   dalvik_system_InMemoryDexClassLoader_init = CacheMethod(
532       d_s_imdcl.Get(),
533       /*is_static=*/ false,
534       "<init>",
535       "(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V",
536       pointer_size);
537   dalvik_system_PathClassLoader_init = CacheMethod(
538       d_s_pcl.Get(),
539       /*is_static=*/ false,
540       "<init>",
541       "(Ljava/lang/String;Ljava/lang/ClassLoader;)V",
542       pointer_size);
543 
544   dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(
545       d_s_vmr.Get(),
546       /*is_static=*/ true,
547       "hiddenApiUsed",
548       "(ILjava/lang/String;Ljava/lang/String;IZ)V",
549       pointer_size);
550 
551   java_lang_BootClassLoader_init =
552       CacheMethod(j_l_bcl.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
553   java_lang_ClassLoader_loadClass = CacheMethod(
554       j_l_cl.Get(),
555       /*is_static=*/ false,
556       "loadClass",
557       "(Ljava/lang/String;)Ljava/lang/Class;",
558       pointer_size);
559 
560   java_lang_ClassNotFoundException_init = CacheMethod(
561       j_l_cnfe.Get(),
562       /*is_static=*/ false,
563       "<init>",
564       "(Ljava/lang/String;Ljava/lang/Throwable;)V",
565       pointer_size);
566 
567   ObjPtr<mirror::Class> j_l_Double = java_lang_Double_valueOf->GetDeclaringClass();
568   java_lang_Double_doubleToRawLongBits =
569       CacheMethod(j_l_Double, /*is_static=*/ true, "doubleToRawLongBits", "(D)J", pointer_size);
570   ObjPtr<mirror::Class> j_l_Float = java_lang_Float_valueOf->GetDeclaringClass();
571   java_lang_Float_floatToRawIntBits =
572       CacheMethod(j_l_Float, /*is_static=*/ true, "floatToRawIntBits", "(F)I", pointer_size);
573 
574   java_lang_Daemons_start = CacheMethod(
575       j_l_Daemons.Get(), /*is_static=*/ true, "start", "()V", pointer_size);
576   java_lang_Daemons_stop = CacheMethod(
577       j_l_Daemons.Get(), /*is_static=*/ true, "stop", "()V", pointer_size);
578   java_lang_Daemons_waitForDaemonStart = CacheMethod(
579       j_l_Daemons.Get(), /*is_static=*/ true, "waitForDaemonStart", "()V", pointer_size);
580 
581   java_lang_Error_init = CacheMethod(
582       j_l_Error.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
583   java_lang_IllegalAccessError_init = CacheMethod(
584       j_l_IllegalAccessError.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
585   java_lang_NoClassDefFoundError_init = CacheMethod(
586       j_l_NoClassDefFoundError.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
587   java_lang_OutOfMemoryError_init = CacheMethod(
588       j_l_OutOfMemoryError.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
589   java_lang_RuntimeException_init = CacheMethod(
590       j_l_RuntimeException.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
591   java_lang_StackOverflowError_init = CacheMethod(
592       j_l_StackOverflowError.Get(), /*is_static=*/ false, "<init>", "()V", pointer_size);
593 
594   ObjPtr<mirror::Class> j_l_String = GetClassRoot<mirror::String>(class_linker);
595   java_lang_String_charAt = CacheMethod(
596       j_l_String, /*is_static=*/ false, "charAt", "(I)C", pointer_size);
597 
598   java_lang_Thread_dispatchUncaughtException = CacheMethod(
599       j_l_Thread.Get(),
600       /*is_static=*/ false,
601       "dispatchUncaughtException",
602       "(Ljava/lang/Throwable;)V",
603       pointer_size);
604   java_lang_Thread_init = CacheMethod(
605       j_l_Thread.Get(),
606       /*is_static=*/ false,
607       "<init>",
608       "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V",
609       pointer_size);
610   java_lang_Thread_run = CacheMethod(
611       j_l_Thread.Get(), /*is_static=*/ false, "run", "()V", pointer_size);
612   java_lang_ThreadGroup_add = CacheMethod(
613       j_l_tg.Get(), /*is_static=*/ false, "add", "(Ljava/lang/Thread;)V", pointer_size);
614   java_lang_ThreadGroup_threadTerminated = CacheMethod(
615       j_l_tg.Get(),
616       /*is_static=*/ false,
617       "threadTerminated",
618       "(Ljava/lang/Thread;)V",
619       pointer_size);
620 
621   java_lang_invoke_MethodHandle_asType = CacheMethod(
622       j_l_i_MethodHandle.Get(),
623       /*is_static=*/ false,
624       "asType",
625       "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
626       pointer_size);
627   java_lang_invoke_MethodHandle_invokeExact = CacheMethod(
628       j_l_i_MethodHandle.Get(),
629       /*is_static=*/ false,
630       "invokeExact",
631       "([Ljava/lang/Object;)Ljava/lang/Object;",
632       pointer_size);
633   java_lang_invoke_MethodHandles_lookup = CacheMethod(
634       j_l_i_MethodHandles.Get(),
635       /*is_static=*/ true,
636       "lookup",
637       "()Ljava/lang/invoke/MethodHandles$Lookup;",
638       pointer_size);
639   java_lang_invoke_MethodHandles_makeIdentity = CacheMethod(
640       j_l_i_MethodHandles.Get(),
641       /*is_static=*/ true,
642       "makeIdentity",
643       "(Ljava/lang/Class;)Ljava/lang/invoke/MethodHandle;",
644       pointer_size);
645   java_lang_invoke_MethodHandles_Lookup_findConstructor = CacheMethod(
646       j_l_i_MethodHandles_Lookup.Get(),
647       /*is_static=*/ false,
648       "findConstructor",
649       "(Ljava/lang/Class;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
650       pointer_size);
651   java_lang_invoke_MethodType_makeImpl = CacheMethod(
652       j_l_i_MethodType.Get(),
653       /* is_static=*/ true,
654       "makeImpl",
655       "(Ljava/lang/Class;[Ljava/lang/Class;Z)Ljava/lang/invoke/MethodType;",
656       pointer_size);
657 
658   java_lang_ref_FinalizerReference_add = CacheMethod(
659       j_l_r_fr.Get(), /*is_static=*/ true, "add", "(Ljava/lang/Object;)V", pointer_size);
660   java_lang_ref_ReferenceQueue_add = CacheMethod(
661       j_l_r_rq.Get(), /*is_static=*/ true, "add", "(Ljava/lang/ref/Reference;)V", pointer_size);
662 
663   java_lang_reflect_InvocationTargetException_init = CacheMethod(
664       j_l_rl_ite.Get(), /*is_static=*/ false, "<init>", "(Ljava/lang/Throwable;)V", pointer_size);
665   java_lang_reflect_Parameter_init = CacheMethod(
666       j_l_rl_Parameter.Get(),
667       /*is_static=*/ false,
668       "<init>",
669       "(Ljava/lang/String;ILjava/lang/reflect/Executable;I)V",
670       pointer_size);
671 
672   ObjPtr<mirror::Class> j_l_rl_Proxy = GetClassRoot<mirror::Proxy>(class_linker);
673   java_lang_reflect_Proxy_init = CacheMethod(
674       j_l_rl_Proxy,
675       /*is_static=*/ false,
676       "<init>",
677       "(Ljava/lang/reflect/InvocationHandler;)V",
678       pointer_size);
679   java_lang_reflect_Proxy_invoke = CacheMethod(
680       j_l_rl_Proxy,
681       /*is_static=*/ true,
682       "invoke",
683       "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;",
684       pointer_size);
685 
686   java_nio_Buffer_isDirect =
687       CacheMethod(j_n_b.Get(), /*is_static=*/ false, "isDirect", "()Z", pointer_size);
688   java_nio_DirectByteBuffer_init =
689       CacheMethod(j_n_dbb.Get(), /*is_static=*/ false, "<init>", "(JI)V", pointer_size);
690 
691   java_util_function_Consumer_accept = CacheMethod(
692       j_u_f_c.Get(), /*is_static=*/ false, "accept", "(Ljava/lang/Object;)V", pointer_size);
693 
694   jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D = CacheMethod(
695       j_i_m_fd.Get(),
696       /*is_static=*/ true,
697       "getBinaryToASCIIConverter",
698       "(D)Ljdk/internal/math/FloatingDecimal$BinaryToASCIIConverter;",
699       pointer_size);
700   jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_F = CacheMethod(
701       j_i_m_fd.Get(),
702       /*is_static=*/ true,
703       "getBinaryToASCIIConverter",
704       "(F)Ljdk/internal/math/FloatingDecimal$BinaryToASCIIConverter;",
705       pointer_size);
706   jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_getChars =
707       CacheMethod(j_i_m_fd_btab.Get(), /*is_static=*/ false, "getChars", "([C)I", pointer_size);
708 
709   libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(
710       l_r_af.Get(),
711       /*is_static=*/ true,
712       "createAnnotation",
713       "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;",
714       pointer_size);
715   libcore_reflect_AnnotationMember_init = CacheMethod(
716       l_r_am.Get(),
717       /*is_static=*/ false,
718       "<init>",
719       "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/reflect/Method;)V",
720       pointer_size);
721 
722   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast =
723       CacheMethod(o_a_h_d_d_ds.Get(), /*is_static=*/ true, "broadcast", "(I)V", pointer_size);
724   org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = CacheMethod(
725       o_a_h_d_d_ds.Get(),
726       /*is_static=*/ true,
727       "dispatch",
728       "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;",
729       pointer_size);
730 
731   dalvik_system_BaseDexClassLoader_pathList = CacheField(
732       d_s_bdcl.Get(), /*is_static=*/ false, "pathList", "Ldalvik/system/DexPathList;");
733   dalvik_system_BaseDexClassLoader_sharedLibraryLoaders = CacheField(
734       d_s_bdcl.Get(), /*is_static=*/ false, "sharedLibraryLoaders", "[Ljava/lang/ClassLoader;");
735   dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter = CacheField(
736       d_s_bdcl.Get(),
737       /*is_static=*/ false,
738       "sharedLibraryLoadersAfter",
739       "[Ljava/lang/ClassLoader;");
740   dalvik_system_DexFile_cookie = CacheField(
741       d_s_df.Get(), /*is_static=*/ false, "mCookie", "Ljava/lang/Object;");
742   dalvik_system_DexFile_fileName = CacheField(
743       d_s_df.Get(), /*is_static=*/ false, "mFileName", "Ljava/lang/String;");
744   dalvik_system_DexPathList_dexElements = CacheField(
745       d_s_dpl.Get(), /*is_static=*/ false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
746   dalvik_system_DexPathList__Element_dexFile = CacheField(
747       d_s_dpl_e.Get(), /*is_static=*/ false, "dexFile", "Ldalvik/system/DexFile;");
748 
749   dalvik_system_VMRuntime_nonSdkApiUsageConsumer = CacheField(
750       d_s_vmr.Get(),
751       /*is_static=*/ true,
752       "nonSdkApiUsageConsumer",
753       "Ljava/util/function/Consumer;");
754 
755   java_io_FileDescriptor_descriptor = CacheField(
756       j_i_fd.Get(), /*is_static=*/ false, "descriptor", "I");
757 
758   java_lang_ClassLoader_parent = CacheField(
759       j_l_cl.Get(), /*is_static=*/ false, "parent", "Ljava/lang/ClassLoader;");
760 
761   java_lang_String_EMPTY =
762       CacheField(j_l_String, /*is_static=*/true, "EMPTY", "Ljava/lang/String;");
763   java_lang_Thread_parkBlocker =
764       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "parkBlocker", "Ljava/lang/Object;");
765   java_lang_Thread_daemon = CacheField(j_l_Thread.Get(), /*is_static=*/ false, "daemon", "Z");
766   java_lang_Thread_group =
767       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "group", "Ljava/lang/ThreadGroup;");
768   java_lang_Thread_lock =
769       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "lock", "Ljava/lang/Object;");
770   java_lang_Thread_name =
771       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "name", "Ljava/lang/String;");
772   java_lang_Thread_priority = CacheField(j_l_Thread.Get(), /*is_static=*/ false, "priority", "I");
773   java_lang_Thread_nativePeer =
774       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "nativePeer", "J");
775   java_lang_Thread_systemDaemon =
776       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "systemDaemon", "Z");
777   java_lang_Thread_unparkedBeforeStart =
778       CacheField(j_l_Thread.Get(), /*is_static=*/ false, "unparkedBeforeStart", "Z");
779 
780   java_lang_ThreadGroup_groups =
781       CacheField(j_l_tg.Get(), /*is_static=*/ false, "groups", "[Ljava/lang/ThreadGroup;");
782   java_lang_ThreadGroup_ngroups = CacheField(j_l_tg.Get(), /*is_static=*/ false, "ngroups", "I");
783   java_lang_ThreadGroup_mainThreadGroup =
784       CacheField(j_l_tg.Get(), /*is_static=*/ true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
785   java_lang_ThreadGroup_name =
786       CacheField(j_l_tg.Get(), /*is_static=*/ false, "name", "Ljava/lang/String;");
787   java_lang_ThreadGroup_parent =
788       CacheField(j_l_tg.Get(), /*is_static=*/ false, "parent", "Ljava/lang/ThreadGroup;");
789   java_lang_ThreadGroup_systemThreadGroup =
790       CacheField(j_l_tg.Get(), /*is_static=*/ true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
791 
792   ObjPtr<mirror::Class> j_l_Throwable = GetClassRoot<mirror::Throwable>(class_linker);
793   java_lang_Throwable_cause = CacheField(
794       j_l_Throwable, /*is_static=*/ false, "cause", "Ljava/lang/Throwable;");
795   java_lang_Throwable_detailMessage = CacheField(
796       j_l_Throwable, /*is_static=*/ false, "detailMessage", "Ljava/lang/String;");
797   java_lang_Throwable_stackTrace = CacheField(
798       j_l_Throwable, /*is_static=*/ false, "stackTrace", "[Ljava/lang/StackTraceElement;");
799   java_lang_Throwable_stackState = CacheField(
800       j_l_Throwable, /*is_static=*/ false, "backtrace", "Ljava/lang/Object;");
801   java_lang_Throwable_suppressedExceptions = CacheField(
802       j_l_Throwable, /*is_static=*/ false, "suppressedExceptions", "Ljava/util/List;");
803 
804   java_nio_Buffer_address = CacheField(j_n_b.Get(), /*is_static=*/ false, "address", "J");
805   java_nio_Buffer_capacity = CacheField(j_n_b.Get(), /*is_static=*/ false, "capacity", "I");
806   java_nio_Buffer_elementSizeShift =
807       CacheField(j_n_b.Get(), /*is_static=*/ false, "_elementSizeShift", "I");
808   java_nio_Buffer_limit = CacheField(j_n_b.Get(), /*is_static=*/ false, "limit", "I");
809   java_nio_Buffer_position = CacheField(j_n_b.Get(), /*is_static=*/ false, "position", "I");
810 
811   java_nio_ByteBuffer_hb = CacheField(j_n_bb.Get(), /*is_static=*/ false, "hb", "[B");
812   java_nio_ByteBuffer_isReadOnly =
813       CacheField(j_n_bb.Get(), /*is_static=*/ false, "isReadOnly", "Z");
814   java_nio_ByteBuffer_offset = CacheField(j_n_bb.Get(), /*is_static=*/ false, "offset", "I");
815 
816   java_util_Collections_EMPTY_LIST =
817       CacheField(j_u_c.Get(), /*is_static=*/ true, "EMPTY_LIST", "Ljava/util/List;");
818 
819   java_util_concurrent_ThreadLocalRandom_seeder = CacheField(
820       j_u_c_tlr.Get(), /*is_static=*/ true, "seeder", "Ljava/util/concurrent/atomic/AtomicLong;");
821 
822   jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_buffer =
823       CacheField(j_i_m_fd_btab.Get(), /*is_static=*/ false, "buffer", "[C");
824   jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer_image = CacheField(
825       j_i_m_fd_ebtab.Get(), /*is_static=*/ false, "image", "Ljava/lang/String;");
826 
827   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(
828       l_u_ea.Get(), /*is_static=*/ true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
829 
830   org_apache_harmony_dalvik_ddmc_Chunk_data =
831       CacheField(o_a_h_d_c.Get(), /*is_static=*/ false, "data", "[B");
832   org_apache_harmony_dalvik_ddmc_Chunk_length =
833       CacheField(o_a_h_d_c.Get(), /*is_static=*/ false, "length", "I");
834   org_apache_harmony_dalvik_ddmc_Chunk_offset =
835       CacheField(o_a_h_d_c.Get(), /*is_static=*/ false, "offset", "I");
836   org_apache_harmony_dalvik_ddmc_Chunk_type =
837       CacheField(o_a_h_d_c.Get(), /*is_static=*/ false, "type", "I");
838 }
839 
LateInit(JNIEnv * env)840 void WellKnownClasses::LateInit(JNIEnv* env) {
841   // Initialize the `Runtime` class that was previously initialized
842   // by `CacheMethod()` calling `FindMethodJNI()`.
843   // TODO: Move this initialization to `ClassLinker`.
844   ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
845   Thread* self = Thread::ForEnv(env);
846   ScopedObjectAccess soa(self);
847   StackHandleScope<1u> hs(self);
848   Handle<mirror::Class> j_l_Runtime =
849       hs.NewHandle(FindSystemClass(class_linker, self, "Ljava/lang/Runtime;"));
850   bool success = class_linker->EnsureInitialized(
851       self, j_l_Runtime, /*can_init_fields=*/ true, /*can_init_parents=*/ true);
852   CHECK(success) << "Failed to initialize " << j_l_Runtime->PrettyDescriptor();
853 
854   // The function `GetClassLoader()` in `jni_internal.cc` is checking if the caller
855   // is `java_lang_Runtime_nativeLoad` and, if so, returns the class loader override.
856   // However, this function is used several times between `WellKnownClasses::Init()`
857   // and setting up the override by the `Runtime` and requires that we take the other
858   // path, rather than returning the uninitialized override. Therefore we cannot
859   // initialize this well-known method early and require the `LateInit()`.
860   // TODO: Clean up the initialization steps.
861   java_lang_Runtime_nativeLoad = CacheMethod(
862       j_l_Runtime.Get(),
863       /*is_static=*/ true,
864       "nativeLoad",
865       "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Class;)Ljava/lang/String;",
866       class_linker->GetImagePointerSize());
867 }
868 
HandleJniIdTypeChange(JNIEnv * env)869 void WellKnownClasses::HandleJniIdTypeChange(JNIEnv* env) {
870   WellKnownClasses::InitFieldsAndMethodsOnly(env);
871   WellKnownClasses::LateInit(env);
872 }
873 
Clear()874 void WellKnownClasses::Clear() {
875   dalvik_annotation_optimization_CriticalNative = nullptr;
876   dalvik_annotation_optimization_FastNative = nullptr;
877   dalvik_annotation_optimization_NeverCompile = nullptr;
878   dalvik_annotation_optimization_NeverInline = nullptr;
879   java_lang_annotation_Annotation__array = nullptr;
880   java_lang_ClassValue = nullptr;
881   java_lang_Record = nullptr;
882   java_lang_reflect_Parameter__array = nullptr;
883   java_lang_StringFactory = nullptr;
884   java_lang_System = nullptr;
885   java_lang_Void = nullptr;
886   libcore_reflect_AnnotationMember__array = nullptr;
887 
888   dalvik_system_BaseDexClassLoader_getLdLibraryPath = nullptr;
889   WellKnownClasses::dalvik_system_DelegateLastClassLoader_init = nullptr;
890   WellKnownClasses::dalvik_system_DexClassLoader_init = nullptr;
891   WellKnownClasses::dalvik_system_InMemoryDexClassLoader_init = nullptr;
892   WellKnownClasses::dalvik_system_PathClassLoader_init = nullptr;
893   dalvik_system_VMRuntime_hiddenApiUsed = nullptr;
894   java_io_FileDescriptor_descriptor = nullptr;
895   java_lang_Boolean_valueOf = nullptr;
896   java_lang_Byte_valueOf = nullptr;
897   java_lang_Character_valueOf = nullptr;
898   java_lang_BootClassLoader_init = nullptr;
899   java_lang_ClassLoader_loadClass = nullptr;
900   java_lang_ClassNotFoundException_init = nullptr;
901   java_lang_Daemons_start = nullptr;
902   java_lang_Daemons_stop = nullptr;
903   java_lang_Daemons_waitForDaemonStart = nullptr;
904   java_lang_Double_doubleToRawLongBits = nullptr;
905   java_lang_Double_valueOf = nullptr;
906   java_lang_Error_init = nullptr;
907   java_lang_Float_floatToRawIntBits = nullptr;
908   java_lang_Float_valueOf = nullptr;
909   java_lang_IllegalAccessError_init = nullptr;
910   java_lang_Integer_valueOf = nullptr;
911   java_lang_Long_valueOf = nullptr;
912   java_lang_NoClassDefFoundError_init = nullptr;
913   java_lang_OutOfMemoryError_init = nullptr;
914   java_lang_Runtime_nativeLoad = nullptr;
915   java_lang_RuntimeException_init = nullptr;
916   java_lang_Short_valueOf = nullptr;
917   java_lang_StackOverflowError_init = nullptr;
918   java_lang_String_charAt = nullptr;
919   java_lang_Thread_dispatchUncaughtException = nullptr;
920   java_lang_Thread_init = nullptr;
921   java_lang_Thread_run = nullptr;
922   java_lang_ThreadGroup_add = nullptr;
923   java_lang_ThreadGroup_threadTerminated = nullptr;
924   java_lang_invoke_MethodHandle_asType = nullptr;
925   java_lang_invoke_MethodHandle_invokeExact = nullptr;
926   java_lang_invoke_MethodHandles_lookup = nullptr;
927   java_lang_invoke_MethodHandles_makeIdentity = nullptr;
928   java_lang_invoke_MethodHandles_Lookup_findConstructor = nullptr;
929   java_lang_invoke_MethodType_makeImpl = nullptr;
930   java_lang_ref_FinalizerReference_add = nullptr;
931   java_lang_ref_ReferenceQueue_add = nullptr;
932   java_lang_reflect_InvocationTargetException_init = nullptr;
933   java_lang_reflect_Parameter_init = nullptr;
934   java_lang_reflect_Proxy_init = nullptr;
935   java_lang_reflect_Proxy_invoke = nullptr;
936   java_nio_Buffer_isDirect = nullptr;
937   java_nio_DirectByteBuffer_init = nullptr;
938   jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D = nullptr;
939   jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_F = nullptr;
940   jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_getChars = nullptr;
941   libcore_reflect_AnnotationFactory_createAnnotation = nullptr;
942   libcore_reflect_AnnotationMember_init = nullptr;
943   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = nullptr;
944   org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = nullptr;
945 
946   dalvik_system_BaseDexClassLoader_pathList = nullptr;
947   dalvik_system_DexFile_cookie = nullptr;
948   dalvik_system_DexFile_fileName = nullptr;
949   dalvik_system_DexPathList_dexElements = nullptr;
950   dalvik_system_DexPathList__Element_dexFile = nullptr;
951   dalvik_system_VMRuntime_nonSdkApiUsageConsumer = nullptr;
952   java_lang_ClassLoader_parent = nullptr;
953   java_lang_String_EMPTY = nullptr;
954   java_lang_Thread_parkBlocker = nullptr;
955   java_lang_Thread_daemon = nullptr;
956   java_lang_Thread_group = nullptr;
957   java_lang_Thread_lock = nullptr;
958   java_lang_Thread_name = nullptr;
959   java_lang_Thread_priority = nullptr;
960   java_lang_Thread_nativePeer = nullptr;
961   java_lang_ThreadGroup_groups = nullptr;
962   java_lang_ThreadGroup_ngroups = nullptr;
963   java_lang_ThreadGroup_mainThreadGroup = nullptr;
964   java_lang_ThreadGroup_name = nullptr;
965   java_lang_ThreadGroup_parent = nullptr;
966   java_lang_ThreadGroup_systemThreadGroup = nullptr;
967   java_lang_Throwable_cause = nullptr;
968   java_lang_Throwable_detailMessage = nullptr;
969   java_lang_Throwable_stackTrace = nullptr;
970   java_lang_Throwable_stackState = nullptr;
971   java_lang_Throwable_suppressedExceptions = nullptr;
972   java_nio_Buffer_address = nullptr;
973   java_nio_Buffer_elementSizeShift = nullptr;
974   java_nio_Buffer_limit = nullptr;
975   java_nio_Buffer_position = nullptr;
976   java_nio_ByteBuffer_hb = nullptr;
977   java_nio_ByteBuffer_isReadOnly = nullptr;
978   java_nio_ByteBuffer_offset = nullptr;
979   java_util_Collections_EMPTY_LIST = nullptr;
980   java_util_concurrent_ThreadLocalRandom_seeder = nullptr;
981   jdk_internal_math_FloatingDecimal_BinaryToASCIIBuffer_buffer = nullptr;
982   jdk_internal_math_FloatingDecimal_ExceptionalBinaryToASCIIBuffer_image = nullptr;
983   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = nullptr;
984   org_apache_harmony_dalvik_ddmc_Chunk_data = nullptr;
985   org_apache_harmony_dalvik_ddmc_Chunk_length = nullptr;
986   org_apache_harmony_dalvik_ddmc_Chunk_offset = nullptr;
987   org_apache_harmony_dalvik_ddmc_Chunk_type = nullptr;
988 
989   java_lang_Byte_ByteCache_cache = nullptr;
990   java_lang_Character_CharacterCache_cache = nullptr;
991   java_lang_Short_ShortCache_cache = nullptr;
992   java_lang_Integer_IntegerCache_cache = nullptr;
993   java_lang_Long_LongCache_cache = nullptr;
994 
995   java_lang_Byte_value = nullptr;
996   java_lang_Character_value = nullptr;
997   java_lang_Short_value = nullptr;
998   java_lang_Integer_value = nullptr;
999   java_lang_Long_value = nullptr;
1000 }
1001 
ToClass(jclass global_jclass)1002 ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
1003   JavaVMExt* vm = Runtime::Current()->GetJavaVM();
1004   auto ret = ObjPtr<mirror::Class>::DownCast(vm->DecodeGlobal(global_jclass));
1005   DCHECK(!ret.IsNull());
1006   return ret;
1007 }
1008 
1009 }  // namespace art
1010