1 /*
2 * Copyright (C) 2013 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 "include_platform/nativehelper/JniInvocation.h"
18
19 #define LOG_TAG "JniInvocation"
20 #include "ALog-priv.h"
21
22 #if defined(__ANDROID__)
23 #include <sys/system_properties.h>
24 #endif
25
26 #include <errno.h>
27 #include <jni.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "DlHelp.h"
35
36 // Name the default library providing the JNI Invocation API.
37 static const char* kDefaultJniInvocationLibrary = "libart.so";
38 static const char* kDebugJniInvocationLibrary = "libartd.so";
39 #if defined(__LP64__)
40 #define LIB_DIR "lib64"
41 #else
42 #define LIB_DIR "lib"
43 #endif
44 static const char* kDebugJniInvocationLibraryPath = "/apex/com.android.art/" LIB_DIR "/libartd.so";
45
46 struct JniInvocationImpl {
47 // Name of library providing JNI_ method implementations.
48 const char* jni_provider_library_name;
49
50 // Opaque pointer to shared library from dlopen / LoadLibrary.
51 void* jni_provider_library;
52
53 // Function pointers to methods in JNI provider.
54 jint (*JNI_GetDefaultJavaVMInitArgs)(void*);
55 jint (*JNI_CreateJavaVM)(JavaVM**, JNIEnv**, void*);
56 jint (*JNI_GetCreatedJavaVMs)(JavaVM**, jsize, jsize*);
57 };
58
59 static struct JniInvocationImpl g_impl;
60
61 //
62 // Internal helpers.
63 //
64
65 #define UNUSED(x) (x) = (x)
66
IsDebuggable()67 static bool IsDebuggable() {
68 #ifdef __ANDROID__
69 char debuggable[PROP_VALUE_MAX] = {0};
70 __system_property_get("ro.debuggable", debuggable);
71 return strcmp(debuggable, "1") == 0;
72 #else
73 // Host is always treated as debuggable, which allows choice of library to be overridden.
74 return true;
75 #endif
76 }
77
GetLibrarySystemProperty(char * buffer)78 static int GetLibrarySystemProperty(char* buffer) {
79 #ifdef __ANDROID__
80 return __system_property_get("persist.sys.dalvik.vm.lib.2", buffer);
81 #else
82 // Host does not use properties.
83 UNUSED(buffer);
84 return 0;
85 #endif
86 }
87
FindSymbol(DlLibrary library,const char * symbol)88 static DlSymbol FindSymbol(DlLibrary library, const char* symbol) {
89 DlSymbol s = DlGetSymbol(library, symbol);
90 if (s == NULL) {
91 ALOGE("Failed to find symbol: %s", symbol);
92 }
93 return s;
94 }
95
96 //
97 // Exported functions for JNI based VM management from JNI spec.
98 //
99
JNI_GetDefaultJavaVMInitArgs(void * vmargs)100 jint JNI_GetDefaultJavaVMInitArgs(void* vmargs) {
101 ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_GetDefaultJavaVMInitArgs, "Runtime library not loaded.");
102 return g_impl.JNI_GetDefaultJavaVMInitArgs(vmargs);
103 }
104
JNI_CreateJavaVM(JavaVM ** p_vm,JNIEnv ** p_env,void * vm_args)105 jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
106 ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_CreateJavaVM, "Runtime library not loaded.");
107 return g_impl.JNI_CreateJavaVM(p_vm, p_env, vm_args);
108 }
109
JNI_GetCreatedJavaVMs(JavaVM ** vms,jsize size,jsize * vm_count)110 jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) {
111 if (NULL == g_impl.JNI_GetCreatedJavaVMs) {
112 *vm_count = 0;
113 return JNI_OK;
114 }
115 return g_impl.JNI_GetCreatedJavaVMs(vms, size, vm_count);
116 }
117
118 //
119 // JniInvocation functions for setting up JNI functions.
120 //
121
JniInvocationGetLibraryWith(const char * library,bool is_debuggable,const char * system_preferred_library)122 const char* JniInvocationGetLibraryWith(const char* library,
123 bool is_debuggable,
124 const char* system_preferred_library) {
125 if (is_debuggable) {
126 // Debuggable property is set. Allow library providing JNI Invocation API to be overridden.
127
128 // Choose the library parameter (if provided).
129 if (library != NULL) {
130 return library;
131 }
132
133 // If the debug library is installed, use it.
134 // TODO(b/216099383): Do this in the test harness instead.
135 struct stat st;
136 if (stat(kDebugJniInvocationLibraryPath, &st) == 0) {
137 return kDebugJniInvocationLibrary;
138 } else if (errno != ENOENT) {
139 ALOGW("Failed to stat %s: %s", kDebugJniInvocationLibraryPath, strerror(errno));
140 }
141
142 // Choose the system_preferred_library (if provided).
143 if (system_preferred_library != NULL) {
144 return system_preferred_library;
145 }
146
147 }
148 return kDefaultJniInvocationLibrary;
149 }
150
JniInvocationGetLibrary(const char * library,char * buffer)151 const char* JniInvocationGetLibrary(const char* library, char* buffer) {
152 bool debuggable = IsDebuggable();
153 const char* system_preferred_library = NULL;
154 if (buffer != NULL && (GetLibrarySystemProperty(buffer) > 0)) {
155 system_preferred_library = buffer;
156 }
157 return JniInvocationGetLibraryWith(library, debuggable, system_preferred_library);
158 }
159
JniInvocationCreate()160 struct JniInvocationImpl* JniInvocationCreate() {
161 // Android only supports a single JniInvocation instance and only a single JavaVM.
162 if (g_impl.jni_provider_library != NULL) {
163 return NULL;
164 }
165 return &g_impl;
166 }
167
JniInvocationInit(struct JniInvocationImpl * instance,const char * library_name)168 bool JniInvocationInit(struct JniInvocationImpl* instance, const char* library_name) {
169 #ifdef __ANDROID__
170 char buffer[PROP_VALUE_MAX];
171 #else
172 char* buffer = NULL;
173 #endif
174 library_name = JniInvocationGetLibrary(library_name, buffer);
175 DlLibrary library = DlOpenLibrary(library_name);
176 if (library == NULL) {
177 if (strcmp(library_name, kDefaultJniInvocationLibrary) == 0) {
178 // Nothing else to try.
179 ALOGE("Failed to dlopen %s: %s", library_name, DlGetError());
180 return false;
181 }
182 // Note that this is enough to get something like the zygote
183 // running, we can't property_set here to fix this for the future
184 // because we are root and not the system user. See
185 // RuntimeInit.commonInit for where we fix up the property to
186 // avoid future fallbacks. http://b/11463182
187 ALOGW("Falling back from %s to %s after dlopen error: %s",
188 library_name, kDefaultJniInvocationLibrary, DlGetError());
189 library_name = kDefaultJniInvocationLibrary;
190 library = DlOpenLibrary(library_name);
191 if (library == NULL) {
192 ALOGE("Failed to dlopen %s: %s", library_name, DlGetError());
193 return false;
194 }
195 }
196
197 DlSymbol JNI_GetDefaultJavaVMInitArgs_ = FindSymbol(library, "JNI_GetDefaultJavaVMInitArgs");
198 if (JNI_GetDefaultJavaVMInitArgs_ == NULL) {
199 return false;
200 }
201
202 DlSymbol JNI_CreateJavaVM_ = FindSymbol(library, "JNI_CreateJavaVM");
203 if (JNI_CreateJavaVM_ == NULL) {
204 return false;
205 }
206
207 DlSymbol JNI_GetCreatedJavaVMs_ = FindSymbol(library, "JNI_GetCreatedJavaVMs");
208 if (JNI_GetCreatedJavaVMs_ == NULL) {
209 return false;
210 }
211
212 instance->jni_provider_library_name = library_name;
213 instance->jni_provider_library = library;
214 instance->JNI_GetDefaultJavaVMInitArgs = (jint (*)(void *)) JNI_GetDefaultJavaVMInitArgs_;
215 instance->JNI_CreateJavaVM = (jint (*)(JavaVM**, JNIEnv**, void*)) JNI_CreateJavaVM_;
216 instance->JNI_GetCreatedJavaVMs = (jint (*)(JavaVM**, jsize, jsize*)) JNI_GetCreatedJavaVMs_;
217
218 return true;
219 }
220
JniInvocationDestroy(struct JniInvocationImpl * instance)221 void JniInvocationDestroy(struct JniInvocationImpl* instance) {
222 DlCloseLibrary(instance->jni_provider_library);
223 memset(instance, 0, sizeof(*instance));
224 }
225