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