• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/enums.h"
27 #include "class_linker.h"
28 #include "entrypoints/quick/quick_entrypoints_enum.h"
29 #include "entrypoints/runtime_asm_entrypoints.h"
30 #include "hidden_api.h"
31 #include "jni/jni_internal.h"
32 #include "mirror/class.h"
33 #include "mirror/throwable.h"
34 #include "nativehelper/scoped_local_ref.h"
35 #include "obj_ptr-inl.h"
36 #include "runtime.h"
37 #include "scoped_thread_state_change-inl.h"
38 #include "thread-current-inl.h"
39 
40 namespace art {
41 
42 jclass WellKnownClasses::dalvik_annotation_optimization_CriticalNative;
43 jclass WellKnownClasses::dalvik_annotation_optimization_FastNative;
44 jclass WellKnownClasses::dalvik_system_BaseDexClassLoader;
45 jclass WellKnownClasses::dalvik_system_DelegateLastClassLoader;
46 jclass WellKnownClasses::dalvik_system_DexClassLoader;
47 jclass WellKnownClasses::dalvik_system_DexFile;
48 jclass WellKnownClasses::dalvik_system_DexPathList;
49 jclass WellKnownClasses::dalvik_system_DexPathList__Element;
50 jclass WellKnownClasses::dalvik_system_EmulatedStackFrame;
51 jclass WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
52 jclass WellKnownClasses::dalvik_system_PathClassLoader;
53 jclass WellKnownClasses::dalvik_system_VMRuntime;
54 jclass WellKnownClasses::java_lang_annotation_Annotation__array;
55 jclass WellKnownClasses::java_lang_BootClassLoader;
56 jclass WellKnownClasses::java_lang_ClassLoader;
57 jclass WellKnownClasses::java_lang_ClassNotFoundException;
58 jclass WellKnownClasses::java_lang_Daemons;
59 jclass WellKnownClasses::java_lang_Error;
60 jclass WellKnownClasses::java_lang_IllegalAccessError;
61 jclass WellKnownClasses::java_lang_NoClassDefFoundError;
62 jclass WellKnownClasses::java_lang_Object;
63 jclass WellKnownClasses::java_lang_OutOfMemoryError;
64 jclass WellKnownClasses::java_lang_reflect_Parameter;
65 jclass WellKnownClasses::java_lang_reflect_Parameter__array;
66 jclass WellKnownClasses::java_lang_reflect_Proxy;
67 jclass WellKnownClasses::java_lang_RuntimeException;
68 jclass WellKnownClasses::java_lang_StackOverflowError;
69 jclass WellKnownClasses::java_lang_String;
70 jclass WellKnownClasses::java_lang_StringFactory;
71 jclass WellKnownClasses::java_lang_System;
72 jclass WellKnownClasses::java_lang_Thread;
73 jclass WellKnownClasses::java_lang_ThreadGroup;
74 jclass WellKnownClasses::java_lang_Throwable;
75 jclass WellKnownClasses::java_nio_ByteBuffer;
76 jclass WellKnownClasses::java_nio_DirectByteBuffer;
77 jclass WellKnownClasses::java_util_Collections;
78 jclass WellKnownClasses::java_util_function_Consumer;
79 jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
80 jclass WellKnownClasses::libcore_reflect_AnnotationMember;
81 jclass WellKnownClasses::libcore_util_EmptyArray;
82 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
83 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
84 
85 jmethodID WellKnownClasses::dalvik_system_BaseDexClassLoader_getLdLibraryPath;
86 jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
87 jmethodID WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed;
88 jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
89 jmethodID WellKnownClasses::java_lang_Byte_valueOf;
90 jmethodID WellKnownClasses::java_lang_Character_valueOf;
91 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
92 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
93 jmethodID WellKnownClasses::java_lang_Daemons_start;
94 jmethodID WellKnownClasses::java_lang_Daemons_stop;
95 jmethodID WellKnownClasses::java_lang_Daemons_waitForDaemonStart;
96 jmethodID WellKnownClasses::java_lang_Double_valueOf;
97 jmethodID WellKnownClasses::java_lang_Float_valueOf;
98 jmethodID WellKnownClasses::java_lang_Integer_valueOf;
99 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
100 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor;
101 jmethodID WellKnownClasses::java_lang_Long_valueOf;
102 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
103 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
104 jmethodID WellKnownClasses::java_lang_reflect_Parameter_init;
105 jmethodID WellKnownClasses::java_lang_reflect_Proxy_init;
106 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
107 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
108 jmethodID WellKnownClasses::java_lang_Short_valueOf;
109 jmethodID WellKnownClasses::java_lang_String_charAt;
110 jmethodID WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
111 jmethodID WellKnownClasses::java_lang_Thread_init;
112 jmethodID WellKnownClasses::java_lang_Thread_run;
113 jmethodID WellKnownClasses::java_lang_ThreadGroup_add;
114 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
115 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
116 jmethodID WellKnownClasses::java_util_function_Consumer_accept;
117 jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
118 jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
119 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
120 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
121 
122 jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
123 jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
124 jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
125 jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders;
126 jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
127 jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
128 jfieldID WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer;
129 jfieldID WellKnownClasses::java_lang_Thread_parkBlocker;
130 jfieldID WellKnownClasses::java_lang_Thread_daemon;
131 jfieldID WellKnownClasses::java_lang_Thread_group;
132 jfieldID WellKnownClasses::java_lang_Thread_lock;
133 jfieldID WellKnownClasses::java_lang_Thread_name;
134 jfieldID WellKnownClasses::java_lang_Thread_priority;
135 jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
136 jfieldID WellKnownClasses::java_lang_Thread_systemDaemon;
137 jfieldID WellKnownClasses::java_lang_Thread_unparkedBeforeStart;
138 jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
139 jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
140 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
141 jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
142 jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
143 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
144 jfieldID WellKnownClasses::java_lang_Throwable_cause;
145 jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
146 jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
147 jfieldID WellKnownClasses::java_lang_Throwable_stackState;
148 jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
149 jfieldID WellKnownClasses::java_nio_ByteBuffer_address;
150 jfieldID WellKnownClasses::java_nio_ByteBuffer_hb;
151 jfieldID WellKnownClasses::java_nio_ByteBuffer_isReadOnly;
152 jfieldID WellKnownClasses::java_nio_ByteBuffer_limit;
153 jfieldID WellKnownClasses::java_nio_ByteBuffer_offset;
154 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
155 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
156 jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
157 jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
158 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
159 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
160 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
161 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
162 
CacheClass(JNIEnv * env,const char * jni_class_name)163 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
164   ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
165   if (c.get() == nullptr) {
166     LOG(FATAL) << "Couldn't find class: " << jni_class_name;
167   }
168   return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
169 }
170 
CacheField(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)171 static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
172                            const char* name, const char* signature) {
173   jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) :
174       env->GetFieldID(c, name, signature);
175   if (fid == nullptr) {
176     ScopedObjectAccess soa(env);
177     if (soa.Self()->IsExceptionPending()) {
178       LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
179     }
180     std::ostringstream os;
181     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
182     LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
183                << os.str();
184   }
185   return fid;
186 }
187 
CacheMethod(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)188 static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
189                              const char* name, const char* signature) {
190   jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) :
191       env->GetMethodID(c, name, signature);
192   if (mid == nullptr) {
193     ScopedObjectAccess soa(env);
194     if (soa.Self()->IsExceptionPending()) {
195       LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
196     }
197     std::ostringstream os;
198     WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
199     LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
200                << os.str();
201   }
202   return mid;
203 }
204 
CacheMethod(JNIEnv * env,const char * klass,bool is_static,const char * name,const char * signature)205 static jmethodID CacheMethod(JNIEnv* env, const char* klass, bool is_static,
206                       const char* name, const char* signature) {
207   ScopedLocalRef<jclass> java_class(env, env->FindClass(klass));
208   return CacheMethod(env, java_class.get(), is_static, name, signature);
209 }
210 
CachePrimitiveBoxingMethod(JNIEnv * env,char prim_name,const char * boxed_name)211 static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
212   ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
213   return CacheMethod(env, boxed_class.get(), true, "valueOf",
214                      android::base::StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
215 }
216 
217 #define STRING_INIT_LIST(V) \
218   V(java_lang_String_init, "()V", newEmptyString, "newEmptyString", "()Ljava/lang/String;", NewEmptyString) \
219   V(java_lang_String_init_B, "([B)V", newStringFromBytes_B, "newStringFromBytes", "([B)Ljava/lang/String;", NewStringFromBytes_B) \
220   V(java_lang_String_init_BI, "([BI)V", newStringFromBytes_BI, "newStringFromBytes", "([BI)Ljava/lang/String;", NewStringFromBytes_BI) \
221   V(java_lang_String_init_BII, "([BII)V", newStringFromBytes_BII, "newStringFromBytes", "([BII)Ljava/lang/String;", NewStringFromBytes_BII) \
222   V(java_lang_String_init_BIII, "([BIII)V", newStringFromBytes_BIII, "newStringFromBytes", "([BIII)Ljava/lang/String;", NewStringFromBytes_BIII) \
223   V(java_lang_String_init_BIIString, "([BIILjava/lang/String;)V", newStringFromBytes_BIIString, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BIIString) \
224   V(java_lang_String_init_BString, "([BLjava/lang/String;)V", newStringFromBytes_BString, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BString) \
225   V(java_lang_String_init_BIICharset, "([BIILjava/nio/charset/Charset;)V", newStringFromBytes_BIICharset, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BIICharset) \
226   V(java_lang_String_init_BCharset, "([BLjava/nio/charset/Charset;)V", newStringFromBytes_BCharset, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BCharset) \
227   V(java_lang_String_init_C, "([C)V", newStringFromChars_C, "newStringFromChars", "([C)Ljava/lang/String;", NewStringFromChars_C) \
228   V(java_lang_String_init_CII, "([CII)V", newStringFromChars_CII, "newStringFromChars", "([CII)Ljava/lang/String;", NewStringFromChars_CII) \
229   V(java_lang_String_init_IIC, "(II[C)V", newStringFromChars_IIC, "newStringFromChars", "(II[C)Ljava/lang/String;", NewStringFromChars_IIC) \
230   V(java_lang_String_init_String, "(Ljava/lang/String;)V", newStringFromString, "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;", NewStringFromString) \
231   V(java_lang_String_init_StringBuffer, "(Ljava/lang/StringBuffer;)V", newStringFromStringBuffer, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;", NewStringFromStringBuffer) \
232   V(java_lang_String_init_III, "([III)V", newStringFromCodePoints, "newStringFromCodePoints", "([III)Ljava/lang/String;", NewStringFromCodePoints) \
233   V(java_lang_String_init_StringBuilder, "(Ljava/lang/StringBuilder;)V", newStringFromStringBuilder, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;", NewStringFromStringBuilder) \
234 
235 #define STATIC_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, ...) \
236     static ArtMethod* init_runtime_name = nullptr; \
237     static ArtMethod* new_runtime_name = nullptr;
STRING_INIT_LIST(STATIC_STRING_INIT)238     STRING_INIT_LIST(STATIC_STRING_INIT)
239 #undef STATIC_STRING_INIT
240 
241 void WellKnownClasses::InitStringInit(ObjPtr<mirror::Class> string_class,
242                                       ObjPtr<mirror::Class> string_builder_class) {
243   PointerSize p_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
244   auto find_method = [p_size](ObjPtr<mirror::Class> klass,
245                               const char* name,
246                               const char* sig,
247                               bool expext_static) REQUIRES_SHARED(Locks::mutator_lock_) {
248     ArtMethod* ret = klass->FindClassMethod(name, sig, p_size);
249     CHECK(ret != nullptr);
250     CHECK_EQ(expext_static, ret->IsStatic());
251     return ret;
252   };
253 
254   #define LOAD_STRING_INIT(init_runtime_name, init_signature, new_runtime_name,                  \
255                            new_java_name, new_signature, ...)                                    \
256       init_runtime_name = find_method(string_class, "<init>", init_signature, false);            \
257       new_runtime_name = find_method(string_builder_class, new_java_name, new_signature, true);
258       STRING_INIT_LIST(LOAD_STRING_INIT)
259   #undef LOAD_STRING_INIT
260 }
261 
InitStringEntryPoints()262 void Thread::InitStringEntryPoints() {
263   QuickEntryPoints* qpoints = &tlsPtr_.quick_entrypoints;
264   #define SET_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name,              \
265                           new_java_name, new_signature, entry_point_name)                   \
266       DCHECK(!Runtime::Current()->IsStarted() || (new_runtime_name) != nullptr);            \
267       qpoints->p ## entry_point_name = reinterpret_cast<void(*)()>(new_runtime_name);
268       STRING_INIT_LIST(SET_ENTRY_POINT)
269   #undef SET_ENTRY_POINT
270 }
271 
StringInitToStringFactory(ArtMethod * string_init)272 ArtMethod* WellKnownClasses::StringInitToStringFactory(ArtMethod* string_init) {
273   #define TO_STRING_FACTORY(init_runtime_name, init_signature, new_runtime_name,            \
274                             new_java_name, new_signature, entry_point_name)                 \
275       DCHECK((init_runtime_name) != nullptr);                                               \
276       if (string_init == (init_runtime_name)) {                                             \
277         DCHECK((new_runtime_name) != nullptr);                                              \
278         return (new_runtime_name);                                                          \
279       }
280       STRING_INIT_LIST(TO_STRING_FACTORY)
281   #undef TO_STRING_FACTORY
282   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
283   UNREACHABLE();
284 }
285 
StringInitToEntryPoint(ArtMethod * string_init)286 uint32_t WellKnownClasses::StringInitToEntryPoint(ArtMethod* string_init) {
287   #define TO_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name,               \
288                          new_java_name, new_signature, entry_point_name)                    \
289       if (string_init == (init_runtime_name)) {                                             \
290         return kQuick ## entry_point_name;                                                  \
291       }
292       STRING_INIT_LIST(TO_ENTRY_POINT)
293   #undef TO_ENTRY_POINT
294   LOG(FATAL) << "Could not find StringFactory method for String.<init>";
295   UNREACHABLE();
296 }
297 #undef STRING_INIT_LIST
298 
Init(JNIEnv * env)299 void WellKnownClasses::Init(JNIEnv* env) {
300   hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
301       hiddenapi::EnforcementPolicy::kDisabled);
302 
303   dalvik_annotation_optimization_CriticalNative =
304       CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
305   dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");
306   dalvik_system_BaseDexClassLoader = CacheClass(env, "dalvik/system/BaseDexClassLoader");
307   dalvik_system_DelegateLastClassLoader = CacheClass(env, "dalvik/system/DelegateLastClassLoader");
308   dalvik_system_DexClassLoader = CacheClass(env, "dalvik/system/DexClassLoader");
309   dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
310   dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
311   dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
312   dalvik_system_EmulatedStackFrame = CacheClass(env, "dalvik/system/EmulatedStackFrame");
313   dalvik_system_InMemoryDexClassLoader = CacheClass(env, "dalvik/system/InMemoryDexClassLoader");
314   dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
315   dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
316 
317   java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
318   java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
319   java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
320   java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
321   java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
322   java_lang_Object = CacheClass(env, "java/lang/Object");
323   java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
324   java_lang_Error = CacheClass(env, "java/lang/Error");
325   java_lang_IllegalAccessError = CacheClass(env, "java/lang/IllegalAccessError");
326   java_lang_NoClassDefFoundError = CacheClass(env, "java/lang/NoClassDefFoundError");
327   java_lang_reflect_Parameter = CacheClass(env, "java/lang/reflect/Parameter");
328   java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
329   java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
330   java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
331   java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
332   java_lang_String = CacheClass(env, "java/lang/String");
333   java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
334   java_lang_System = CacheClass(env, "java/lang/System");
335   java_lang_Thread = CacheClass(env, "java/lang/Thread");
336   java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
337   java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
338   java_nio_ByteBuffer = CacheClass(env, "java/nio/ByteBuffer");
339   java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
340   java_util_Collections = CacheClass(env, "java/util/Collections");
341   java_util_function_Consumer = CacheClass(env, "java/util/function/Consumer");
342   libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
343   libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
344   libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
345   org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
346   org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
347 
348   dalvik_system_BaseDexClassLoader_getLdLibraryPath = CacheMethod(env, dalvik_system_BaseDexClassLoader, false, "getLdLibraryPath", "()Ljava/lang/String;");
349   dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
350   dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(env, dalvik_system_VMRuntime, true, "hiddenApiUsed", "(ILjava/lang/String;Ljava/lang/String;IZ)V");
351   java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
352   java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
353 
354   java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
355   java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
356   java_lang_Daemons_waitForDaemonStart = CacheMethod(env, java_lang_Daemons, true, "waitForDaemonStart", "()V");
357   java_lang_invoke_MethodHandles_lookup = CacheMethod(env, "java/lang/invoke/MethodHandles", true, "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;");
358   java_lang_invoke_MethodHandles_Lookup_findConstructor = CacheMethod(env, "java/lang/invoke/MethodHandles$Lookup", false, "findConstructor", "(Ljava/lang/Class;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;");
359 
360   java_lang_ref_FinalizerReference_add = CacheMethod(env, "java/lang/ref/FinalizerReference", true, "add", "(Ljava/lang/Object;)V");
361   java_lang_ref_ReferenceQueue_add = CacheMethod(env, "java/lang/ref/ReferenceQueue", true, "add", "(Ljava/lang/ref/Reference;)V");
362 
363   java_lang_reflect_Parameter_init = CacheMethod(env, java_lang_reflect_Parameter, false, "<init>", "(Ljava/lang/String;ILjava/lang/reflect/Executable;I)V");
364   java_lang_String_charAt = CacheMethod(env, java_lang_String, false, "charAt", "(I)C");
365   java_lang_Thread_dispatchUncaughtException = CacheMethod(env, java_lang_Thread, false, "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
366   java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
367   java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
368   java_lang_ThreadGroup_add = CacheMethod(env, java_lang_ThreadGroup, false, "add", "(Ljava/lang/Thread;)V");
369   java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
370   java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
371   java_util_function_Consumer_accept = CacheMethod(env, java_util_function_Consumer, false, "accept", "(Ljava/lang/Object;)V");
372   libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
373   libcore_reflect_AnnotationMember_init = CacheMethod(env, libcore_reflect_AnnotationMember, false, "<init>", "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;Ljava/lang/reflect/Method;)V");
374   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
375   org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "dispatch", "(I[BII)Lorg/apache/harmony/dalvik/ddmc/Chunk;");
376 
377   dalvik_system_BaseDexClassLoader_pathList = CacheField(env, dalvik_system_BaseDexClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
378   dalvik_system_BaseDexClassLoader_sharedLibraryLoaders = CacheField(env, dalvik_system_BaseDexClassLoader, false, "sharedLibraryLoaders", "[Ljava/lang/ClassLoader;");
379   dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
380   dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
381   dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
382   dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
383   dalvik_system_VMRuntime_nonSdkApiUsageConsumer = CacheField(env, dalvik_system_VMRuntime, true, "nonSdkApiUsageConsumer", "Ljava/util/function/Consumer;");
384   java_lang_Thread_parkBlocker = CacheField(env, java_lang_Thread, false, "parkBlocker", "Ljava/lang/Object;");
385   java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
386   java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
387   java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
388   java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
389   java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
390   java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
391   java_lang_Thread_systemDaemon = CacheField(env, java_lang_Thread, false, "systemDaemon", "Z");
392   java_lang_Thread_unparkedBeforeStart = CacheField(env, java_lang_Thread, false, "unparkedBeforeStart", "Z");
393   java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
394   java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
395   java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
396   java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
397   java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
398   java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
399   java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
400   java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
401   java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
402   java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
403   java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
404   java_nio_ByteBuffer_address = CacheField(env, java_nio_ByteBuffer, false, "address", "J");
405   java_nio_ByteBuffer_hb = CacheField(env, java_nio_ByteBuffer, false, "hb", "[B");
406   java_nio_ByteBuffer_isReadOnly = CacheField(env, java_nio_ByteBuffer, false, "isReadOnly", "Z");
407   java_nio_ByteBuffer_limit = CacheField(env, java_nio_ByteBuffer, false, "limit", "I");
408   java_nio_ByteBuffer_offset = CacheField(env, java_nio_ByteBuffer, false, "offset", "I");
409   java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
410   java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "address", "J");
411   java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
412   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
413   org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
414   org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
415   org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
416   org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
417 
418   java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
419   java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
420   java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
421   java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
422   java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
423   java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
424   java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
425   java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
426 }
427 
LateInit(JNIEnv * env)428 void WellKnownClasses::LateInit(JNIEnv* env) {
429   ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
430   // CacheField and CacheMethod will initialize their classes. Classes below
431   // have clinit sections that call JNI methods. Late init is required
432   // to make sure these JNI methods are available.
433   java_lang_Runtime_nativeLoad =
434       CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
435                   "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Class;)"
436                       "Ljava/lang/String;");
437   java_lang_reflect_Proxy_init =
438     CacheMethod(env, java_lang_reflect_Proxy, false, "<init>",
439                 "(Ljava/lang/reflect/InvocationHandler;)V");
440   // This invariant is important since otherwise we will have the entire proxy invoke system
441   // confused.
442   DCHECK_NE(
443       jni::DecodeArtMethod(java_lang_reflect_Proxy_init)->GetEntryPointFromQuickCompiledCode(),
444       GetQuickInstrumentationEntryPoint());
445   java_lang_reflect_Proxy_invoke =
446     CacheMethod(env, java_lang_reflect_Proxy, true, "invoke",
447                 "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;"
448                     "[Ljava/lang/Object;)Ljava/lang/Object;");
449 }
450 
Clear()451 void WellKnownClasses::Clear() {
452   dalvik_annotation_optimization_CriticalNative = nullptr;
453   dalvik_annotation_optimization_FastNative = nullptr;
454   dalvik_system_BaseDexClassLoader = nullptr;
455   dalvik_system_DelegateLastClassLoader = nullptr;
456   dalvik_system_DexClassLoader = nullptr;
457   dalvik_system_DexFile = nullptr;
458   dalvik_system_DexPathList = nullptr;
459   dalvik_system_DexPathList__Element = nullptr;
460   dalvik_system_EmulatedStackFrame = nullptr;
461   dalvik_system_PathClassLoader = nullptr;
462   dalvik_system_VMRuntime = nullptr;
463   java_lang_annotation_Annotation__array = nullptr;
464   java_lang_BootClassLoader = nullptr;
465   java_lang_ClassLoader = nullptr;
466   java_lang_ClassNotFoundException = nullptr;
467   java_lang_Daemons = nullptr;
468   java_lang_Error = nullptr;
469   java_lang_IllegalAccessError = nullptr;
470   java_lang_NoClassDefFoundError = nullptr;
471   java_lang_Object = nullptr;
472   java_lang_OutOfMemoryError = nullptr;
473   java_lang_reflect_Parameter = nullptr;
474   java_lang_reflect_Parameter__array = nullptr;
475   java_lang_reflect_Proxy = nullptr;
476   java_lang_RuntimeException = nullptr;
477   java_lang_StackOverflowError = nullptr;
478   java_lang_String = nullptr;
479   java_lang_StringFactory = nullptr;
480   java_lang_System = nullptr;
481   java_lang_Thread = nullptr;
482   java_lang_ThreadGroup = nullptr;
483   java_lang_Throwable = nullptr;
484   java_util_Collections = nullptr;
485   java_nio_ByteBuffer = nullptr;
486   java_nio_DirectByteBuffer = nullptr;
487   libcore_reflect_AnnotationFactory = nullptr;
488   libcore_reflect_AnnotationMember = nullptr;
489   libcore_util_EmptyArray = nullptr;
490   org_apache_harmony_dalvik_ddmc_Chunk = nullptr;
491   org_apache_harmony_dalvik_ddmc_DdmServer = nullptr;
492 
493   dalvik_system_BaseDexClassLoader_getLdLibraryPath = nullptr;
494   dalvik_system_VMRuntime_runFinalization = nullptr;
495   dalvik_system_VMRuntime_hiddenApiUsed = nullptr;
496   java_lang_Boolean_valueOf = nullptr;
497   java_lang_Byte_valueOf = nullptr;
498   java_lang_Character_valueOf = nullptr;
499   java_lang_ClassLoader_loadClass = nullptr;
500   java_lang_ClassNotFoundException_init = nullptr;
501   java_lang_Daemons_start = nullptr;
502   java_lang_Daemons_stop = nullptr;
503   java_lang_Double_valueOf = nullptr;
504   java_lang_Float_valueOf = nullptr;
505   java_lang_Integer_valueOf = nullptr;
506   java_lang_invoke_MethodHandles_lookup = nullptr;
507   java_lang_invoke_MethodHandles_Lookup_findConstructor = nullptr;
508   java_lang_Long_valueOf = nullptr;
509   java_lang_ref_FinalizerReference_add = nullptr;
510   java_lang_ref_ReferenceQueue_add = nullptr;
511   java_lang_reflect_Parameter_init = nullptr;
512   java_lang_reflect_Proxy_init = nullptr;
513   java_lang_reflect_Proxy_invoke = nullptr;
514   java_lang_Runtime_nativeLoad = nullptr;
515   java_lang_Short_valueOf = nullptr;
516   java_lang_String_charAt = nullptr;
517   java_lang_Thread_dispatchUncaughtException = nullptr;
518   java_lang_Thread_init = nullptr;
519   java_lang_Thread_run = nullptr;
520   java_lang_ThreadGroup_add = nullptr;
521   java_lang_ThreadGroup_removeThread = nullptr;
522   java_nio_DirectByteBuffer_init = nullptr;
523   libcore_reflect_AnnotationFactory_createAnnotation = nullptr;
524   libcore_reflect_AnnotationMember_init = nullptr;
525   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = nullptr;
526   org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = nullptr;
527 
528   dalvik_system_BaseDexClassLoader_pathList = nullptr;
529   dalvik_system_DexFile_cookie = nullptr;
530   dalvik_system_DexFile_fileName = nullptr;
531   dalvik_system_DexPathList_dexElements = nullptr;
532   dalvik_system_DexPathList__Element_dexFile = nullptr;
533   dalvik_system_VMRuntime_nonSdkApiUsageConsumer = nullptr;
534   java_lang_Thread_parkBlocker = nullptr;
535   java_lang_Thread_daemon = nullptr;
536   java_lang_Thread_group = nullptr;
537   java_lang_Thread_lock = nullptr;
538   java_lang_Thread_name = nullptr;
539   java_lang_Thread_priority = nullptr;
540   java_lang_Thread_nativePeer = nullptr;
541   java_lang_ThreadGroup_groups = nullptr;
542   java_lang_ThreadGroup_ngroups = nullptr;
543   java_lang_ThreadGroup_mainThreadGroup = nullptr;
544   java_lang_ThreadGroup_name = nullptr;
545   java_lang_ThreadGroup_parent = nullptr;
546   java_lang_ThreadGroup_systemThreadGroup = nullptr;
547   java_lang_Throwable_cause = nullptr;
548   java_lang_Throwable_detailMessage = nullptr;
549   java_lang_Throwable_stackTrace = nullptr;
550   java_lang_Throwable_stackState = nullptr;
551   java_lang_Throwable_suppressedExceptions = nullptr;
552   java_nio_ByteBuffer_address = nullptr;
553   java_nio_ByteBuffer_hb = nullptr;
554   java_nio_ByteBuffer_isReadOnly = nullptr;
555   java_nio_ByteBuffer_limit = nullptr;
556   java_nio_ByteBuffer_offset = nullptr;
557   java_nio_DirectByteBuffer_capacity = nullptr;
558   java_nio_DirectByteBuffer_effectiveDirectAddress = nullptr;
559   java_util_Collections_EMPTY_LIST = nullptr;
560   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = nullptr;
561   org_apache_harmony_dalvik_ddmc_Chunk_data = nullptr;
562   org_apache_harmony_dalvik_ddmc_Chunk_length = nullptr;
563   org_apache_harmony_dalvik_ddmc_Chunk_offset = nullptr;
564   org_apache_harmony_dalvik_ddmc_Chunk_type = nullptr;
565 }
566 
ToClass(jclass global_jclass)567 ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
568   auto ret = ObjPtr<mirror::Class>::DownCast(Thread::Current()->DecodeJObject(global_jclass));
569   DCHECK(!ret.IsNull());
570   return ret;
571 }
572 
573 }  // namespace art
574