Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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 | |
Orion Hodson | f839d79 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 17 | #include "include_platform/nativehelper/JniInvocation.h" |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 18 | |
| 19 | #define LOG_TAG "JniInvocation" |
Orion Hodson | f839d79 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 20 | #include "ALog-priv.h" |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 21 | |
| 22 | #if defined(__ANDROID__) |
| 23 | #include <sys/system_properties.h> |
| 24 | #endif |
| 25 | |
Nicolas Geoffray | 944b109 | 2022-01-12 14:45:38 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Orion Hodson | f839d79 | 2020-06-02 12:18:23 +0100 | [diff] [blame] | 27 | #include <jni.h> |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 28 | #include <stdbool.h> |
| 29 | #include <string.h> |
Nicolas Geoffray | 944b109 | 2022-01-12 14:45:38 +0000 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 33 | |
| 34 | #include "DlHelp.h" |
| 35 | |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 36 | // Name the default library providing the JNI Invocation API. |
| 37 | static const char* kDefaultJniInvocationLibrary = "libart.so"; |
Nicolas Geoffray | 944b109 | 2022-01-12 14:45:38 +0000 | [diff] [blame] | 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"; |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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 | |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 67 | static bool IsDebuggable() { |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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 |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 73 | // Host is always treated as debuggable, which allows choice of library to be overridden. |
| 74 | return true; |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
| 78 | static int GetLibrarySystemProperty(char* buffer) { |
| 79 | #ifdef __ANDROID__ |
| 80 | return __system_property_get("persist.sys.dalvik.vm.lib.2", buffer); |
| 81 | #else |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 82 | // Host does not use properties. |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 83 | UNUSED(buffer); |
| 84 | return 0; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 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 | |
| 100 | jint JNI_GetDefaultJavaVMInitArgs(void* vmargs) { |
Santiago Seifert | d6ec41b | 2020-12-04 22:32:02 +0000 | [diff] [blame] | 101 | ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_GetDefaultJavaVMInitArgs, "Runtime library not loaded."); |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 102 | return g_impl.JNI_GetDefaultJavaVMInitArgs(vmargs); |
| 103 | } |
| 104 | |
| 105 | jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { |
Santiago Seifert | d6ec41b | 2020-12-04 22:32:02 +0000 | [diff] [blame] | 106 | ALOG_ALWAYS_FATAL_IF(NULL == g_impl.JNI_CreateJavaVM, "Runtime library not loaded."); |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 107 | return g_impl.JNI_CreateJavaVM(p_vm, p_env, vm_args); |
| 108 | } |
| 109 | |
| 110 | jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) { |
Santiago Seifert | d6ec41b | 2020-12-04 22:32:02 +0000 | [diff] [blame] | 111 | if (NULL == g_impl.JNI_GetCreatedJavaVMs) { |
| 112 | *vm_count = 0; |
| 113 | return JNI_OK; |
| 114 | } |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 115 | return g_impl.JNI_GetCreatedJavaVMs(vms, size, vm_count); |
| 116 | } |
| 117 | |
| 118 | // |
| 119 | // JniInvocation functions for setting up JNI functions. |
| 120 | // |
| 121 | |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 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. |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 127 | |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 128 | // Choose the library parameter (if provided). |
| 129 | if (library != NULL) { |
| 130 | return library; |
| 131 | } |
Nicolas Geoffray | 944b109 | 2022-01-12 14:45:38 +0000 | [diff] [blame] | 132 | |
Nicolas Geoffray | 8f9ac54 | 2022-01-24 14:06:58 +0000 | [diff] [blame] | 133 | // If the debug library is installed, use it. |
| 134 | // TODO(b/216099383): Do this in the test harness instead. |
Nicolas Geoffray | 944b109 | 2022-01-12 14:45:38 +0000 | [diff] [blame] | 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 | } |
Nicolas Geoffray | 8f9ac54 | 2022-01-24 14:06:58 +0000 | [diff] [blame] | 141 | |
| 142 | // Choose the system_preferred_library (if provided). |
| 143 | if (system_preferred_library != NULL) { |
| 144 | return system_preferred_library; |
| 145 | } |
| 146 | |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 147 | } |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 148 | return kDefaultJniInvocationLibrary; |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | const char* JniInvocationGetLibrary(const char* library, char* buffer) { |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 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); |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 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 | |
Orion Hodson | 962ee00 | 2020-04-19 09:45:28 +0100 | [diff] [blame] | 168 | bool JniInvocationInit(struct JniInvocationImpl* instance, const char* library_name) { |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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) { |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 177 | if (strcmp(library_name, kDefaultJniInvocationLibrary) == 0) { |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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", |
Orion Hodson | 0e4dff8 | 2020-04-27 14:01:20 +0100 | [diff] [blame] | 188 | library_name, kDefaultJniInvocationLibrary, DlGetError()); |
| 189 | library_name = kDefaultJniInvocationLibrary; |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 190 | library = DlOpenLibrary(library_name); |
| 191 | if (library == NULL) { |
Stephen Hines | d6dbc47 | 2020-07-14 12:25:36 -0700 | [diff] [blame] | 192 | ALOGE("Failed to dlopen %s: %s", library_name, DlGetError()); |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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 | |
Orion Hodson | 6af7525 | 2020-05-01 16:27:02 +0100 | [diff] [blame] | 207 | DlSymbol JNI_GetCreatedJavaVMs_ = FindSymbol(library, "JNI_GetCreatedJavaVMs"); |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 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_; |
Orion Hodson | b8cb9f4 | 2020-05-07 18:32:02 +0100 | [diff] [blame] | 216 | instance->JNI_GetCreatedJavaVMs = (jint (*)(JavaVM**, jsize, jsize*)) JNI_GetCreatedJavaVMs_; |
Orion Hodson | 85a2032 | 2020-04-21 13:34:14 +0100 | [diff] [blame] | 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void JniInvocationDestroy(struct JniInvocationImpl* instance) { |
| 222 | DlCloseLibrary(instance->jni_provider_library); |
| 223 | memset(instance, 0, sizeof(*instance)); |
Orion Hodson | 7e438db | 2020-04-19 16:39:50 +0100 | [diff] [blame] | 224 | } |