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