1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "well_known_classes.h"
18 
19 #include <stdlib.h>
20 
21 #include "base/logging.h"
22 #include "mirror/class.h"
23 #include "ScopedLocalRef.h"
24 #include "thread-inl.h"
25 
26 namespace art {
27 
28 jclass WellKnownClasses::com_android_dex_Dex;
29 jclass WellKnownClasses::dalvik_system_DexFile;
30 jclass WellKnownClasses::dalvik_system_DexPathList;
31 jclass WellKnownClasses::dalvik_system_DexPathList$Element;
32 jclass WellKnownClasses::dalvik_system_PathClassLoader;
33 jclass WellKnownClasses::java_lang_BootClassLoader;
34 jclass WellKnownClasses::java_lang_ClassLoader;
35 jclass WellKnownClasses::java_lang_ClassNotFoundException;
36 jclass WellKnownClasses::java_lang_Daemons;
37 jclass WellKnownClasses::java_lang_Error;
38 jclass WellKnownClasses::java_lang_Object;
39 jclass WellKnownClasses::java_lang_reflect_AbstractMethod;
40 jclass WellKnownClasses::java_lang_reflect_ArtMethod;
41 jclass WellKnownClasses::java_lang_reflect_Constructor;
42 jclass WellKnownClasses::java_lang_reflect_Field;
43 jclass WellKnownClasses::java_lang_reflect_Method;
44 jclass WellKnownClasses::java_lang_reflect_Proxy;
45 jclass WellKnownClasses::java_lang_RuntimeException;
46 jclass WellKnownClasses::java_lang_StackOverflowError;
47 jclass WellKnownClasses::java_lang_String;
48 jclass WellKnownClasses::java_lang_System;
49 jclass WellKnownClasses::java_lang_Thread;
50 jclass WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler;
51 jclass WellKnownClasses::java_lang_ThreadGroup;
52 jclass WellKnownClasses::java_lang_Throwable;
53 jclass WellKnownClasses::java_nio_DirectByteBuffer;
54 jclass WellKnownClasses::java_util_Collections;
55 jclass WellKnownClasses::libcore_util_EmptyArray;
56 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk;
57 jclass WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer;
58 
59 jmethodID WellKnownClasses::com_android_dex_Dex_create;
60 jmethodID WellKnownClasses::java_lang_Boolean_valueOf;
61 jmethodID WellKnownClasses::java_lang_Byte_valueOf;
62 jmethodID WellKnownClasses::java_lang_Character_valueOf;
63 jmethodID WellKnownClasses::java_lang_ClassLoader_loadClass;
64 jmethodID WellKnownClasses::java_lang_ClassNotFoundException_init;
65 jmethodID WellKnownClasses::java_lang_Daemons_requestGC;
66 jmethodID WellKnownClasses::java_lang_Daemons_requestHeapTrim;
67 jmethodID WellKnownClasses::java_lang_Daemons_start;
68 jmethodID WellKnownClasses::java_lang_Double_valueOf;
69 jmethodID WellKnownClasses::java_lang_Float_valueOf;
70 jmethodID WellKnownClasses::java_lang_Integer_valueOf;
71 jmethodID WellKnownClasses::java_lang_Long_valueOf;
72 jmethodID WellKnownClasses::java_lang_ref_FinalizerReference_add;
73 jmethodID WellKnownClasses::java_lang_ref_ReferenceQueue_add;
74 jmethodID WellKnownClasses::java_lang_reflect_Proxy_invoke;
75 jmethodID WellKnownClasses::java_lang_Runtime_nativeLoad;
76 jmethodID WellKnownClasses::java_lang_Short_valueOf;
77 jmethodID WellKnownClasses::java_lang_System_runFinalization = NULL;
78 jmethodID WellKnownClasses::java_lang_Thread_init;
79 jmethodID WellKnownClasses::java_lang_Thread_run;
80 jmethodID WellKnownClasses::java_lang_Thread$UncaughtExceptionHandler_uncaughtException;
81 jmethodID WellKnownClasses::java_lang_ThreadGroup_removeThread;
82 jmethodID WellKnownClasses::java_nio_DirectByteBuffer_init;
83 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast;
84 jmethodID WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch;
85 
86 jfieldID WellKnownClasses::dalvik_system_DexFile_cookie;
87 jfieldID WellKnownClasses::dalvik_system_PathClassLoader_pathList;
88 jfieldID WellKnownClasses::dalvik_system_DexPathList_dexElements;
89 jfieldID WellKnownClasses::dalvik_system_DexPathList$Element_dexFile;
90 jfieldID WellKnownClasses::java_lang_Thread_daemon;
91 jfieldID WellKnownClasses::java_lang_Thread_group;
92 jfieldID WellKnownClasses::java_lang_Thread_lock;
93 jfieldID WellKnownClasses::java_lang_Thread_name;
94 jfieldID WellKnownClasses::java_lang_Thread_priority;
95 jfieldID WellKnownClasses::java_lang_Thread_uncaughtHandler;
96 jfieldID WellKnownClasses::java_lang_Thread_nativePeer;
97 jfieldID WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
98 jfieldID WellKnownClasses::java_lang_ThreadGroup_name;
99 jfieldID WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
100 jfieldID WellKnownClasses::java_lang_Throwable_cause;
101 jfieldID WellKnownClasses::java_lang_Throwable_detailMessage;
102 jfieldID WellKnownClasses::java_lang_Throwable_stackTrace;
103 jfieldID WellKnownClasses::java_lang_Throwable_stackState;
104 jfieldID WellKnownClasses::java_lang_Throwable_suppressedExceptions;
105 jfieldID WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod;
106 jfieldID WellKnownClasses::java_lang_reflect_Field_artField;
107 jfieldID WellKnownClasses::java_lang_reflect_Proxy_h;
108 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_capacity;
109 jfieldID WellKnownClasses::java_nio_DirectByteBuffer_effectiveDirectAddress;
110 jfieldID WellKnownClasses::java_util_Collections_EMPTY_LIST;
111 jfieldID WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT;
112 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data;
113 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length;
114 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset;
115 jfieldID WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type;
116 
CacheClass(JNIEnv * env,const char * jni_class_name)117 static jclass CacheClass(JNIEnv* env, const char* jni_class_name) {
118   ScopedLocalRef<jclass> c(env, env->FindClass(jni_class_name));
119   if (c.get() == NULL) {
120     LOG(FATAL) << "Couldn't find class: " << jni_class_name;
121   }
122   return reinterpret_cast<jclass>(env->NewGlobalRef(c.get()));
123 }
124 
CacheField(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)125 static jfieldID CacheField(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
126   jfieldID fid = is_static ? env->GetStaticFieldID(c, name, signature) : env->GetFieldID(c, name, signature);
127   if (fid == NULL) {
128     LOG(FATAL) << "Couldn't find field \"" << name << "\" with signature \"" << signature << "\"";
129   }
130   return fid;
131 }
132 
CacheMethod(JNIEnv * env,jclass c,bool is_static,const char * name,const char * signature)133 jmethodID CacheMethod(JNIEnv* env, jclass c, bool is_static, const char* name, const char* signature) {
134   jmethodID mid = is_static ? env->GetStaticMethodID(c, name, signature) : env->GetMethodID(c, name, signature);
135   if (mid == NULL) {
136     LOG(FATAL) << "Couldn't find method \"" << name << "\" with signature \"" << signature << "\"";
137   }
138   return mid;
139 }
140 
CachePrimitiveBoxingMethod(JNIEnv * env,char prim_name,const char * boxed_name)141 static jmethodID CachePrimitiveBoxingMethod(JNIEnv* env, char prim_name, const char* boxed_name) {
142   ScopedLocalRef<jclass> boxed_class(env, env->FindClass(boxed_name));
143   return CacheMethod(env, boxed_class.get(), true, "valueOf",
144                      StringPrintf("(%c)L%s;", prim_name, boxed_name).c_str());
145 }
146 
Init(JNIEnv * env)147 void WellKnownClasses::Init(JNIEnv* env) {
148   com_android_dex_Dex = CacheClass(env, "com/android/dex/Dex");
149   dalvik_system_DexFile = CacheClass(env, "dalvik/system/DexFile");
150   dalvik_system_DexPathList = CacheClass(env, "dalvik/system/DexPathList");
151   dalvik_system_DexPathList$Element = CacheClass(env, "dalvik/system/DexPathList$Element");
152   dalvik_system_PathClassLoader = CacheClass(env, "dalvik/system/PathClassLoader");
153   java_lang_BootClassLoader = CacheClass(env, "java/lang/BootClassLoader");
154   java_lang_ClassLoader = CacheClass(env, "java/lang/ClassLoader");
155   java_lang_ClassNotFoundException = CacheClass(env, "java/lang/ClassNotFoundException");
156   java_lang_Daemons = CacheClass(env, "java/lang/Daemons");
157   java_lang_Object = CacheClass(env, "java/lang/Object");
158   java_lang_Error = CacheClass(env, "java/lang/Error");
159   java_lang_reflect_AbstractMethod = CacheClass(env, "java/lang/reflect/AbstractMethod");
160   java_lang_reflect_ArtMethod = CacheClass(env, "java/lang/reflect/ArtMethod");
161   java_lang_reflect_Constructor = CacheClass(env, "java/lang/reflect/Constructor");
162   java_lang_reflect_Field = CacheClass(env, "java/lang/reflect/Field");
163   java_lang_reflect_Method = CacheClass(env, "java/lang/reflect/Method");
164   java_lang_reflect_Proxy = CacheClass(env, "java/lang/reflect/Proxy");
165   java_lang_RuntimeException = CacheClass(env, "java/lang/RuntimeException");
166   java_lang_StackOverflowError = CacheClass(env, "java/lang/StackOverflowError");
167   java_lang_String = CacheClass(env, "java/lang/String");
168   java_lang_System = CacheClass(env, "java/lang/System");
169   java_lang_Thread = CacheClass(env, "java/lang/Thread");
170   java_lang_Thread$UncaughtExceptionHandler = CacheClass(env, "java/lang/Thread$UncaughtExceptionHandler");
171   java_lang_ThreadGroup = CacheClass(env, "java/lang/ThreadGroup");
172   java_lang_Throwable = CacheClass(env, "java/lang/Throwable");
173   java_nio_DirectByteBuffer = CacheClass(env, "java/nio/DirectByteBuffer");
174   java_util_Collections = CacheClass(env, "java/util/Collections");
175   libcore_util_EmptyArray = CacheClass(env, "libcore/util/EmptyArray");
176   org_apache_harmony_dalvik_ddmc_Chunk = CacheClass(env, "org/apache/harmony/dalvik/ddmc/Chunk");
177   org_apache_harmony_dalvik_ddmc_DdmServer = CacheClass(env, "org/apache/harmony/dalvik/ddmc/DdmServer");
178 
179   com_android_dex_Dex_create = CacheMethod(env, com_android_dex_Dex, true, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
180   java_lang_ClassNotFoundException_init = CacheMethod(env, java_lang_ClassNotFoundException, false, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
181   java_lang_ClassLoader_loadClass = CacheMethod(env, java_lang_ClassLoader, false, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
182 
183   java_lang_Daemons_requestGC = CacheMethod(env, java_lang_Daemons, true, "requestGC", "()V");
184   java_lang_Daemons_requestHeapTrim = CacheMethod(env, java_lang_Daemons, true, "requestHeapTrim", "()V");
185   java_lang_Daemons_start = CacheMethod(env, java_lang_Daemons, true, "start", "()V");
186 
187   ScopedLocalRef<jclass> java_lang_ref_FinalizerReference(env, env->FindClass("java/lang/ref/FinalizerReference"));
188   java_lang_ref_FinalizerReference_add = CacheMethod(env, java_lang_ref_FinalizerReference.get(), true, "add", "(Ljava/lang/Object;)V");
189   ScopedLocalRef<jclass> java_lang_ref_ReferenceQueue(env, env->FindClass("java/lang/ref/ReferenceQueue"));
190   java_lang_ref_ReferenceQueue_add = CacheMethod(env, java_lang_ref_ReferenceQueue.get(), true, "add", "(Ljava/lang/ref/Reference;)V");
191 
192   java_lang_reflect_Proxy_invoke = CacheMethod(env, java_lang_reflect_Proxy, true, "invoke", "(Ljava/lang/reflect/Proxy;Ljava/lang/reflect/ArtMethod;[Ljava/lang/Object;)Ljava/lang/Object;");
193   java_lang_Thread_init = CacheMethod(env, java_lang_Thread, false, "<init>", "(Ljava/lang/ThreadGroup;Ljava/lang/String;IZ)V");
194   java_lang_Thread_run = CacheMethod(env, java_lang_Thread, false, "run", "()V");
195   java_lang_Thread$UncaughtExceptionHandler_uncaughtException = CacheMethod(env, java_lang_Thread$UncaughtExceptionHandler, false, "uncaughtException", "(Ljava/lang/Thread;Ljava/lang/Throwable;)V");
196   java_lang_ThreadGroup_removeThread = CacheMethod(env, java_lang_ThreadGroup, false, "removeThread", "(Ljava/lang/Thread;)V");
197   java_nio_DirectByteBuffer_init = CacheMethod(env, java_nio_DirectByteBuffer, false, "<init>", "(JI)V");
198   org_apache_harmony_dalvik_ddmc_DdmServer_broadcast = CacheMethod(env, org_apache_harmony_dalvik_ddmc_DdmServer, true, "broadcast", "(I)V");
199   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;");
200 
201   dalvik_system_DexFile_cookie = CacheField(env, dalvik_system_DexFile, false, "mCookie", "J");
202   dalvik_system_PathClassLoader_pathList = CacheField(env, dalvik_system_PathClassLoader, false, "pathList", "Ldalvik/system/DexPathList;");
203   dalvik_system_DexPathList_dexElements = CacheField(env, dalvik_system_DexPathList, false, "dexElements", "[Ldalvik/system/DexPathList$Element;");
204   dalvik_system_DexPathList$Element_dexFile = CacheField(env, dalvik_system_DexPathList$Element, false, "dexFile", "Ldalvik/system/DexFile;");
205   java_lang_Thread_daemon = CacheField(env, java_lang_Thread, false, "daemon", "Z");
206   java_lang_Thread_group = CacheField(env, java_lang_Thread, false, "group", "Ljava/lang/ThreadGroup;");
207   java_lang_Thread_lock = CacheField(env, java_lang_Thread, false, "lock", "Ljava/lang/Object;");
208   java_lang_Thread_name = CacheField(env, java_lang_Thread, false, "name", "Ljava/lang/String;");
209   java_lang_Thread_priority = CacheField(env, java_lang_Thread, false, "priority", "I");
210   java_lang_Thread_uncaughtHandler = CacheField(env, java_lang_Thread, false, "uncaughtHandler", "Ljava/lang/Thread$UncaughtExceptionHandler;");
211   java_lang_Thread_nativePeer = CacheField(env, java_lang_Thread, false, "nativePeer", "J");
212   java_lang_ThreadGroup_mainThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "mainThreadGroup", "Ljava/lang/ThreadGroup;");
213   java_lang_ThreadGroup_name = CacheField(env, java_lang_ThreadGroup, false, "name", "Ljava/lang/String;");
214   java_lang_ThreadGroup_systemThreadGroup = CacheField(env, java_lang_ThreadGroup, true, "systemThreadGroup", "Ljava/lang/ThreadGroup;");
215   java_lang_Throwable_cause = CacheField(env, java_lang_Throwable, false, "cause", "Ljava/lang/Throwable;");
216   java_lang_Throwable_detailMessage = CacheField(env, java_lang_Throwable, false, "detailMessage", "Ljava/lang/String;");
217   java_lang_Throwable_stackTrace = CacheField(env, java_lang_Throwable, false, "stackTrace", "[Ljava/lang/StackTraceElement;");
218   java_lang_Throwable_stackState = CacheField(env, java_lang_Throwable, false, "stackState", "Ljava/lang/Object;");
219   java_lang_Throwable_suppressedExceptions = CacheField(env, java_lang_Throwable, false, "suppressedExceptions", "Ljava/util/List;");
220   java_lang_reflect_AbstractMethod_artMethod = CacheField(env, java_lang_reflect_AbstractMethod, false, "artMethod", "Ljava/lang/reflect/ArtMethod;");
221   java_lang_reflect_Field_artField = CacheField(env, java_lang_reflect_Field, false, "artField", "Ljava/lang/reflect/ArtField;");
222   java_lang_reflect_Proxy_h = CacheField(env, java_lang_reflect_Proxy, false, "h", "Ljava/lang/reflect/InvocationHandler;");
223   java_nio_DirectByteBuffer_capacity = CacheField(env, java_nio_DirectByteBuffer, false, "capacity", "I");
224   java_nio_DirectByteBuffer_effectiveDirectAddress = CacheField(env, java_nio_DirectByteBuffer, false, "effectiveDirectAddress", "J");
225   java_util_Collections_EMPTY_LIST = CacheField(env, java_util_Collections, true, "EMPTY_LIST", "Ljava/util/List;");
226   libcore_util_EmptyArray_STACK_TRACE_ELEMENT = CacheField(env, libcore_util_EmptyArray, true, "STACK_TRACE_ELEMENT", "[Ljava/lang/StackTraceElement;");
227   org_apache_harmony_dalvik_ddmc_Chunk_data = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "data", "[B");
228   org_apache_harmony_dalvik_ddmc_Chunk_length = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "length", "I");
229   org_apache_harmony_dalvik_ddmc_Chunk_offset = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "offset", "I");
230   org_apache_harmony_dalvik_ddmc_Chunk_type = CacheField(env, org_apache_harmony_dalvik_ddmc_Chunk, false, "type", "I");
231 
232   java_lang_Boolean_valueOf = CachePrimitiveBoxingMethod(env, 'Z', "java/lang/Boolean");
233   java_lang_Byte_valueOf = CachePrimitiveBoxingMethod(env, 'B', "java/lang/Byte");
234   java_lang_Character_valueOf = CachePrimitiveBoxingMethod(env, 'C', "java/lang/Character");
235   java_lang_Double_valueOf = CachePrimitiveBoxingMethod(env, 'D', "java/lang/Double");
236   java_lang_Float_valueOf = CachePrimitiveBoxingMethod(env, 'F', "java/lang/Float");
237   java_lang_Integer_valueOf = CachePrimitiveBoxingMethod(env, 'I', "java/lang/Integer");
238   java_lang_Long_valueOf = CachePrimitiveBoxingMethod(env, 'J', "java/lang/Long");
239   java_lang_Short_valueOf = CachePrimitiveBoxingMethod(env, 'S', "java/lang/Short");
240 }
241 
LateInit(JNIEnv * env)242 void WellKnownClasses::LateInit(JNIEnv* env) {
243   ScopedLocalRef<jclass> java_lang_Runtime(env, env->FindClass("java/lang/Runtime"));
244   java_lang_Runtime_nativeLoad = CacheMethod(env, java_lang_Runtime.get(), true, "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/String;");
245 }
246 
ToClass(jclass global_jclass)247 mirror::Class* WellKnownClasses::ToClass(jclass global_jclass) {
248   return reinterpret_cast<mirror::Class*>(Thread::Current()->DecodeJObject(global_jclass));
249 }
250 
251 }  // namespace art
252