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