1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  The Android Open Source
7  * Project designates this particular file as subject to the "Classpath"
8  * exception as provided by The Android Open Source Project in the LICENSE
9  * file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <jni.h>
23 #include <android-base/logging.h>
24 
25 extern "C" void register_java_util_zip_ZipFile(JNIEnv* env);
26 extern "C" void register_java_util_zip_Inflater(JNIEnv* env);
27 extern "C" void register_java_util_zip_Deflater(JNIEnv* env);
28 extern "C" void register_java_util_zip_CRC32(JNIEnv* env);
29 extern "C" void register_java_util_zip_Adler32(JNIEnv* env);
30 extern "C" void register_java_io_FileDescriptor(JNIEnv* env);
31 extern "C" void register_sun_nio_ch_DatagramChannelImpl(JNIEnv* env);
32 extern "C" void register_sun_nio_ch_DatagramDispatcher(JNIEnv* env);
33 extern "C" void register_java_io_Console(JNIEnv* env);
34 extern "C" void register_sun_nio_ch_IOUtil(JNIEnv* env);
35 extern "C" void register_sun_nio_ch_SocketChannelImpl(JNIEnv* env);
36 extern "C" void register_sun_nio_ch_FileChannelImpl(JNIEnv* env);
37 extern "C" void register_sun_nio_ch_FileDispatcherImpl(JNIEnv* env);
38 extern "C" void register_java_io_FileInputStream(JNIEnv* env);
39 extern "C" void register_java_util_prefs_FileSystemPreferences(JNIEnv* env);
40 extern "C" void register_sun_nio_ch_NativeThread(JNIEnv* env);
41 extern "C" void register_sun_nio_ch_FileKey(JNIEnv* env);
42 extern "C" void register_java_io_UnixFileSystem(JNIEnv* env);
43 extern "C" void register_java_io_ObjectStreamClass(JNIEnv* env);
44 extern "C" void register_java_io_ObjectOutputStream(JNIEnv* env);
45 extern "C" void register_java_io_ObjectInputStream(JNIEnv* env);
46 extern "C" void register_java_net_InetAddress(JNIEnv* env);
47 extern "C" jint net_JNI_OnLoad(JavaVM *vm, void* ignored);
48 extern "C" void register_sun_nio_ch_Net(JNIEnv* env);
49 extern "C" void register_java_nio_MappedByteBuffer(JNIEnv* env);
50 extern "C" void register_java_net_Inet6Address(JNIEnv* env);
51 extern "C" void register_java_net_Inet4Address(JNIEnv* env);
52 extern "C" void register_sun_nio_ch_ServerSocketChannelImpl(JNIEnv* env);
53 extern "C" void register_java_net_SocketInputStream(JNIEnv* env);
54 extern "C" void register_java_net_SocketOutputStream(JNIEnv* env);
55 extern "C" void register_java_lang_Float(JNIEnv* env);
56 extern "C" void register_java_lang_Double(JNIEnv* env);
57 extern "C" void register_java_lang_StrictMath(JNIEnv* env);
58 extern "C" void register_java_lang_Math(JNIEnv* env);
59 extern "C" void register_java_lang_ProcessEnvironment(JNIEnv* env);
60 extern "C" void register_java_lang_System(JNIEnv* env);
61 extern "C" void register_java_lang_Runtime(JNIEnv* env);
62 extern "C" void register_java_lang_UNIXProcess(JNIEnv* env);
63 void register_java_lang_Character(JNIEnv* env);
64 
JNI_OnLoad(JavaVM * vm,void *)65 extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
66   jint version = JNI_VERSION_1_6;
67   void* raw_env;
68   jint result = vm->GetEnv(&raw_env, version);
69   CHECK_EQ(result, JNI_OK);
70   CHECK(raw_env != nullptr);
71   JNIEnv* env = static_cast<JNIEnv*>(raw_env);
72 
73   // Some registration functions also do some extra local initialization,
74   // creating local references in the process. ART does not expect JNI_OnLoad()
75   // to leave any local references in the current frame, so create a new one.
76   // Request space for 256 local references (increase if necessary).
77   result = env->PushLocalFrame(256);
78   CHECK_EQ(result, 0);
79 
80   // Some registration functions also record field ids retrieved using
81   // GetFieldID(), forcing the initialization of the searched class. As some
82   // class initializers (notably Inet*Address) directly or indirectly use
83   // the StringBuilder, we need to start by registering native methods needed
84   // for resizing its internal buffer. That's done through Arrays.copyOf()
85   // which uses System.arraycopy() and Math.min(), forcing the initialization
86   // of System and Main. The former uses System's own native methods while the
87   // latter uses native methods of Float and Double but not Math's own.
88   register_java_lang_Float(env);
89   register_java_lang_Double(env);
90   register_java_lang_System(env);
91 
92   // Initialize the rest in the order in which they appear in Android.bp .
93   register_java_util_zip_ZipFile(env);
94   register_java_util_zip_Inflater(env);
95   register_java_util_zip_Deflater(env);
96   register_java_util_zip_CRC32(env);
97   register_java_util_zip_Adler32(env);
98   register_java_io_FileDescriptor(env);
99   register_sun_nio_ch_DatagramChannelImpl(env);
100   register_sun_nio_ch_DatagramDispatcher(env);
101   register_java_io_Console(env);
102   register_sun_nio_ch_IOUtil(env);
103   register_sun_nio_ch_SocketChannelImpl(env);
104   register_sun_nio_ch_FileChannelImpl(env);
105   register_sun_nio_ch_FileDispatcherImpl(env);
106   register_java_io_FileInputStream(env);
107   register_java_util_prefs_FileSystemPreferences(env);
108   register_sun_nio_ch_NativeThread(env);
109   register_sun_nio_ch_FileKey(env);
110   register_java_io_UnixFileSystem(env);
111   register_java_io_ObjectStreamClass(env);
112   register_java_io_ObjectOutputStream(env);
113   register_java_io_ObjectInputStream(env);
114   register_java_net_InetAddress(env);
115 
116   jint net_jni_version = net_JNI_OnLoad(vm, /* ignored */ nullptr);
117   CHECK(net_jni_version == JNI_VERSION_1_2 ||
118         net_jni_version == JNI_VERSION_1_4 ||
119         net_jni_version == JNI_VERSION_1_6);
120 
121   register_sun_nio_ch_Net(env);
122   register_java_nio_MappedByteBuffer(env);
123   register_java_net_Inet6Address(env);
124   register_java_net_Inet4Address(env);
125   register_sun_nio_ch_ServerSocketChannelImpl(env);
126   register_java_net_SocketInputStream(env);
127   register_java_net_SocketOutputStream(env);
128   register_java_lang_StrictMath(env);
129   register_java_lang_Math(env);
130   register_java_lang_ProcessEnvironment(env);
131   register_java_lang_Runtime(env);
132   register_java_lang_UNIXProcess(env);
133   register_java_lang_Character(env);
134 
135   env->PopLocalFrame(/* result */ nullptr);  // Pop the local frame.
136   return version;
137 }
138