blob: 7effddb2918c5ebdc0fc47b3123270ca9dd4dc27 [file] [log] [blame]
Orion Hodson85a20322020-04-21 13:34:14 +01001/*
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
Orion Hodsonf839d792020-06-02 12:18:23 +010017#include "include_platform/nativehelper/JniInvocation.h"
Orion Hodson85a20322020-04-21 13:34:14 +010018
19#define LOG_TAG "JniInvocation"
Orion Hodsonf839d792020-06-02 12:18:23 +010020#include "ALog-priv.h"
Orion Hodson85a20322020-04-21 13:34:14 +010021
22#if defined(__ANDROID__)
23#include <sys/system_properties.h>
24#endif
25
Nicolas Geoffray944b1092022-01-12 14:45:38 +000026#include <errno.h>
Orion Hodsonf839d792020-06-02 12:18:23 +010027#include <jni.h>
Orion Hodson85a20322020-04-21 13:34:14 +010028#include <stdbool.h>
29#include <string.h>
Nicolas Geoffray944b1092022-01-12 14:45:38 +000030#include <sys/types.h>
31#include <sys/stat.h>
32#include <unistd.h>
Orion Hodson85a20322020-04-21 13:34:14 +010033
34#include "DlHelp.h"
35
Orion Hodson0e4dff82020-04-27 14:01:20 +010036// Name the default library providing the JNI Invocation API.
37static const char* kDefaultJniInvocationLibrary = "libart.so";
Nicolas Geoffray944b1092022-01-12 14:45:38 +000038static const char* kDebugJniInvocationLibrary = "libartd.so";
39#if defined(__LP64__)
40#define LIB_DIR "lib64"
41#else
42#define LIB_DIR "lib"
43#endif
44static const char* kDebugJniInvocationLibraryPath = "/apex/com.android.art/" LIB_DIR "/libartd.so";
Orion Hodson85a20322020-04-21 13:34:14 +010045
46struct 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
59static struct JniInvocationImpl g_impl;
60
61//
62// Internal helpers.
63//
64
65#define UNUSED(x) (x) = (x)
66
Orion Hodson0e4dff82020-04-27 14:01:20 +010067static bool IsDebuggable() {
Orion Hodson85a20322020-04-21 13:34:14 +010068#ifdef __ANDROID__
69 char debuggable[PROP_VALUE_MAX] = {0};
70 __system_property_get("ro.debuggable", debuggable);
71 return strcmp(debuggable, "1") == 0;
72#else
Orion Hodson0e4dff82020-04-27 14:01:20 +010073 // Host is always treated as debuggable, which allows choice of library to be overridden.
74 return true;
Orion Hodson85a20322020-04-21 13:34:14 +010075#endif
76}
77
78static int GetLibrarySystemProperty(char* buffer) {
79#ifdef __ANDROID__
80 return __system_property_get("persist.sys.dalvik.vm.lib.2", buffer);
81#else
Orion Hodson0e4dff82020-04-27 14:01:20 +010082 // Host does not use properties.
Orion Hodson85a20322020-04-21 13:34:14 +010083 UNUSED(buffer);
84 return 0;
85#endif
86}
87
88static 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
100jint JNI_GetDefaultJavaVMInitArgs(void* vmargs) {
Santiago Seifertd6ec41b2020-12-04 22:32:02 +0000101 ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_GetDefaultJavaVMInitArgs, "Runtime library not loaded.");
Orion Hodson85a20322020-04-21 13:34:14 +0100102 return g_impl.JNI_GetDefaultJavaVMInitArgs(vmargs);
103}
104
105jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Santiago Seifertd6ec41b2020-12-04 22:32:02 +0000106 ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_CreateJavaVM, "Runtime library not loaded.");
Orion Hodson85a20322020-04-21 13:34:14 +0100107 return g_impl.JNI_CreateJavaVM(p_vm, p_env, vm_args);
108}
109
110jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) {
Santiago Seifertd6ec41b2020-12-04 22:32:02 +0000111 if (NULL == g_impl.JNI_GetCreatedJavaVMs) {
112 *vm_count = 0;
113 return JNI_OK;
114 }
Orion Hodson85a20322020-04-21 13:34:14 +0100115 return g_impl.JNI_GetCreatedJavaVMs(vms, size, vm_count);
116}
117
118//
119// JniInvocation functions for setting up JNI functions.
120//
121
Orion Hodson0e4dff82020-04-27 14:01:20 +0100122const 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.
Orion Hodson85a20322020-04-21 13:34:14 +0100127
Orion Hodson0e4dff82020-04-27 14:01:20 +0100128 // Choose the library parameter (if provided).
129 if (library != NULL) {
130 return library;
131 }
Nicolas Geoffray944b1092022-01-12 14:45:38 +0000132
Nicolas Geoffray8f9ac542022-01-24 14:06:58 +0000133 // If the debug library is installed, use it.
134 // TODO(b/216099383): Do this in the test harness instead.
Nicolas Geoffray944b1092022-01-12 14:45:38 +0000135 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 }
Nicolas Geoffray8f9ac542022-01-24 14:06:58 +0000141
142 // Choose the system_preferred_library (if provided).
143 if (system_preferred_library != NULL) {
144 return system_preferred_library;
145 }
146
Orion Hodson85a20322020-04-21 13:34:14 +0100147 }
Orion Hodson0e4dff82020-04-27 14:01:20 +0100148 return kDefaultJniInvocationLibrary;
Orion Hodson85a20322020-04-21 13:34:14 +0100149}
150
151const char* JniInvocationGetLibrary(const char* library, char* buffer) {
Orion Hodson0e4dff82020-04-27 14:01:20 +0100152 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);
Orion Hodson85a20322020-04-21 13:34:14 +0100158}
159
160struct 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
Orion Hodson962ee002020-04-19 09:45:28 +0100168bool JniInvocationInit(struct JniInvocationImpl* instance, const char* library_name) {
Orion Hodson85a20322020-04-21 13:34:14 +0100169#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) {
Orion Hodson0e4dff82020-04-27 14:01:20 +0100177 if (strcmp(library_name, kDefaultJniInvocationLibrary) == 0) {
Orion Hodson85a20322020-04-21 13:34:14 +0100178 // 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",
Orion Hodson0e4dff82020-04-27 14:01:20 +0100188 library_name, kDefaultJniInvocationLibrary, DlGetError());
189 library_name = kDefaultJniInvocationLibrary;
Orion Hodson85a20322020-04-21 13:34:14 +0100190 library = DlOpenLibrary(library_name);
191 if (library == NULL) {
Stephen Hinesd6dbc472020-07-14 12:25:36 -0700192 ALOGE("Failed to dlopen %s: %s", library_name, DlGetError());
Orion Hodson85a20322020-04-21 13:34:14 +0100193 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
Orion Hodson6af75252020-05-01 16:27:02 +0100207 DlSymbol JNI_GetCreatedJavaVMs_ = FindSymbol(library, "JNI_GetCreatedJavaVMs");
Orion Hodson85a20322020-04-21 13:34:14 +0100208 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_;
Orion Hodsonb8cb9f42020-05-07 18:32:02 +0100216 instance->JNI_GetCreatedJavaVMs = (jint (*)(JavaVM**, jsize, jsize*)) JNI_GetCreatedJavaVMs_;
Orion Hodson85a20322020-04-21 13:34:14 +0100217
218 return true;
219}
220
221void JniInvocationDestroy(struct JniInvocationImpl* instance) {
222 DlCloseLibrary(instance->jni_provider_library);
223 memset(instance, 0, sizeof(*instance));
Orion Hodson7e438db2020-04-19 16:39:50 +0100224}