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