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_Buffer;
79 jclass WellKnownClasses::java_nio_ByteBuffer;
80 jclass WellKnownClasses::java_nio_DirectByteBuffer;
81 jclass WellKnownClasses::java_util_Collections;
82 jclass WellKnownClasses::java_util_function_Consumer;
83 jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
84 jclass WellKnownClasses::libcore_reflect_AnnotationMember;
85 jclass WellKnownClasses::libcore_util_EmptyArray;
86 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
87 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
88
89 jmethodID WellKnownClasses::dalvik_system_BaseDexClassLoader_getLdLibraryPath;
90 jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
91 jmethodID WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed;
92 jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
93 jmethodID WellKnownClasses::java_lang_Byte_valueOf;
94 jmethodID WellKnownClasses::java_lang_Character_valueOf;
95 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
96 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
97 jmethodID WellKnownClasses::java_lang_Daemons_start;
98 jmethodID WellKnownClasses::java_lang_Daemons_stop;
99 jmethodID WellKnownClasses::java_lang_Daemons_waitForDaemonStart;
100 jmethodID WellKnownClasses::java_lang_Double_valueOf;
101 jmethodID WellKnownClasses::java_lang_Float_valueOf;
102 jmethodID WellKnownClasses::java_lang_Integer_valueOf;
103 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
104 jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor;
105 jmethodID WellKnownClasses::java_lang_Long_valueOf;
106 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
107 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
108 jmethodID WellKnownClasses::java_lang_reflect_InvocationTargetException_init;
109 jmethodID WellKnownClasses::java_lang_reflect_Parameter_init;
110 jmethodID WellKnownClasses::java_lang_reflect_Proxy_init;
111 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
112 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
113 jmethodID WellKnownClasses::java_lang_Short_valueOf;
114 jmethodID WellKnownClasses::java_lang_String_charAt;
115 jmethodID WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
116 jmethodID WellKnownClasses::java_lang_Thread_init;
117 jmethodID WellKnownClasses::java_lang_Thread_run;
118 jmethodID WellKnownClasses::java_lang_ThreadGroup_add;
119 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
120 jmethodID WellKnownClasses::java_nio_Buffer_isDirect;
121 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
122 jmethodID WellKnownClasses::java_util_function_Consumer_accept;
123 jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
124 jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
125 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
126 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
127
128 jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
129 jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
130 jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
131 jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders;
132 jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
133 jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
134 jfieldID WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer;
135 jfieldID WellKnownClasses::java_io_FileDescriptor_descriptor;
136 jfieldID WellKnownClasses::java_lang_Thread_parkBlocker;
137 jfieldID WellKnownClasses::java_lang_Thread_daemon;
138 jfieldID WellKnownClasses::java_lang_Thread_group;
139 jfieldID WellKnownClasses::java_lang_Thread_lock;
140 jfieldID WellKnownClasses::java_lang_Thread_name;
141 jfieldID WellKnownClasses::java_lang_Thread_priority;
142 jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
143 jfieldID WellKnownClasses::java_lang_Thread_systemDaemon;
144 jfieldID WellKnownClasses::java_lang_Thread_unparkedBeforeStart;
145 jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
146 jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
147 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
148 jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
149 jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
150 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
151 jfieldID WellKnownClasses::java_lang_Throwable_cause;
152 jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
153 jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
154 jfieldID WellKnownClasses::java_lang_Throwable_stackState;
155 jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
156 jfieldID WellKnownClasses::java_nio_Buffer_address;
157 jfieldID WellKnownClasses::java_nio_Buffer_capacity;
158 jfieldID WellKnownClasses::java_nio_Buffer_elementSizeShift;
159 jfieldID WellKnownClasses::java_nio_Buffer_limit;
160 jfieldID WellKnownClasses::java_nio_Buffer_position;
161 jfieldID WellKnownClasses::java_nio_ByteBuffer_address;
162 jfieldID WellKnownClasses::java_nio_ByteBuffer_hb;
163 jfieldID WellKnownClasses::java_nio_ByteBuffer_isReadOnly;
164 jfieldID WellKnownClasses::java_nio_ByteBuffer_limit;
165 jfieldID WellKnownClasses::java_nio_ByteBuffer_offset;
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_Buffer = CacheClass(env, "java/nio/Buffer");
368 java_nio_ByteBuffer = CacheClass(env, "java/nio/ByteBuffer");
369 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
370 java_util_Collections = CacheClass(env, "java/util/Collections");
371 java_util_function_Consumer = CacheClass(env, "java/util/function/Consumer");
372 libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
373 libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
374 libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
375 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
376 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
377
378 InitFieldsAndMethodsOnly(env);
379 }
380
InitFieldsAndMethodsOnly(JNIEnv * env)381 void WellKnownClasses::InitFieldsAndMethodsOnly(JNIEnv* env) {
382 hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
383 hiddenapi::EnforcementPolicy::kDisabled);
384
385 dalvik_system_BaseDexClassLoader_getLdLibraryPath = CacheMethod(env, dalvik_system_BaseDexClassLoader, false, "getLdLibraryPath", "()Ljava/lang/String;");
386 dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
387 dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(env, dalvik_system_VMRuntime, true, "hiddenApiUsed", "(ILjava/lang/String;Ljava/lang/String;IZ)V");
388
389 java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
390 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
391
392 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
393 java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
394 java_lang_Daemons_waitForDaemonStart = CacheMethod(env, java_lang_Daemons, true, "waitForDaemonStart", "()V");
395 java_lang_invoke_MethodHandles_lookup = CacheMethod(env, "java/lang/invoke/MethodHandles", true, "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;");
396 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;");
397
398 java_lang_ref_FinalizerReference_add = CacheMethod(env, "java/lang/ref/FinalizerReference", true, "add", "(Ljava/lang/Object;)V");
399 java_lang_ref_ReferenceQueue_add = CacheMethod(env, "java/lang/ref/ReferenceQueue", true, "add", "(Ljava/lang/ref/Reference;)V");
400
401 java_lang_reflect_InvocationTargetException_init = CacheMethod(env, java_lang_reflect_InvocationTargetException, false, "<init>", "(Ljava/lang/Throwable;)V");
402 java_lang_reflect_Parameter_init = CacheMethod(env, java_lang_reflect_Parameter, false, "<init>", "(Ljava/lang/String;ILjava/lang/reflect/Executable;I)V");
403 java_lang_String_charAt = CacheMethod(env, java_lang_String, false, "charAt", "(I)C");
404 java_lang_Thread_dispatchUncaughtException = CacheMethod(env, java_lang_Thread, false, "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
405 java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
406 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
407 java_lang_ThreadGroup_add = CacheMethod(env, java_lang_ThreadGroup, false, "add", "(Ljava/lang/Thread;)V");
408 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
409 java_nio_Buffer_isDirect = CacheMethod(env, java_nio_Buffer, false, "isDirect", "()Z");
410 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
411 java_util_function_Consumer_accept = CacheMethod(env, java_util_function_Consumer, false, "accept", "(Ljava/lang/Object;)V");
412 libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
413 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");
414 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
415 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;");
416
417 dalvik_system_BaseDexClassLoader_pathList = CacheField(env, dalvik_system_BaseDexClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
418 dalvik_system_BaseDexClassLoader_sharedLibraryLoaders = CacheField(env, dalvik_system_BaseDexClassLoader, false, "sharedLibraryLoaders", "[Ljava/lang/ClassLoader;");
419 dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
420 dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
421 dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
422 dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
423 dalvik_system_VMRuntime_nonSdkApiUsageConsumer = CacheField(env, dalvik_system_VMRuntime, true, "nonSdkApiUsageConsumer", "Ljava/util/function/Consumer;");
424
425 ScopedLocalRef<jclass> java_io_FileDescriptor(env, env->FindClass("java/io/FileDescriptor"));
426 java_io_FileDescriptor_descriptor = CacheField(env, java_io_FileDescriptor.get(), false, "descriptor", "I");
427
428 java_lang_Thread_parkBlocker = CacheField(env, java_lang_Thread, false, "parkBlocker", "Ljava/lang/Object;");
429 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
430 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
431 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
432 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
433 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
434 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
435 java_lang_Thread_systemDaemon = CacheField(env, java_lang_Thread, false, "systemDaemon", "Z");
436 java_lang_Thread_unparkedBeforeStart = CacheField(env, java_lang_Thread, false, "unparkedBeforeStart", "Z");
437 java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
438 java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
439 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
440 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
441 java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
442 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
443 java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
444 java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
445 java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
446 java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
447 java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
448
449 java_nio_Buffer_address = CacheField(env, java_nio_Buffer, false, "address", "J");
450 java_nio_Buffer_capacity = CacheField(env, java_nio_Buffer, false, "capacity", "I");
451 java_nio_Buffer_elementSizeShift = CacheField(env, java_nio_Buffer, false, "_elementSizeShift", "I");
452 java_nio_Buffer_limit = CacheField(env, java_nio_Buffer, false, "limit", "I");
453 java_nio_Buffer_position = CacheField(env, java_nio_Buffer, false, "position", "I");
454
455 java_nio_ByteBuffer_address = CacheField(env, java_nio_ByteBuffer, false, "address", "J");
456 java_nio_ByteBuffer_hb = CacheField(env, java_nio_ByteBuffer, false, "hb", "[B");
457 java_nio_ByteBuffer_isReadOnly = CacheField(env, java_nio_ByteBuffer, false, "isReadOnly", "Z");
458 java_nio_ByteBuffer_limit = CacheField(env, java_nio_ByteBuffer, false, "limit", "I");
459 java_nio_ByteBuffer_offset = CacheField(env, java_nio_ByteBuffer, false, "offset", "I");
460 java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
461 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
462 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
463 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
464 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
465 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
466
467 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
468 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
469 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
470 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
471 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
472 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
473 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
474 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
475 }
476
LateInit(JNIEnv * env)477 void WellKnownClasses::LateInit(JNIEnv* env) {
478 // CacheField and CacheMethod will initialize their classes. Classes below
479 // have clinit sections that call JNI methods. Late init is required
480 // to make sure these JNI methods are available.
481 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
482 java_lang_Runtime_nativeLoad =
483 CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
484 "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Class;)"
485 "Ljava/lang/String;");
486 java_lang_reflect_Proxy_init =
487 CacheMethod(env, java_lang_reflect_Proxy, false, "<init>",
488 "(Ljava/lang/reflect/InvocationHandler;)V");
489 // This invariant is important since otherwise we will have the entire proxy invoke system
490 // confused.
491 DCHECK_NE(
492 jni::DecodeArtMethod(java_lang_reflect_Proxy_init)->GetEntryPointFromQuickCompiledCode(),
493 GetQuickInstrumentationEntryPoint());
494 java_lang_reflect_Proxy_invoke =
495 CacheMethod(env, java_lang_reflect_Proxy, true, "invoke",
496 "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;"
497 "[Ljava/lang/Object;)Ljava/lang/Object;");
498 }
499
HandleJniIdTypeChange(JNIEnv * env)500 void WellKnownClasses::HandleJniIdTypeChange(JNIEnv* env) {
501 WellKnownClasses::InitFieldsAndMethodsOnly(env);
502 WellKnownClasses::LateInit(env);
503 }
504
Clear()505 void WellKnownClasses::Clear() {
506 dalvik_annotation_optimization_CriticalNative = nullptr;
507 dalvik_annotation_optimization_FastNative = nullptr;
508 dalvik_system_BaseDexClassLoader = nullptr;
509 dalvik_system_DelegateLastClassLoader = nullptr;
510 dalvik_system_DexClassLoader = nullptr;
511 dalvik_system_DexFile = nullptr;
512 dalvik_system_DexPathList = nullptr;
513 dalvik_system_DexPathList__Element = nullptr;
514 dalvik_system_EmulatedStackFrame = nullptr;
515 dalvik_system_PathClassLoader = nullptr;
516 dalvik_system_VMRuntime = nullptr;
517 java_lang_annotation_Annotation__array = nullptr;
518 java_lang_BootClassLoader = nullptr;
519 java_lang_ClassLoader = nullptr;
520 java_lang_ClassNotFoundException = nullptr;
521 java_lang_Daemons = nullptr;
522 java_lang_Error = nullptr;
523 java_lang_IllegalAccessError = nullptr;
524 java_lang_NoClassDefFoundError = nullptr;
525 java_lang_Object = nullptr;
526 java_lang_OutOfMemoryError = nullptr;
527 java_lang_reflect_InvocationTargetException = nullptr;
528 java_lang_reflect_Parameter = nullptr;
529 java_lang_reflect_Parameter__array = nullptr;
530 java_lang_reflect_Proxy = nullptr;
531 java_lang_RuntimeException = nullptr;
532 java_lang_StackOverflowError = nullptr;
533 java_lang_String = nullptr;
534 java_lang_StringFactory = nullptr;
535 java_lang_System = nullptr;
536 java_lang_Thread = nullptr;
537 java_lang_ThreadGroup = nullptr;
538 java_lang_Throwable = nullptr;
539 java_util_Collections = nullptr;
540 java_nio_Buffer = 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_lang_Boolean_valueOf = nullptr;
554 java_lang_Byte_valueOf = nullptr;
555 java_lang_Character_valueOf = nullptr;
556 java_lang_ClassLoader_loadClass = nullptr;
557 java_lang_ClassNotFoundException_init = nullptr;
558 java_lang_Daemons_start = nullptr;
559 java_lang_Daemons_stop = nullptr;
560 java_lang_Double_valueOf = nullptr;
561 java_lang_Float_valueOf = nullptr;
562 java_lang_Integer_valueOf = nullptr;
563 java_lang_invoke_MethodHandles_lookup = nullptr;
564 java_lang_invoke_MethodHandles_Lookup_findConstructor = nullptr;
565 java_lang_Long_valueOf = nullptr;
566 java_lang_ref_FinalizerReference_add = nullptr;
567 java_lang_ref_ReferenceQueue_add = nullptr;
568 java_lang_reflect_InvocationTargetException_init = nullptr;
569 java_lang_reflect_Parameter_init = nullptr;
570 java_lang_reflect_Proxy_init = nullptr;
571 java_lang_reflect_Proxy_invoke = nullptr;
572 java_lang_Runtime_nativeLoad = nullptr;
573 java_lang_Short_valueOf = nullptr;
574 java_lang_String_charAt = nullptr;
575 java_lang_Thread_dispatchUncaughtException = nullptr;
576 java_lang_Thread_init = nullptr;
577 java_lang_Thread_run = nullptr;
578 java_lang_ThreadGroup_add = nullptr;
579 java_lang_ThreadGroup_removeThread = nullptr;
580 java_nio_Buffer_isDirect = 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_util_Collections_EMPTY_LIST = nullptr;
621 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = nullptr;
622 org_apache_harmony_dalvik_ddmc_Chunk_data = nullptr;
623 org_apache_harmony_dalvik_ddmc_Chunk_length = nullptr;
624 org_apache_harmony_dalvik_ddmc_Chunk_offset = nullptr;
625 org_apache_harmony_dalvik_ddmc_Chunk_type = nullptr;
626 }
627
ToClass(jclass global_jclass)628 ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
629 auto ret = ObjPtr<mirror::Class>::DownCast(Thread::Current()->DecodeJObject(global_jclass));
630 DCHECK(!ret.IsNull());
631 return ret;
632 }
633
634 } // namespace art
635