blob: 507ea165e524a6f0b484ab2c6390c8c15f61cf1f [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 Gampe46ee31b2016-12-14 10:11:49 -080023#include "android-base/stringprintf.h"
24
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +010026#include "entrypoints/quick/quick_entrypoints_enum.h"
Andreas Gampe13b27842016-11-07 16:48:23 -080027#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/throwable.h"
Mathieu Chartierc4f39252016-10-05 18:32:08 -070030#include "obj_ptr-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "ScopedLocalRef.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070032#include "scoped_thread_state_change-inl.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080033#include "thread-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070034
35namespace art {
36
37jclass WellKnownClasses::com_android_dex_Dex;
Igor Murashkin367f3dd2016-09-01 17:00:24 -070038jclass WellKnownClasses::dalvik_annotation_optimization_CriticalNative;
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070039jclass WellKnownClasses::dalvik_annotation_optimization_FastNative;
Andreas Gampe46aba362016-10-28 14:33:28 -070040jclass WellKnownClasses::dalvik_system_BaseDexClassLoader;
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +000041jclass WellKnownClasses::dalvik_system_DexClassLoader;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070042jclass WellKnownClasses::dalvik_system_DexFile;
43jclass WellKnownClasses::dalvik_system_DexPathList;
Andreas Gampec8ccf682014-09-29 20:07:43 -070044jclass WellKnownClasses::dalvik_system_DexPathList__Element;
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +010045jclass WellKnownClasses::dalvik_system_EmulatedStackFrame;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046jclass WellKnownClasses::dalvik_system_PathClassLoader;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070047jclass WellKnownClasses::dalvik_system_VMRuntime;
Jeff Hao13e748b2015-08-25 20:44:19 +000048jclass WellKnownClasses::java_lang_annotation_Annotation__array;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -070049jclass WellKnownClasses::java_lang_BootClassLoader;
Elliott Hugheseac76672012-05-24 21:56:51 -070050jclass WellKnownClasses::java_lang_ClassLoader;
51jclass WellKnownClasses::java_lang_ClassNotFoundException;
52jclass WellKnownClasses::java_lang_Daemons;
53jclass WellKnownClasses::java_lang_Error;
Jeff Haoc92a7a12016-06-06 11:09:20 -070054jclass WellKnownClasses::java_lang_ExceptionInInitializerError;
Orion Hodson811bd5f2016-12-07 11:35:37 +000055jclass WellKnownClasses::java_lang_invoke_MethodHandle;
Jeff Haoc92a7a12016-06-06 11:09:20 -070056jclass WellKnownClasses::java_lang_IllegalAccessError;
57jclass WellKnownClasses::java_lang_NoClassDefFoundError;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080058jclass WellKnownClasses::java_lang_Object;
Mathieu Chartiera61894d2015-04-23 16:32:54 -070059jclass WellKnownClasses::java_lang_OutOfMemoryError;
Brian Carlstromea46f952013-07-30 01:26:50 -070060jclass WellKnownClasses::java_lang_reflect_Constructor;
Neil Fuller0e844392016-09-08 13:43:31 +010061jclass WellKnownClasses::java_lang_reflect_Executable;
Brian Carlstromea46f952013-07-30 01:26:50 -070062jclass WellKnownClasses::java_lang_reflect_Field;
63jclass WellKnownClasses::java_lang_reflect_Method;
Neil Fuller79a21e72016-09-09 14:24:51 +010064jclass WellKnownClasses::java_lang_reflect_Parameter;
65jclass WellKnownClasses::java_lang_reflect_Parameter__array;
Elliott Hugheseac76672012-05-24 21:56:51 -070066jclass WellKnownClasses::java_lang_reflect_Proxy;
Elliott Hughesa4f94742012-05-29 16:28:38 -070067jclass WellKnownClasses::java_lang_RuntimeException;
Ian Rogers120f1c72012-09-28 17:17:10 -070068jclass WellKnownClasses::java_lang_StackOverflowError;
Ian Rogersdd157d72014-05-15 14:47:50 -070069jclass WellKnownClasses::java_lang_String;
Jeff Hao848f70a2014-01-15 13:49:50 -080070jclass WellKnownClasses::java_lang_StringFactory;
Mathieu Chartier987ccff2013-07-08 11:05:21 -070071jclass WellKnownClasses::java_lang_System;
Elliott Hugheseac76672012-05-24 21:56:51 -070072jclass WellKnownClasses::java_lang_Thread;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -070073jclass WellKnownClasses::java_lang_ThreadGroup;
Elliott Hughesa4f94742012-05-29 16:28:38 -070074jclass WellKnownClasses::java_lang_Throwable;
Elliott Hughesaecb5f32013-03-28 08:27:38 -070075jclass WellKnownClasses::java_nio_DirectByteBuffer;
Sebastien Hertze49e1952014-10-13 11:27:13 +020076jclass WellKnownClasses::java_util_ArrayList;
Andreas Gampe7ea6f792014-07-14 16:21:44 -070077jclass WellKnownClasses::java_util_Collections;
Jeff Hao13e748b2015-08-25 20:44:19 +000078jclass WellKnownClasses::libcore_reflect_AnnotationFactory;
79jclass WellKnownClasses::libcore_reflect_AnnotationMember;
Andreas Gampe7ea6f792014-07-14 16:21:44 -070080jclass WellKnownClasses::libcore_util_EmptyArray;
Elliott Hugheseac76672012-05-24 21:56:51 -070081jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
82jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
83
84jmethodID WellKnownClasses::com_android_dex_Dex_create;
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -070085jmethodID WellKnownClasses::dalvik_system_VMRuntime_runFinalization;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
87jmethodID WellKnownClasses::java_lang_Byte_valueOf;
88jmethodID WellKnownClasses::java_lang_Character_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -070089jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
Elliott Hugheseac76672012-05-24 21:56:51 -070091jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
92jmethodID WellKnownClasses::java_lang_Daemons_start;
Mathieu Chartiercef50f02014-12-09 17:38:52 -080093jmethodID WellKnownClasses::java_lang_Daemons_stop;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094jmethodID WellKnownClasses::java_lang_Double_valueOf;
95jmethodID WellKnownClasses::java_lang_Float_valueOf;
96jmethodID WellKnownClasses::java_lang_Integer_valueOf;
Orion Hodson811bd5f2016-12-07 11:35:37 +000097jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_invoke;
98jmethodID WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099jmethodID WellKnownClasses::java_lang_Long_valueOf;
Elliott Hughesa4f94742012-05-29 16:28:38 -0700100jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
101jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
Neil Fuller79a21e72016-09-09 14:24:51 +0100102jmethodID WellKnownClasses::java_lang_reflect_Parameter_init;
Brian Carlstromea46f952013-07-30 01:26:50 -0700103jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
Ian Rogersef28b142012-11-30 14:22:18 -0800104jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105jmethodID WellKnownClasses::java_lang_Short_valueOf;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000106jmethodID WellKnownClasses::java_lang_String_charAt;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700107jmethodID WellKnownClasses::java_lang_System_runFinalization = nullptr;
Tobias Thierer5cbe65d2016-07-22 18:37:59 +0100108jmethodID WellKnownClasses::java_lang_Thread_dispatchUncaughtException;
Elliott Hugheseac76672012-05-24 21:56:51 -0700109jmethodID WellKnownClasses::java_lang_Thread_init;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700110jmethodID WellKnownClasses::java_lang_Thread_run;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700111jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700112jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
Jeff Hao13e748b2015-08-25 20:44:19 +0000113jmethodID WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation;
114jmethodID WellKnownClasses::libcore_reflect_AnnotationMember_init;
Elliott Hugheseac76672012-05-24 21:56:51 -0700115jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
116jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
117
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700118jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800119jfieldID WellKnownClasses::dalvik_system_DexFile_fileName;
Andreas Gampe46aba362016-10-28 14:33:28 -0700120jfieldID WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList;
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700121jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700122jfieldID WellKnownClasses::dalvik_system_DexPathList__Element_dexFile;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700123jfieldID WellKnownClasses::java_lang_Thread_daemon;
124jfieldID WellKnownClasses::java_lang_Thread_group;
125jfieldID WellKnownClasses::java_lang_Thread_lock;
126jfieldID WellKnownClasses::java_lang_Thread_name;
127jfieldID WellKnownClasses::java_lang_Thread_priority;
Anwar Ghuloum3c50a4b2013-06-21 13:05:23 -0700128jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200129jfieldID WellKnownClasses::java_lang_ThreadGroup_groups;
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000130jfieldID WellKnownClasses::java_lang_ThreadGroup_ngroups;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700131jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
132jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200133jfieldID WellKnownClasses::java_lang_ThreadGroup_parent;
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700134jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
Brian Carlstrom34375312014-09-10 23:10:47 -0700135jfieldID WellKnownClasses::java_lang_Throwable_cause;
136jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
137jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
138jfieldID WellKnownClasses::java_lang_Throwable_stackState;
139jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
Neil Fuller0e844392016-09-08 13:43:31 +0100140jfieldID WellKnownClasses::java_lang_reflect_Executable_artMethod;
Elliott Hugheseac76672012-05-24 21:56:51 -0700141jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700142jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
143jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
Sebastien Hertze49e1952014-10-13 11:27:13 +0200144jfieldID WellKnownClasses::java_util_ArrayList_array;
145jfieldID WellKnownClasses::java_util_ArrayList_size;
Brian Carlstrom34375312014-09-10 23:10:47 -0700146jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
147jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
Elliott Hugheseac76672012-05-24 21:56:51 -0700148jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
149jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
150jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
151jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
152
153static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
154 ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 if (c.get() == nullptr) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700156 LOG(FATAL) << "Couldn't find class: " << jni_class_name;
157 }
158 return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
159}
160
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700161static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static,
162 const char* name, const char* signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700163 jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) :
164 env->GetFieldID(c, name, signature);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700165 if (fid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700166 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700167 if (soa.Self()->IsExceptionPending()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700168 LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700169 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700170 std::ostringstream os;
171 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
172 LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\": "
173 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700174 }
175 return fid;
176}
177
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700178jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static,
179 const char* name, const char* signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700180 jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) :
181 env->GetMethodID(c, name, signature);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700182 if (mid == nullptr) {
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700183 ScopedObjectAccess soa(env);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700184 if (soa.Self()->IsExceptionPending()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700185 LOG(FATAL_WITHOUT_ABORT) << soa.Self()->GetException()->Dump();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 }
Brian Carlstrom3c821c62014-09-10 10:08:37 -0700187 std::ostringstream os;
188 WellKnownClasses::ToClass(c)->DumpClass(os, mirror::Class::kDumpClassFullDetail);
189 LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\": "
190 << os.str();
Elliott Hugheseac76672012-05-24 21:56:51 -0700191 }
192 return mid;
193}
194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
196 ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
197 return CacheMethod(env, boxed_class.get(), true, "valueOf",
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800198 android::base::StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199}
200
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100201#define STRING_INIT_LIST(V) \
202 V(java_lang_String_init, "()V", newEmptyString, "newEmptyString", "()Ljava/lang/String;", NewEmptyString) \
203 V(java_lang_String_init_B, "([B)V", newStringFromBytes_B, "newStringFromBytes", "([B)Ljava/lang/String;", NewStringFromBytes_B) \
204 V(java_lang_String_init_BI, "([BI)V", newStringFromBytes_BI, "newStringFromBytes", "([BI)Ljava/lang/String;", NewStringFromBytes_BI) \
205 V(java_lang_String_init_BII, "([BII)V", newStringFromBytes_BII, "newStringFromBytes", "([BII)Ljava/lang/String;", NewStringFromBytes_BII) \
206 V(java_lang_String_init_BIII, "([BIII)V", newStringFromBytes_BIII, "newStringFromBytes", "([BIII)Ljava/lang/String;", NewStringFromBytes_BIII) \
207 V(java_lang_String_init_BIIString, "([BIILjava/lang/String;)V", newStringFromBytes_BIIString, "newStringFromBytes", "([BIILjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BIIString) \
208 V(java_lang_String_init_BString, "([BLjava/lang/String;)V", newStringFromBytes_BString, "newStringFromBytes", "([BLjava/lang/String;)Ljava/lang/String;", NewStringFromBytes_BString) \
209 V(java_lang_String_init_BIICharset, "([BIILjava/nio/charset/Charset;)V", newStringFromBytes_BIICharset, "newStringFromBytes", "([BIILjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BIICharset) \
210 V(java_lang_String_init_BCharset, "([BLjava/nio/charset/Charset;)V", newStringFromBytes_BCharset, "newStringFromBytes", "([BLjava/nio/charset/Charset;)Ljava/lang/String;", NewStringFromBytes_BCharset) \
211 V(java_lang_String_init_C, "([C)V", newStringFromChars_C, "newStringFromChars", "([C)Ljava/lang/String;", NewStringFromChars_C) \
212 V(java_lang_String_init_CII, "([CII)V", newStringFromChars_CII, "newStringFromChars", "([CII)Ljava/lang/String;", NewStringFromChars_CII) \
213 V(java_lang_String_init_IIC, "(II[C)V", newStringFromChars_IIC, "newStringFromChars", "(II[C)Ljava/lang/String;", NewStringFromChars_IIC) \
214 V(java_lang_String_init_String, "(Ljava/lang/String;)V", newStringFromString, "newStringFromString", "(Ljava/lang/String;)Ljava/lang/String;", NewStringFromString) \
215 V(java_lang_String_init_StringBuffer, "(Ljava/lang/StringBuffer;)V", newStringFromStringBuffer, "newStringFromStringBuffer", "(Ljava/lang/StringBuffer;)Ljava/lang/String;", NewStringFromStringBuffer) \
216 V(java_lang_String_init_III, "([III)V", newStringFromCodePoints, "newStringFromCodePoints", "([III)Ljava/lang/String;", NewStringFromCodePoints) \
217 V(java_lang_String_init_StringBuilder, "(Ljava/lang/StringBuilder;)V", newStringFromStringBuilder, "newStringFromStringBuilder", "(Ljava/lang/StringBuilder;)Ljava/lang/String;", NewStringFromStringBuilder) \
218
219#define STATIC_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, ...) \
220 static ArtMethod* init_runtime_name; \
221 static ArtMethod* new_runtime_name;
222 STRING_INIT_LIST(STATIC_STRING_INIT)
223#undef STATIC_STRING_INIT
224
225void WellKnownClasses::InitStringInit(JNIEnv* env) {
226 ScopedObjectAccess soa(Thread::Current());
227 #define LOAD_STRING_INIT(init_runtime_name, init_signature, new_runtime_name, \
228 new_java_name, new_signature, ...) \
Andreas Gampe13b27842016-11-07 16:48:23 -0800229 init_runtime_name = jni::DecodeArtMethod( \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100230 CacheMethod(env, java_lang_String, false, "<init>", init_signature)); \
Andreas Gampe13b27842016-11-07 16:48:23 -0800231 new_runtime_name = jni::DecodeArtMethod( \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100232 CacheMethod(env, java_lang_StringFactory, true, new_java_name, new_signature));
233 STRING_INIT_LIST(LOAD_STRING_INIT)
234 #undef LOAD_STRING_INIT
235}
236
237void Thread::InitStringEntryPoints() {
238 QuickEntryPoints* qpoints = &tlsPtr_.quick_entrypoints;
239 #define SET_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name, \
240 new_java_name, new_signature, entry_point_name) \
241 qpoints->p ## entry_point_name = reinterpret_cast<void(*)()>(new_runtime_name);
242 STRING_INIT_LIST(SET_ENTRY_POINT)
243 #undef SET_ENTRY_POINT
244}
245
246ArtMethod* WellKnownClasses::StringInitToStringFactory(ArtMethod* string_init) {
247 #define TO_STRING_FACTORY(init_runtime_name, init_signature, new_runtime_name, \
248 new_java_name, new_signature, entry_point_name) \
Andreas Gampeca620d72016-11-08 08:09:33 -0800249 if (string_init == (init_runtime_name)) { \
250 return (new_runtime_name); \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100251 }
252 STRING_INIT_LIST(TO_STRING_FACTORY)
253 #undef TO_STRING_FACTORY
254 LOG(FATAL) << "Could not find StringFactory method for String.<init>";
255 return nullptr;
256}
257
258uint32_t WellKnownClasses::StringInitToEntryPoint(ArtMethod* string_init) {
259 #define TO_ENTRY_POINT(init_runtime_name, init_signature, new_runtime_name, \
260 new_java_name, new_signature, entry_point_name) \
Andreas Gampeca620d72016-11-08 08:09:33 -0800261 if (string_init == (init_runtime_name)) { \
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100262 return kQuick ## entry_point_name; \
263 }
264 STRING_INIT_LIST(TO_ENTRY_POINT)
265 #undef TO_STRING_FACTORY
266 LOG(FATAL) << "Could not find StringFactory method for String.<init>";
267 return 0;
268}
269#undef STRING_INIT_LIST
270
Brian Carlstromea46f952013-07-30 01:26:50 -0700271void WellKnownClasses::Init(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700272 com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700273 dalvik_annotation_optimization_CriticalNative =
274 CacheClass(env, "dalvik/annotation/optimization/CriticalNative");
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700275 dalvik_annotation_optimization_FastNative = CacheClass(env, "dalvik/annotation/optimization/FastNative");
Andreas Gampe46aba362016-10-28 14:33:28 -0700276 dalvik_system_BaseDexClassLoader = CacheClass(env, "dalvik/system/BaseDexClassLoader");
Nicolas Geoffray7d8d8ff2016-11-02 12:38:05 +0000277 dalvik_system_DexClassLoader = CacheClass(env, "dalvik/system/DexClassLoader");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700278 dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
279 dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700280 dalvik_system_DexPathList__Element = CacheClass(env, "dalvik/system/DexPathList$Element");
Narayan Kamathc3b7f1a2016-10-19 11:05:04 +0100281 dalvik_system_EmulatedStackFrame = CacheClass(env, "dalvik/system/EmulatedStackFrame");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700283 dalvik_system_VMRuntime = CacheClass(env, "dalvik/system/VMRuntime");
284
Jeff Hao13e748b2015-08-25 20:44:19 +0000285 java_lang_annotation_Annotation__array = CacheClass(env, "[Ljava/lang/annotation/Annotation;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700286 java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
Elliott Hugheseac76672012-05-24 21:56:51 -0700287 java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
288 java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
289 java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800290 java_lang_Object = CacheClass(env, "java/lang/Object");
Mathieu Chartiera61894d2015-04-23 16:32:54 -0700291 java_lang_OutOfMemoryError = CacheClass(env, "java/lang/OutOfMemoryError");
Elliott Hugheseac76672012-05-24 21:56:51 -0700292 java_lang_Error = CacheClass(env, "java/lang/Error");
Jeff Haoc92a7a12016-06-06 11:09:20 -0700293 java_lang_ExceptionInInitializerError = CacheClass(env, "java/lang/ExceptionInInitializerError");
294 java_lang_IllegalAccessError = CacheClass(env, "java/lang/IllegalAccessError");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000295 java_lang_invoke_MethodHandle = CacheClass(env, "java/lang/invoke/MethodHandle");
Jeff Haoc92a7a12016-06-06 11:09:20 -0700296 java_lang_NoClassDefFoundError = CacheClass(env, "java/lang/NoClassDefFoundError");
Brian Carlstromea46f952013-07-30 01:26:50 -0700297 java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor");
Neil Fuller0e844392016-09-08 13:43:31 +0100298 java_lang_reflect_Executable = CacheClass(env, "java/lang/reflect/Executable");
Brian Carlstromea46f952013-07-30 01:26:50 -0700299 java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field");
300 java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
Neil Fuller79a21e72016-09-09 14:24:51 +0100301 java_lang_reflect_Parameter = CacheClass(env, "java/lang/reflect/Parameter");
302 java_lang_reflect_Parameter__array = CacheClass(env, "[Ljava/lang/reflect/Parameter;");
Elliott Hugheseac76672012-05-24 21:56:51 -0700303 java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700304 java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
Ian Rogers120f1c72012-09-28 17:17:10 -0700305 java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
Ian Rogersdd157d72014-05-15 14:47:50 -0700306 java_lang_String = CacheClass(env, "java/lang/String");
Jeff Hao848f70a2014-01-15 13:49:50 -0800307 java_lang_StringFactory = CacheClass(env, "java/lang/StringFactory");
Mathieu Chartier987ccff2013-07-08 11:05:21 -0700308 java_lang_System = CacheClass(env, "java/lang/System");
Elliott Hugheseac76672012-05-24 21:56:51 -0700309 java_lang_Thread = CacheClass(env, "java/lang/Thread");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700310 java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700311 java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700312 java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200313 java_util_ArrayList = CacheClass(env, "java/util/ArrayList");
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700314 java_util_Collections = CacheClass(env, "java/util/Collections");
Jeff Hao13e748b2015-08-25 20:44:19 +0000315 libcore_reflect_AnnotationFactory = CacheClass(env, "libcore/reflect/AnnotationFactory");
316 libcore_reflect_AnnotationMember = CacheClass(env, "libcore/reflect/AnnotationMember");
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700317 libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
Elliott Hugheseac76672012-05-24 21:56:51 -0700318 org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
319 org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
320
Mathieu Chartierb5de3bb2015-06-05 13:21:05 -0700321 dalvik_system_VMRuntime_runFinalization = CacheMethod(env, dalvik_system_VMRuntime, true, "runFinalization", "(J)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700322 com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
323 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 -0700324 java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700325
Elliott Hugheseac76672012-05-24 21:56:51 -0700326 java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
327 java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
Mathieu Chartiercef50f02014-12-09 17:38:52 -0800328 java_lang_Daemons_stop = CacheMethod(env, java_lang_Daemons, true, "stop", "()V");
Orion Hodson811bd5f2016-12-07 11:35:37 +0000329 java_lang_invoke_MethodHandle_invoke =
330 CacheMethod(env, java_lang_invoke_MethodHandle, false,
331 "invoke", "([Ljava/lang/Object;)Ljava/lang/Object;");
332 java_lang_invoke_MethodHandle_invokeExact =
333 CacheMethod(env, java_lang_invoke_MethodHandle, false,
334 "invokeExact", "([Ljava/lang/Object;)Ljava/lang/Object;");
Elliott Hughesa4f94742012-05-29 16:28:38 -0700335 ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
336 java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
337 ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
338 java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
339
Neil Fuller79a21e72016-09-09 14:24:51 +0100340 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 +0000341 java_lang_String_charAt = CacheMethod(env, java_lang_String, false, "charAt", "(I)C");
Tobias Thierer5cbe65d2016-07-22 18:37:59 +0100342 java_lang_Thread_dispatchUncaughtException = CacheMethod(env, java_lang_Thread, false, "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
Elliott Hugheseac76672012-05-24 21:56:51 -0700343 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 -0700344 java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
Piotr Jastrzebskie6cd3b52015-05-07 09:41:00 +0100345 java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "threadTerminated", "(Ljava/lang/Thread;)V");
Elliott Hughesb5681212013-03-29 17:29:22 -0700346 java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
Jeff Hao13e748b2015-08-25 20:44:19 +0000347 libcore_reflect_AnnotationFactory_createAnnotation = CacheMethod(env, libcore_reflect_AnnotationFactory, true, "createAnnotation", "(Ljava/lang/Class;[Llibcore/reflect/AnnotationMember;)Ljava/lang/annotation/Annotation;");
348 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 -0700349 org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
350 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;");
351
Andreas Gampe46aba362016-10-28 14:33:28 -0700352 dalvik_system_BaseDexClassLoader_pathList = CacheField(env, dalvik_system_BaseDexClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
Andreas Gampe324b9bb2015-02-23 16:33:22 -0800353 dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "Ljava/lang/Object;");
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800354 dalvik_system_DexFile_fileName = CacheField(env, dalvik_system_DexFile, false, "mFileName", "Ljava/lang/String;");
Mathieu Chartier6bcae8f2014-09-04 18:33:17 -0700355 dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
Andreas Gampec8ccf682014-09-29 20:07:43 -0700356 dalvik_system_DexPathList__Element_dexFile = CacheField(env, dalvik_system_DexPathList__Element, false, "dexFile", "Ldalvik/system/DexFile;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700357 java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
358 java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
Elliott Hughesc5dc2ff2013-01-09 13:44:30 -0800359 java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700360 java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
361 java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
Ian Rogersef7d42f2014-01-06 12:55:46 -0800362 java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000363 java_lang_ThreadGroup_groups = CacheField(env, java_lang_ThreadGroup, false, "groups", "[Ljava/lang/ThreadGroup;");
Przemyslaw Szczepaniak464595f2015-11-24 11:59:59 +0000364 java_lang_ThreadGroup_ngroups = CacheField(env, java_lang_ThreadGroup, false, "ngroups", "I");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700365 java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
366 java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200367 java_lang_ThreadGroup_parent = CacheField(env, java_lang_ThreadGroup, false, "parent", "Ljava/lang/ThreadGroup;");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700368 java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
Brian Carlstrom34375312014-09-10 23:10:47 -0700369 java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
370 java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
371 java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
Piotr Jastrzebskic16a50f2015-05-07 09:41:00 +0100372 java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "backtrace", "Ljava/lang/Object;");
Brian Carlstrom34375312014-09-10 23:10:47 -0700373 java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
Neil Fuller0e844392016-09-08 13:43:31 +0100374 java_lang_reflect_Executable_artMethod = CacheField(env, java_lang_reflect_Executable, false, "artMethod", "J");
Elliott Hughesaecb5f32013-03-28 08:27:38 -0700375 java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
Piotr Jastrzebski8c145b92015-05-05 12:54:00 +0100376 java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "address", "J");
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000377 java_util_ArrayList_array = CacheField(env, java_util_ArrayList, false, "elementData", "[Ljava/lang/Object;");
Sebastien Hertze49e1952014-10-13 11:27:13 +0200378 java_util_ArrayList_size = CacheField(env, java_util_ArrayList, false, "size", "I");
Brian Carlstrom34375312014-09-10 23:10:47 -0700379 java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
380 libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
Sebastien Hertzb30e0c62014-09-11 16:23:44 +0200381 org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700382 org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
383 org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
384 org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700385
386 java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
387 java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
388 java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
389 java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
390 java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
391 java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
392 java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
393 java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
Jeff Hao848f70a2014-01-15 13:49:50 -0800394
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +0100395 InitStringInit(env);
Jeff Hao848f70a2014-01-15 13:49:50 -0800396 Thread::Current()->InitStringEntryPoints();
Elliott Hughesaf8d15a2012-05-29 09:12:18 -0700397}
398
Ian Rogersef28b142012-11-30 14:22:18 -0800399void WellKnownClasses::LateInit(JNIEnv* env) {
400 ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
Przemyslaw Szczepaniakf11cd292016-08-17 17:46:38 +0100401 // CacheField and CacheMethod will initialize their classes. Classes below
402 // have clinit sections that call JNI methods. Late init is required
403 // to make sure these JNI methods are available.
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800404 java_lang_Runtime_nativeLoad =
405 CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad",
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800406 "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)"
Dmitriy Ivanov44409d32015-11-11 14:18:55 -0800407 "Ljava/lang/String;");
Przemyslaw Szczepaniakf11cd292016-08-17 17:46:38 +0100408 java_lang_reflect_Proxy_invoke =
409 CacheMethod(env, java_lang_reflect_Proxy, true, "invoke",
410 "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/Method;"
411 "[Ljava/lang/Object;)Ljava/lang/Object;");
412 java_lang_reflect_Proxy_h =
413 CacheField(env, java_lang_reflect_Proxy, false, "h",
414 "Ljava/lang/reflect/InvocationHandler;");
Ian Rogersef28b142012-11-30 14:22:18 -0800415}
416
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700417ObjPtr<mirror::Class> WellKnownClasses::ToClass(jclass global_jclass) {
Alex Lightd6251582016-10-31 11:12:30 -0700418 auto ret = ObjPtr<mirror::Class>::DownCast(Thread::Current()->DecodeJObject(global_jclass));
419 DCHECK(!ret.IsNull());
420 return ret;
Elliott Hugheseac76672012-05-24 21:56:51 -0700421}
422
Elliott Hugheseac76672012-05-24 21:56:51 -0700423} // namespace art