blob: cf9e321bd6de85f03ec6edfb0d381384f967fe86 [file] [log] [blame]
Elliott Hugheseac76672012-05-24 21:56:51 -07001/*
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
Ian Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Andreas Gampe57943812017-12-06 21:39:13 -080023#include <android-base/logging.h>
24#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080025
Andreas Gampe9abc31e2018-05-17 11:47:09 -070026#include "base/enums.h"
27#include "class_linker.h"
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +010028#include "entrypoints/quick/quick_entrypoints_enum.h"
Alex Light6cae5ea2018-06-07 17:07:02 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Mathew Inwood597d7f62018-03-22 11:36:47 +000030#include "hidden_api.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010031#include "jni/jni_internal.h"
Alex Lightf3677472019-06-26 16:31:53 -070032#include "jni_id_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/class.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/throwable.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070035#include "nativehelper/scoped_local_ref.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070036#include "obj_ptr-inl.h"
Andreas Gampe9abc31e2018-05-17 11:47:09 -070037#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Alex Lightf3677472019-06-26 16:31:53 -070039#include "scoped_thread_state_change.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070040#include "thread-current-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070041
42namespace art {
43
Igor Murashkin367f3dd2016-09-01 17:00:24 -070044jclass WellKnownClasses::dalvik_annotation_optimization_CriticalNative;
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070045jclass WellKnownClasses::dalvik_annotation_optimization_FastNative;
Kevin Jeon55737562021-11-19 20:39:30 +000046jclass WellKnownClasses::dalvik_annotation_optimization_NeverCompile;
Andreas Gampe46aba362016-10-28 14:33:28 -070047jclass WellKnownClasses::dalvik_system_BaseDexClassLoader;
Calin Juravle7865ac72017-06-28 11:03:12 -070048jclass WellKnownClasses::dalvik_system_DelegateLastClassLoader;
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +000049jclass WellKnownClasses::dalvik_system_DexClassLoader;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070050jclass WellKnownClasses::dalvik_system_DexFile;
51jclass WellKnownClasses::dalvik_system_DexPathList;
Andreas Gampec8ccf682014-09-29 20:07:43 -070052jclass WellKnownClasses::dalvik_system_DexPathList__Element;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010053jclass WellKnownClasses::dalvik_system_EmulatedStackFrame;
David Brazdil05909d82018-12-06 16:25:16 +000054jclass WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055jclass WellKnownClasses::dalvik_system_PathClassLoader;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070056jclass WellKnownClasses::dalvik_system_VMRuntime;
Jeff Hao13e748b2015-08-25 20:44:19 +000057jclass WellKnownClasses::java_lang_annotation_Annotation__array;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070058jclass WellKnownClasses::java_lang_BootClassLoader;
Elliott Hugheseac76672012-05-24 21:56:51 -070059jclass WellKnownClasses::java_lang_ClassLoader;
60jclass WellKnownClasses::java_lang_ClassNotFoundException;
61jclass WellKnownClasses::java_lang_Daemons;
62jclass WellKnownClasses::java_lang_Error;
Jeff Haoc92a7a12016-06-06 11:09:20 -070063jclass WellKnownClasses::java_lang_IllegalAccessError;
64jclass WellKnownClasses::java_lang_NoClassDefFoundError;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080065jclass WellKnownClasses::java_lang_Object;
Mathieu Chartiera61894d2015-04-23 16:32:54 -070066jclass WellKnownClasses::java_lang_OutOfMemoryError;
Alex Lightf284a2d2019-06-24 13:30:42 -070067jclass WellKnownClasses::java_lang_reflect_InvocationTargetException;
Neil Fuller79a21e72016-09-09 14:24:51 +010068jclass WellKnownClasses::java_lang_reflect_Parameter;
69jclass WellKnownClasses::java_lang_reflect_Parameter__array;
Elliott Hugheseac76672012-05-24 21:56:51 -070070jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070071jclass WellKnownClasses::java_lang_RuntimeException;
Ian Rogers120f1c72012-09-28 17:17:10 -070072jclass WellKnownClasses::java_lang_StackOverflowError;
Ian Rogersdd157d72014-05-15 14:47:50 -070073jclass WellKnownClasses::java_lang_String;
Jeff Hao848f70a2014-01-15 13:49:50 -080074jclass WellKnownClasses::java_lang_StringFactory;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070075jclass WellKnownClasses::java_lang_System;
Elliott Hugheseac76672012-05-24 21:56:51 -070076jclass WellKnownClasses::java_lang_Thread;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070077jclass WellKnownClasses::java_lang_ThreadGroup;
Elliott Hughesa4f94742012-05-29 16:28:38 -070078jclass WellKnownClasses::java_lang_Throwable;
Orion Hodson2ff0cb12020-08-27 09:04:57 +010079jclass WellKnownClasses::java_lang_Void;
Orion Hodsonbda163d2020-04-01 17:36:26 +010080jclass WellKnownClasses::java_nio_Buffer;
Orion Hodson928033d2018-02-07 05:30:54 +000081jclass WellKnownClasses::java_nio_ByteBuffer;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070082jclass WellKnownClasses::java_nio_DirectByteBuffer;
Andreas Gampe7ea6f792014-07-14 16:21:44 -070083jclass WellKnownClasses::java_util_Collections;
Narayan Kamathf5f1f802018-04-03 15:23:46 +010084jclass WellKnownClasses::java_util_function_Consumer;
Jeff Hao13e748b2015-08-25 20:44:19 +000085jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
86jclass WellKnownClasses::libcore_reflect_AnnotationMember;
Neil Fullerc0f02d42018-06-11 09:49:01 +000087jclass WellKnownClasses::libcore_util_EmptyArray;
Elliott Hugheseac76672012-05-24 21:56:51 -070088jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
89jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
90
Andreas Gampe473191c2017-12-28 16:55:31 -080091jmethodID WellKnownClasses::dalvik_system_BaseDexClassLoader_getLdLibraryPath;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070092jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
Andrei Oneaa2d2bc22019-01-25 16:18:53 +000093jmethodID WellKnownClasses::dalvik_system_VMRuntime_hiddenApiUsed;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
95jmethodID WellKnownClasses::java_lang_Byte_valueOf;
96jmethodID WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070097jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Elliott Hugheseac76672012-05-24 21:56:51 -070099jmethodID WellKnownClasses::java_lang_Daemons_start;
Mathieu Chartiercef50f02014-12-09 17:38:52 -0800100jmethodID WellKnownClasses::java_lang_Daemons_stop;
Alex Lighte0b2ce42019-02-21 19:23:42 +0000101jmethodID WellKnownClasses::java_lang_Daemons_waitForDaemonStart;
Vladimir Markod3e9c622020-08-05 12:20:28 +0100102jmethodID WellKnownClasses::java_lang_Double_doubleToRawLongBits;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103jmethodID WellKnownClasses::java_lang_Double_valueOf;
Vladimir Markod3e9c622020-08-05 12:20:28 +0100104jmethodID WellKnownClasses::java_lang_Float_floatToRawIntBits;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105jmethodID WellKnownClasses::java_lang_Float_valueOf;
106jmethodID WellKnownClasses::java_lang_Integer_valueOf;
Orion Hodsonb08e1152022-01-05 14:29:05 +0000107jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_asType;
Orion Hodson8671f582022-03-09 06:53:48 +0000108jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact;
Orion Hodsonf8db2c32017-07-07 20:07:12 +0100109jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_lookup;
110jmethodID WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111jmethodID WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700112jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
113jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Alex Lightf284a2d2019-06-24 13:30:42 -0700114jmethodID WellKnownClasses::java_lang_reflect_InvocationTargetException_init;
Neil Fuller79a21e72016-09-09 14:24:51 +0100115jmethodID WellKnownClasses::java_lang_reflect_Parameter_init;
Alex Light6cae5ea2018-06-07 17:07:02 -0700116jmethodID WellKnownClasses::java_lang_reflect_Proxy_init;
Brian Carlstromea46f952013-07-30 01:26:50 -0700117jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
Ian Rogersef28b142012-11-30 14:22:18 -0800118jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119jmethodID WellKnownClasses::java_lang_Short_valueOf;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000120jmethodID WellKnownClasses::java_lang_String_charAt;
Tobias Thierer5cbe65d2016-07-22 18:37:59 +0100121jmethodID WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
Elliott Hugheseac76672012-05-24 21:56:51 -0700122jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700123jmethodID WellKnownClasses::java_lang_Thread_run;
Andreas Gampe56776012018-01-26 17:40:55 -0800124jmethodID WellKnownClasses::java_lang_ThreadGroup_add;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700125jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Orion Hodson7285a4b2020-07-10 13:31:35 +0100126jmethodID WellKnownClasses::java_nio_Buffer_isDirect;
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700127jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100128jmethodID WellKnownClasses::java_util_function_Consumer_accept;
Jeff Hao13e748b2015-08-25 20:44:19 +0000129jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
130jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
Elliott Hugheseac76672012-05-24 21:56:51 -0700131jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
132jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
133
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700134jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800135jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
Andreas Gampe46aba362016-10-28 14:33:28 -0700136jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
Nicolas Geoffray80a560c2018-10-26 13:48:51 +0100137jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders;
Brad Stenning9c924e82021-10-11 19:09:00 -0700138jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700139jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700140jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100141jfieldID WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer;
Orion Hodsoneb070f02020-03-10 14:00:18 +0000142jfieldID WellKnownClasses::java_io_FileDescriptor_descriptor;
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100143jfieldID WellKnownClasses::java_lang_ClassLoader_parent;
Charles Munger5cc0e752018-11-09 12:30:46 -0800144jfieldID WellKnownClasses::java_lang_Thread_parkBlocker;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700145jfieldID WellKnownClasses::java_lang_Thread_daemon;
146jfieldID WellKnownClasses::java_lang_Thread_group;
147jfieldID WellKnownClasses::java_lang_Thread_lock;
148jfieldID WellKnownClasses::java_lang_Thread_name;
149jfieldID WellKnownClasses::java_lang_Thread_priority;
Anwar Ghuloum3c50a4b2013-06-21 13:05:23 -0700150jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
Alex Lighte0b2ce42019-02-21 19:23:42 +0000151jfieldID WellKnownClasses::java_lang_Thread_systemDaemon;
Charles Mungeraa31f492018-11-01 18:57:38 +0000152jfieldID WellKnownClasses::java_lang_Thread_unparkedBeforeStart;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200153jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000154jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700155jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
156jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200157jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700158jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
Brian Carlstrom34375312014-09-10 23:10:47 -0700159jfieldID WellKnownClasses::java_lang_Throwable_cause;
160jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
161jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
162jfieldID WellKnownClasses::java_lang_Throwable_stackState;
163jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
Orion Hodson814b9282020-02-19 16:37:11 +0000164jfieldID WellKnownClasses::java_nio_Buffer_address;
Orion Hodsonbda163d2020-04-01 17:36:26 +0100165jfieldID WellKnownClasses::java_nio_Buffer_capacity;
Orion Hodson814b9282020-02-19 16:37:11 +0000166jfieldID WellKnownClasses::java_nio_Buffer_elementSizeShift;
167jfieldID WellKnownClasses::java_nio_Buffer_limit;
168jfieldID WellKnownClasses::java_nio_Buffer_position;
Orion Hodson928033d2018-02-07 05:30:54 +0000169jfieldID WellKnownClasses::java_nio_ByteBuffer_address;
170jfieldID WellKnownClasses::java_nio_ByteBuffer_hb;
171jfieldID WellKnownClasses::java_nio_ByteBuffer_isReadOnly;
172jfieldID WellKnownClasses::java_nio_ByteBuffer_limit;
173jfieldID WellKnownClasses::java_nio_ByteBuffer_offset;
Brian Carlstrom34375312014-09-10 23:10:47 -0700174jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
Neil Fullerc0f02d42018-06-11 09:49:01 +0000175jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
Elliott Hugheseac76672012-05-24 21:56:51 -0700176jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
177jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
178jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
179jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
180
181static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
182 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 if (c.get() == nullptr) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700184 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
185 }
186 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
187}
188
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700189static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
190 const char* name, const char* signature) {
Alex Lightf3677472019-06-26 16:31:53 -0700191 jfieldID fid;
192 {
193 ScopedObjectAccess soa(env);
Alex Lightf3677472019-06-26 16:31:53 -0700194 if (Runtime::Current()->GetJniIdType() != JniIdType::kSwapablePointer) {
195 fid = jni::EncodeArtField</*kEnableIndexIds*/ true>(
196 FindFieldJNI(soa, c, name, signature, is_static));
197 } else {
198 fid = jni::EncodeArtField</*kEnableIndexIds*/ false>(
199 FindFieldJNI(soa, c, name, signature, is_static));
200 }
201 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 if (fid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700203 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700204 if (soa.Self()->IsExceptionPending()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700205 LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700207 std::ostringstream os;
208 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
209 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
210 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700211 }
212 return fid;
213}
214
Orion Hodsonf8db2c32017-07-07 20:07:12 +0100215static jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
216 const char* name, const char* signature) {
Alex Lightf3677472019-06-26 16:31:53 -0700217 jmethodID mid;
218 {
219 ScopedObjectAccess soa(env);
Alex Lightf3677472019-06-26 16:31:53 -0700220 if (Runtime::Current()->GetJniIdType() != JniIdType::kSwapablePointer) {
221 mid = jni::EncodeArtMethod</*kEnableIndexIds*/ true>(
222 FindMethodJNI(soa, c, name, signature, is_static));
223 } else {
224 mid = jni::EncodeArtMethod</*kEnableIndexIds*/ false>(
225 FindMethodJNI(soa, c, name, signature, is_static));
226 }
227 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700228 if (mid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700229 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 if (soa.Self()->IsExceptionPending()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700231 LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700233 std::ostringstream os;
234 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
235 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
236 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700237 }
238 return mid;
239}
240
Orion Hodsonf8db2c32017-07-07 20:07:12 +0100241static jmethodID CacheMethod(JNIEnv* env, const char* klass, bool is_static,
242 const char* name, const char* signature) {
243 ScopedLocalRef<jclass> java_class(env, env->FindClass(klass));
244 return CacheMethod(env, java_class.get(), is_static, name, signature);
245}
246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
248 ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
249 return CacheMethod(env, boxed_class.get(), true, "valueOf",
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800250 android::base::StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251}
252
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100253#define STRING_INIT_LIST(V) \
254 V(java_lang_String_init, "()V", newEmptyString, "newEmptyString", "()Ljava/lang/String;", NewEmptyString) \
255 V(java_lang_String_init_B, "([B)V", newStringFromBytes_B, "newStringFromBytes", "([B)Ljava/lang/String;", NewStringFromBytes_B) \
256 V(java_lang_String_init_BI, "([BI)V", newStringFromBytes_BI, "newStringFromBytes", "([BI)Ljava/lang/String;", NewStringFromBytes_BI) \
257 V(java_lang_String_init_BII, "([BII)V", newStringFromBytes_BII, "newStringFromBytes", "([BII)Ljava/lang/String;", NewStringFromBytes_BII) \
258 V(java_lang_String_init_BIII, "([BIII)V", newStringFromBytes_BIII, "newStringFromBytes", "([BIII)Ljava/lang/String;", NewStringFromBytes_BIII) \
259 V(java_lang_String_init_BIIString, "([BIILjava/lang/String;)V", newStringFromBytes_BIIString, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BIIString) \
260 V(java_lang_String_init_BString, "([BLjava/lang/String;)V", newStringFromBytes_BString, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BString) \
261 V(java_lang_String_init_BIICharset, "([BIILjava/nio/charset/Charset;)V", newStringFromBytes_BIICharset, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BIICharset) \
262 V(java_lang_String_init_BCharset, "([BLjava/nio/charset/Charset;)V", newStringFromBytes_BCharset, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BCharset) \
263 V(java_lang_String_init_C, "([C)V", newStringFromChars_C, "newStringFromChars", "([C)Ljava/lang/String;", NewStringFromChars_C) \
264 V(java_lang_String_init_CII, "([CII)V", newStringFromChars_CII, "newStringFromChars", "([CII)Ljava/lang/String;", NewStringFromChars_CII) \
265 V(java_lang_String_init_IIC, "(II[C)V", newStringFromChars_IIC, "newStringFromChars", "(II[C)Ljava/lang/String;", NewStringFromChars_IIC) \
266 V(java_lang_String_init_String, "(Ljava/lang/String;)V", newStringFromString, "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;", NewStringFromString) \
267 V(java_lang_String_init_StringBuffer, "(Ljava/lang/StringBuffer;)V", newStringFromStringBuffer, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;", NewStringFromStringBuffer) \
268 V(java_lang_String_init_III, "([III)V", newStringFromCodePoints, "newStringFromCodePoints", "([III)Ljava/lang/String;", NewStringFromCodePoints) \
269 V(java_lang_String_init_StringBuilder, "(Ljava/lang/StringBuilder;)V", newStringFromStringBuilder, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;", NewStringFromStringBuilder) \
270
271#define STATIC_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, ...) \
Andreas Gampe9abc31e2018-05-17 11:47:09 -0700272 static ArtMethod* init_runtime_name = nullptr; \
273 static ArtMethod* new_runtime_name = nullptr;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100274 STRING_INIT_LIST(STATIC_STRING_INIT)
275#undef STATIC_STRING_INIT
276
Andreas Gampe9abc31e2018-05-17 11:47:09 -0700277void WellKnownClasses::InitStringInit(ObjPtr<mirror::Class> string_class,
278 ObjPtr<mirror::Class> string_builder_class) {
279 PointerSize p_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
280 auto find_method = [p_size](ObjPtr<mirror::Class> klass,
281 const char* name,
282 const char* sig,
283 bool expext_static) REQUIRES_SHARED(Locks::mutator_lock_) {
284 ArtMethod* ret = klass->FindClassMethod(name, sig, p_size);
285 CHECK(ret != nullptr);
286 CHECK_EQ(expext_static, ret->IsStatic());
287 return ret;
288 };
289
290 #define LOAD_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, \
291 new_java_name, new_signature, ...) \
292 init_runtime_name = find_method(string_class, "<init>", init_signature, false); \
293 new_runtime_name = find_method(string_builder_class, new_java_name, new_signature, true);
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100294 STRING_INIT_LIST(LOAD_STRING_INIT)
295 #undef LOAD_STRING_INIT
296}
297
298void Thread::InitStringEntryPoints() {
299 QuickEntryPoints* qpoints = &tlsPtr_.quick_entrypoints;
Santiago Aboy Solanes6cdabe12022-02-18 15:27:43 +0000300#define SET_ENTRY_POINT(init_runtime_name, \
301 init_signature, \
302 new_runtime_name, \
303 new_java_name, \
304 new_signature, \
305 entry_point_name) \
306 DCHECK_IMPLIES(Runtime::Current()->IsStarted(), (new_runtime_name) != nullptr); \
307 qpoints->p##entry_point_name = reinterpret_cast<void*>(new_runtime_name);
308 STRING_INIT_LIST(SET_ENTRY_POINT)
309#undef SET_ENTRY_POINT
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100310}
311
312ArtMethod* WellKnownClasses::StringInitToStringFactory(ArtMethod* string_init) {
313 #define TO_STRING_FACTORY(init_runtime_name, init_signature, new_runtime_name, \
314 new_java_name, new_signature, entry_point_name) \
Andreas Gampe9abc31e2018-05-17 11:47:09 -0700315 DCHECK((init_runtime_name) != nullptr); \
Andreas Gampeca620d72016-11-08 08:09:33 -0800316 if (string_init == (init_runtime_name)) { \
Andreas Gampe9abc31e2018-05-17 11:47:09 -0700317 DCHECK((new_runtime_name) != nullptr); \
Andreas Gampeca620d72016-11-08 08:09:33 -0800318 return (new_runtime_name); \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100319 }
320 STRING_INIT_LIST(TO_STRING_FACTORY)
321 #undef TO_STRING_FACTORY
322 LOG(FATAL) << "Could not find StringFactory method for String.<init>";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800323 UNREACHABLE();
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100324}
325
326uint32_t WellKnownClasses::StringInitToEntryPoint(ArtMethod* string_init) {
327 #define TO_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name, \
328 new_java_name, new_signature, entry_point_name) \
Andreas Gampeca620d72016-11-08 08:09:33 -0800329 if (string_init == (init_runtime_name)) { \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100330 return kQuick ## entry_point_name; \
331 }
332 STRING_INIT_LIST(TO_ENTRY_POINT)
Victor Chang2f5bfb12017-12-18 19:04:53 +0100333 #undef TO_ENTRY_POINT
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100334 LOG(FATAL) << "Could not find StringFactory method for String.<init>";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800335 UNREACHABLE();
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100336}
337#undef STRING_INIT_LIST
338
Brian Carlstromea46f952013-07-30 01:26:50 -0700339void WellKnownClasses::Init(JNIEnv* env) {
David Brazdil32bde992018-05-14 15:24:34 +0100340 hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
David Brazdilf50ac102018-10-17 18:00:06 +0100341 hiddenapi::EnforcementPolicy::kDisabled);
David Brazdila5efc4c2018-02-13 10:01:31 -0800342
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700343 dalvik_annotation_optimization_CriticalNative =
344 CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700345 dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");
Kevin Jeon55737562021-11-19 20:39:30 +0000346 dalvik_annotation_optimization_NeverCompile =
347 CacheClass(env, "dalvik/annotation/optimization/NeverCompile");
Andreas Gampe46aba362016-10-28 14:33:28 -0700348 dalvik_system_BaseDexClassLoader = CacheClass(env, "dalvik/system/BaseDexClassLoader");
Calin Juravle7865ac72017-06-28 11:03:12 -0700349 dalvik_system_DelegateLastClassLoader = CacheClass(env, "dalvik/system/DelegateLastClassLoader");
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +0000350 dalvik_system_DexClassLoader = CacheClass(env, "dalvik/system/DexClassLoader");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700351 dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
352 dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700353 dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100354 dalvik_system_EmulatedStackFrame = CacheClass(env, "dalvik/system/EmulatedStackFrame");
David Brazdil05909d82018-12-06 16:25:16 +0000355 dalvik_system_InMemoryDexClassLoader = CacheClass(env, "dalvik/system/InMemoryDexClassLoader");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700357 dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
358
Jeff Hao13e748b2015-08-25 20:44:19 +0000359 java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700360 java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
Elliott Hugheseac76672012-05-24 21:56:51 -0700361 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
362 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
363 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800364 java_lang_Object = CacheClass(env, "java/lang/Object");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700365 java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
Elliott Hugheseac76672012-05-24 21:56:51 -0700366 java_lang_Error = CacheClass(env, "java/lang/Error");
Jeff Haoc92a7a12016-06-06 11:09:20 -0700367 java_lang_IllegalAccessError = CacheClass(env, "java/lang/IllegalAccessError");
368 java_lang_NoClassDefFoundError = CacheClass(env, "java/lang/NoClassDefFoundError");
Alex Lightf284a2d2019-06-24 13:30:42 -0700369 java_lang_reflect_InvocationTargetException = CacheClass(env, "java/lang/reflect/InvocationTargetException");
Neil Fuller79a21e72016-09-09 14:24:51 +0100370 java_lang_reflect_Parameter = CacheClass(env, "java/lang/reflect/Parameter");
371 java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
Elliott Hugheseac76672012-05-24 21:56:51 -0700372 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700373 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Ian Rogers120f1c72012-09-28 17:17:10 -0700374 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
Ian Rogersdd157d72014-05-15 14:47:50 -0700375 java_lang_String = CacheClass(env, "java/lang/String");
Jeff Hao848f70a2014-01-15 13:49:50 -0800376 java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700377 java_lang_System = CacheClass(env, "java/lang/System");
Elliott Hugheseac76672012-05-24 21:56:51 -0700378 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700379 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700380 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Orion Hodson2ff0cb12020-08-27 09:04:57 +0100381 java_lang_Void = CacheClass(env, "java/lang/Void");
Orion Hodsonbda163d2020-04-01 17:36:26 +0100382 java_nio_Buffer = CacheClass(env, "java/nio/Buffer");
Orion Hodson928033d2018-02-07 05:30:54 +0000383 java_nio_ByteBuffer = CacheClass(env, "java/nio/ByteBuffer");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700384 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700385 java_util_Collections = CacheClass(env, "java/util/Collections");
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100386 java_util_function_Consumer = CacheClass(env, "java/util/function/Consumer");
Jeff Hao13e748b2015-08-25 20:44:19 +0000387 libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
388 libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
Neil Fullerc0f02d42018-06-11 09:49:01 +0000389 libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
Elliott Hugheseac76672012-05-24 21:56:51 -0700390 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
391 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
392
Alex Lightf3677472019-06-26 16:31:53 -0700393 InitFieldsAndMethodsOnly(env);
394}
395
396void WellKnownClasses::InitFieldsAndMethodsOnly(JNIEnv* env) {
397 hiddenapi::ScopedHiddenApiEnforcementPolicySetting hiddenapi_exemption(
398 hiddenapi::EnforcementPolicy::kDisabled);
399
Andreas Gampe473191c2017-12-28 16:55:31 -0800400 dalvik_system_BaseDexClassLoader_getLdLibraryPath = CacheMethod(env, dalvik_system_BaseDexClassLoader, false, "getLdLibraryPath", "()Ljava/lang/String;");
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700401 dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
Andrei Onea6ad020d2019-02-18 12:15:51 +0000402 dalvik_system_VMRuntime_hiddenApiUsed = CacheMethod(env, dalvik_system_VMRuntime, true, "hiddenApiUsed", "(ILjava/lang/String;Ljava/lang/String;IZ)V");
Orion Hodson814b9282020-02-19 16:37:11 +0000403
Elliott Hugheseac76672012-05-24 21:56:51 -0700404 java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700405 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700406
Elliott Hugheseac76672012-05-24 21:56:51 -0700407 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Mathieu Chartiercef50f02014-12-09 17:38:52 -0800408 java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
Alex Lighte0b2ce42019-02-21 19:23:42 +0000409 java_lang_Daemons_waitForDaemonStart = CacheMethod(env, java_lang_Daemons, true, "waitForDaemonStart", "()V");
Orion Hodsonb08e1152022-01-05 14:29:05 +0000410 java_lang_invoke_MethodHandle_asType = CacheMethod(env, "java/lang/invoke/MethodHandle", false, "asType", "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;");
Orion Hodson8671f582022-03-09 06:53:48 +0000411 java_lang_invoke_MethodHandle_invokeExact = CacheMethod(env, "java/lang/invoke/MethodHandle", false, "invokeExact", "([Ljava/lang/Object;)Ljava/lang/Object;");
Orion Hodsonf8db2c32017-07-07 20:07:12 +0100412 java_lang_invoke_MethodHandles_lookup = CacheMethod(env, "java/lang/invoke/MethodHandles", true, "lookup", "()Ljava/lang/invoke/MethodHandles$Lookup;");
413 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;");
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100414
Orion Hodsonf8db2c32017-07-07 20:07:12 +0100415 java_lang_ref_FinalizerReference_add = CacheMethod(env, "java/lang/ref/FinalizerReference", true, "add", "(Ljava/lang/Object;)V");
416 java_lang_ref_ReferenceQueue_add = CacheMethod(env, "java/lang/ref/ReferenceQueue", true, "add", "(Ljava/lang/ref/Reference;)V");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700417
Alex Lightf284a2d2019-06-24 13:30:42 -0700418 java_lang_reflect_InvocationTargetException_init = CacheMethod(env, java_lang_reflect_InvocationTargetException, false, "<init>", "(Ljava/lang/Throwable;)V");
Neil Fuller79a21e72016-09-09 14:24:51 +0100419 java_lang_reflect_Parameter_init = CacheMethod(env, java_lang_reflect_Parameter, false, "<init>", "(Ljava/lang/String;ILjava/lang/reflect/Executable;I)V");
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000420 java_lang_String_charAt = CacheMethod(env, java_lang_String, false, "charAt", "(I)C");
Tobias Thierer5cbe65d2016-07-22 18:37:59 +0100421 java_lang_Thread_dispatchUncaughtException = CacheMethod(env, java_lang_Thread, false, "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700422 java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700423 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
Andreas Gampe56776012018-01-26 17:40:55 -0800424 java_lang_ThreadGroup_add = CacheMethod(env, java_lang_ThreadGroup, false, "add", "(Ljava/lang/Thread;)V");
Piotr Jastrzebskie6cd3b52015-05-07 09:41:00 +0100425 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
Orion Hodson7285a4b2020-07-10 13:31:35 +0100426 java_nio_Buffer_isDirect = CacheMethod(env, java_nio_Buffer, false, "isDirect", "()Z");
Elliott Hughesb5681212013-03-29 17:29:22 -0700427 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100428 java_util_function_Consumer_accept = CacheMethod(env, java_util_function_Consumer, false, "accept", "(Ljava/lang/Object;)V");
Jeff Hao13e748b2015-08-25 20:44:19 +0000429 libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
430 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");
Elliott Hugheseac76672012-05-24 21:56:51 -0700431 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
432 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;");
433
Andreas Gampe46aba362016-10-28 14:33:28 -0700434 dalvik_system_BaseDexClassLoader_pathList = CacheField(env, dalvik_system_BaseDexClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
Nicolas Geoffray80a560c2018-10-26 13:48:51 +0100435 dalvik_system_BaseDexClassLoader_sharedLibraryLoaders = CacheField(env, dalvik_system_BaseDexClassLoader, false, "sharedLibraryLoaders", "[Ljava/lang/ClassLoader;");
Brad Stenning9c924e82021-10-11 19:09:00 -0700436 dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter = CacheField(env, dalvik_system_BaseDexClassLoader, false, "sharedLibraryLoadersAfter", "[Ljava/lang/ClassLoader;");
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800437 dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800438 dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700439 dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700440 dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100441 dalvik_system_VMRuntime_nonSdkApiUsageConsumer = CacheField(env, dalvik_system_VMRuntime, true, "nonSdkApiUsageConsumer", "Ljava/util/function/Consumer;");
Orion Hodson814b9282020-02-19 16:37:11 +0000442
Orion Hodsoneb070f02020-03-10 14:00:18 +0000443 ScopedLocalRef<jclass> java_io_FileDescriptor(env, env->FindClass("java/io/FileDescriptor"));
444 java_io_FileDescriptor_descriptor = CacheField(env, java_io_FileDescriptor.get(), false, "descriptor", "I");
Orion Hodsoneb070f02020-03-10 14:00:18 +0000445
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100446 java_lang_ClassLoader_parent =
447 CacheField(env, java_lang_ClassLoader, false, "parent", "Ljava/lang/ClassLoader;");
448 java_lang_Thread_parkBlocker =
449 CacheField(env, java_lang_Thread, false, "parkBlocker", "Ljava/lang/Object;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700450 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100451 java_lang_Thread_group =
452 CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
Elliott Hughesc5dc2ff2013-01-09 13:44:30 -0800453 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700454 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
455 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800456 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
Alex Lighte0b2ce42019-02-21 19:23:42 +0000457 java_lang_Thread_systemDaemon = CacheField(env, java_lang_Thread, false, "systemDaemon", "Z");
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100458 java_lang_Thread_unparkedBeforeStart =
459 CacheField(env, java_lang_Thread, false, "unparkedBeforeStart", "Z");
460 java_lang_ThreadGroup_groups =
461 CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000462 java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100463 java_lang_ThreadGroup_mainThreadGroup =
464 CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
465 java_lang_ThreadGroup_name =
466 CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
467 java_lang_ThreadGroup_parent =
468 CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
469 java_lang_ThreadGroup_systemThreadGroup =
470 CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
471 java_lang_Throwable_cause =
472 CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
473 java_lang_Throwable_detailMessage =
474 CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
475 java_lang_Throwable_stackTrace =
476 CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
477 java_lang_Throwable_stackState =
478 CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
479 java_lang_Throwable_suppressedExceptions =
480 CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
Orion Hodson814b9282020-02-19 16:37:11 +0000481
Orion Hodsonbda163d2020-04-01 17:36:26 +0100482 java_nio_Buffer_address = CacheField(env, java_nio_Buffer, false, "address", "J");
483 java_nio_Buffer_capacity = CacheField(env, java_nio_Buffer, false, "capacity", "I");
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100484 java_nio_Buffer_elementSizeShift =
485 CacheField(env, java_nio_Buffer, false, "_elementSizeShift", "I");
Orion Hodsonbda163d2020-04-01 17:36:26 +0100486 java_nio_Buffer_limit = CacheField(env, java_nio_Buffer, false, "limit", "I");
487 java_nio_Buffer_position = CacheField(env, java_nio_Buffer, false, "position", "I");
Orion Hodson814b9282020-02-19 16:37:11 +0000488
Orion Hodson928033d2018-02-07 05:30:54 +0000489 java_nio_ByteBuffer_address = CacheField(env, java_nio_ByteBuffer, false, "address", "J");
490 java_nio_ByteBuffer_hb = CacheField(env, java_nio_ByteBuffer, false, "hb", "[B");
491 java_nio_ByteBuffer_isReadOnly = CacheField(env, java_nio_ByteBuffer, false, "isReadOnly", "Z");
492 java_nio_ByteBuffer_limit = CacheField(env, java_nio_ByteBuffer, false, "limit", "I");
493 java_nio_ByteBuffer_offset = CacheField(env, java_nio_ByteBuffer, false, "offset", "I");
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100494 java_util_Collections_EMPTY_LIST =
495 CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
496 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(
497 env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
498 org_apache_harmony_dalvik_ddmc_Chunk_data =
499 CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
500 org_apache_harmony_dalvik_ddmc_Chunk_length =
501 CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
502 org_apache_harmony_dalvik_ddmc_Chunk_offset =
503 CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
504 org_apache_harmony_dalvik_ddmc_Chunk_type =
505 CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700506
507 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
508 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
509 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
510 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
511 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
512 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
513 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
514 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
Vladimir Markod3e9c622020-08-05 12:20:28 +0100515
516 java_lang_Double_doubleToRawLongBits =
517 CacheMethod(env, "java/lang/Double", /*is_static=*/ true, "doubleToRawLongBits", "(D)J");
518 java_lang_Float_floatToRawIntBits =
519 CacheMethod(env, "java/lang/Float", /*is_static=*/ true, "floatToRawIntBits", "(F)I");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700520}
521
Ian Rogersef28b142012-11-30 14:22:18 -0800522void WellKnownClasses::LateInit(JNIEnv* env) {
Przemyslaw Szczepaniakf11cd292016-08-17 17:46:38 +0100523 // CacheField and CacheMethod will initialize their classes. Classes below
524 // have clinit sections that call JNI methods. Late init is required
525 // to make sure these JNI methods are available.
Orion Hodson814b9282020-02-19 16:37:11 +0000526 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800527 java_lang_Runtime_nativeLoad =
528 CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
Nicolas Geoffray96259f12019-01-18 10:04:51 +0000529 "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/Class;)"
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800530 "Ljava/lang/String;");
Alex Light6cae5ea2018-06-07 17:07:02 -0700531 java_lang_reflect_Proxy_init =
532 CacheMethod(env, java_lang_reflect_Proxy, false, "<init>",
533 "(Ljava/lang/reflect/InvocationHandler;)V");
534 // This invariant is important since otherwise we will have the entire proxy invoke system
535 // confused.
536 DCHECK_NE(
537 jni::DecodeArtMethod(java_lang_reflect_Proxy_init)->GetEntryPointFromQuickCompiledCode(),
538 GetQuickInstrumentationEntryPoint());
Przemyslaw Szczepaniakf11cd292016-08-17 17:46:38 +0100539 java_lang_reflect_Proxy_invoke =
540 CacheMethod(env, java_lang_reflect_Proxy, true, "invoke",
541 "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;"
542 "[Ljava/lang/Object;)Ljava/lang/Object;");
Ian Rogersef28b142012-11-30 14:22:18 -0800543}
544
Alex Lightf3677472019-06-26 16:31:53 -0700545void WellKnownClasses::HandleJniIdTypeChange(JNIEnv* env) {
546 WellKnownClasses::InitFieldsAndMethodsOnly(env);
547 WellKnownClasses::LateInit(env);
548}
549
Andreas Gampedb48a792017-09-08 22:18:30 -0700550void WellKnownClasses::Clear() {
551 dalvik_annotation_optimization_CriticalNative = nullptr;
552 dalvik_annotation_optimization_FastNative = nullptr;
Kevin Jeon55737562021-11-19 20:39:30 +0000553 dalvik_annotation_optimization_NeverCompile = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700554 dalvik_system_BaseDexClassLoader = nullptr;
555 dalvik_system_DelegateLastClassLoader = nullptr;
556 dalvik_system_DexClassLoader = nullptr;
557 dalvik_system_DexFile = nullptr;
558 dalvik_system_DexPathList = nullptr;
559 dalvik_system_DexPathList__Element = nullptr;
560 dalvik_system_EmulatedStackFrame = nullptr;
561 dalvik_system_PathClassLoader = nullptr;
562 dalvik_system_VMRuntime = nullptr;
563 java_lang_annotation_Annotation__array = nullptr;
564 java_lang_BootClassLoader = nullptr;
565 java_lang_ClassLoader = nullptr;
566 java_lang_ClassNotFoundException = nullptr;
567 java_lang_Daemons = nullptr;
568 java_lang_Error = nullptr;
569 java_lang_IllegalAccessError = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700570 java_lang_NoClassDefFoundError = nullptr;
571 java_lang_Object = nullptr;
572 java_lang_OutOfMemoryError = nullptr;
Alex Lightf284a2d2019-06-24 13:30:42 -0700573 java_lang_reflect_InvocationTargetException = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700574 java_lang_reflect_Parameter = nullptr;
575 java_lang_reflect_Parameter__array = nullptr;
576 java_lang_reflect_Proxy = nullptr;
577 java_lang_RuntimeException = nullptr;
578 java_lang_StackOverflowError = nullptr;
579 java_lang_String = nullptr;
580 java_lang_StringFactory = nullptr;
581 java_lang_System = nullptr;
582 java_lang_Thread = nullptr;
583 java_lang_ThreadGroup = nullptr;
584 java_lang_Throwable = nullptr;
Orion Hodson2ff0cb12020-08-27 09:04:57 +0100585 java_lang_Void = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700586 java_util_Collections = nullptr;
Orion Hodsonbda163d2020-04-01 17:36:26 +0100587 java_nio_Buffer = nullptr;
Orion Hodson928033d2018-02-07 05:30:54 +0000588 java_nio_ByteBuffer = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700589 java_nio_DirectByteBuffer = nullptr;
590 libcore_reflect_AnnotationFactory = nullptr;
591 libcore_reflect_AnnotationMember = nullptr;
Neil Fullerc0f02d42018-06-11 09:49:01 +0000592 libcore_util_EmptyArray = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700593 org_apache_harmony_dalvik_ddmc_Chunk = nullptr;
594 org_apache_harmony_dalvik_ddmc_DdmServer = nullptr;
595
Andreas Gampe473191c2017-12-28 16:55:31 -0800596 dalvik_system_BaseDexClassLoader_getLdLibraryPath = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700597 dalvik_system_VMRuntime_runFinalization = nullptr;
Andrei Oneaa2d2bc22019-01-25 16:18:53 +0000598 dalvik_system_VMRuntime_hiddenApiUsed = nullptr;
Orion Hodsoneb070f02020-03-10 14:00:18 +0000599 java_io_FileDescriptor_descriptor = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700600 java_lang_Boolean_valueOf = nullptr;
601 java_lang_Byte_valueOf = nullptr;
602 java_lang_Character_valueOf = nullptr;
603 java_lang_ClassLoader_loadClass = nullptr;
604 java_lang_ClassNotFoundException_init = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700605 java_lang_Daemons_start = nullptr;
606 java_lang_Daemons_stop = nullptr;
Vladimir Markod3e9c622020-08-05 12:20:28 +0100607 java_lang_Double_doubleToRawLongBits = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700608 java_lang_Double_valueOf = nullptr;
Vladimir Markod3e9c622020-08-05 12:20:28 +0100609 java_lang_Float_floatToRawIntBits = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700610 java_lang_Float_valueOf = nullptr;
611 java_lang_Integer_valueOf = nullptr;
Orion Hodsonb08e1152022-01-05 14:29:05 +0000612 java_lang_invoke_MethodHandle_asType = nullptr;
Orion Hodson8671f582022-03-09 06:53:48 +0000613 java_lang_invoke_MethodHandle_invokeExact = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700614 java_lang_invoke_MethodHandles_lookup = nullptr;
615 java_lang_invoke_MethodHandles_Lookup_findConstructor = nullptr;
616 java_lang_Long_valueOf = nullptr;
617 java_lang_ref_FinalizerReference_add = nullptr;
618 java_lang_ref_ReferenceQueue_add = nullptr;
Alex Lightf284a2d2019-06-24 13:30:42 -0700619 java_lang_reflect_InvocationTargetException_init = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700620 java_lang_reflect_Parameter_init = nullptr;
Alex Light6cae5ea2018-06-07 17:07:02 -0700621 java_lang_reflect_Proxy_init = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700622 java_lang_reflect_Proxy_invoke = nullptr;
623 java_lang_Runtime_nativeLoad = nullptr;
624 java_lang_Short_valueOf = nullptr;
625 java_lang_String_charAt = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700626 java_lang_Thread_dispatchUncaughtException = nullptr;
627 java_lang_Thread_init = nullptr;
628 java_lang_Thread_run = nullptr;
Andreas Gampe56776012018-01-26 17:40:55 -0800629 java_lang_ThreadGroup_add = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700630 java_lang_ThreadGroup_removeThread = nullptr;
Orion Hodson7285a4b2020-07-10 13:31:35 +0100631 java_nio_Buffer_isDirect = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700632 java_nio_DirectByteBuffer_init = nullptr;
633 libcore_reflect_AnnotationFactory_createAnnotation = nullptr;
634 libcore_reflect_AnnotationMember_init = nullptr;
635 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = nullptr;
636 org_apache_harmony_dalvik_ddmc_DdmServer_dispatch = nullptr;
637
638 dalvik_system_BaseDexClassLoader_pathList = nullptr;
639 dalvik_system_DexFile_cookie = nullptr;
640 dalvik_system_DexFile_fileName = nullptr;
641 dalvik_system_DexPathList_dexElements = nullptr;
642 dalvik_system_DexPathList__Element_dexFile = nullptr;
Vladimir Marko60976722018-04-06 15:29:38 +0100643 dalvik_system_VMRuntime_nonSdkApiUsageConsumer = nullptr;
Vladimir Markoe300c4e2021-06-08 16:00:05 +0100644 java_lang_ClassLoader_parent = nullptr;
Charles Munger5cc0e752018-11-09 12:30:46 -0800645 java_lang_Thread_parkBlocker = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700646 java_lang_Thread_daemon = nullptr;
647 java_lang_Thread_group = nullptr;
648 java_lang_Thread_lock = nullptr;
649 java_lang_Thread_name = nullptr;
650 java_lang_Thread_priority = nullptr;
651 java_lang_Thread_nativePeer = nullptr;
652 java_lang_ThreadGroup_groups = nullptr;
653 java_lang_ThreadGroup_ngroups = nullptr;
654 java_lang_ThreadGroup_mainThreadGroup = nullptr;
655 java_lang_ThreadGroup_name = nullptr;
656 java_lang_ThreadGroup_parent = nullptr;
657 java_lang_ThreadGroup_systemThreadGroup = nullptr;
658 java_lang_Throwable_cause = nullptr;
659 java_lang_Throwable_detailMessage = nullptr;
660 java_lang_Throwable_stackTrace = nullptr;
661 java_lang_Throwable_stackState = nullptr;
662 java_lang_Throwable_suppressedExceptions = nullptr;
Orion Hodson814b9282020-02-19 16:37:11 +0000663 java_nio_Buffer_address = nullptr;
664 java_nio_Buffer_elementSizeShift = nullptr;
665 java_nio_Buffer_limit = nullptr;
666 java_nio_Buffer_position = nullptr;
Orion Hodson928033d2018-02-07 05:30:54 +0000667 java_nio_ByteBuffer_address = nullptr;
668 java_nio_ByteBuffer_hb = nullptr;
669 java_nio_ByteBuffer_isReadOnly = nullptr;
670 java_nio_ByteBuffer_limit = nullptr;
671 java_nio_ByteBuffer_offset = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700672 java_util_Collections_EMPTY_LIST = nullptr;
Neil Fullerc0f02d42018-06-11 09:49:01 +0000673 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = nullptr;
Andreas Gampedb48a792017-09-08 22:18:30 -0700674 org_apache_harmony_dalvik_ddmc_Chunk_data = nullptr;
675 org_apache_harmony_dalvik_ddmc_Chunk_length = nullptr;
676 org_apache_harmony_dalvik_ddmc_Chunk_offset = nullptr;
677 org_apache_harmony_dalvik_ddmc_Chunk_type = nullptr;
678}
679
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700680ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
Alex Lightd6251582016-10-31 11:12:30 -0700681 auto ret = ObjPtr<mirror::Class>::DownCast(Thread::Current()->DecodeJObject(global_jclass));
682 DCHECK(!ret.IsNull());
683 return ret;
Elliott Hugheseac76672012-05-24 21:56:51 -0700684}
685
Elliott Hugheseac76672012-05-24 21:56:51 -0700686} // namespace art