jdk

Форк
0
/
NativeLibraries.c 
295 строк · 8.9 Кб
1
/*
2
 * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE 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
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
 * or visit www.oracle.com if you need additional information or have any
23
 * questions.
24
 */
25

26
#include <stdlib.h>
27
#include <assert.h>
28
#include <string.h>
29

30
#include "jni.h"
31
#include "jni_util.h"
32
#include "jlong.h"
33
#include "jvm.h"
34
#include "jdk_internal_loader_NativeLibraries.h"
35

36
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
37
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);
38

39
static jfieldID handleID;
40
static jfieldID jniVersionID;
41
static void *procHandle;
42

43
static jboolean initIDs(JNIEnv *env)
44
{
45
    if (handleID == 0) {
46
        jclass nlClz =
47
            (*env)->FindClass(env, "jdk/internal/loader/NativeLibraries$NativeLibraryImpl");
48
        if (nlClz == 0)
49
            return JNI_FALSE;
50
        handleID = (*env)->GetFieldID(env, nlClz, "handle", "J");
51
        if (handleID == 0)
52
            return JNI_FALSE;
53
        jniVersionID = (*env)->GetFieldID(env, nlClz, "jniVersion", "I");
54
        if (jniVersionID == 0)
55
            return JNI_FALSE;
56
        procHandle = getProcessHandle();
57
    }
58
    return JNI_TRUE;
59
}
60

61

62
/*
63
 * Support for finding JNI_On(Un)Load_<lib_name> if it exists.
64
 * If cname == NULL then just find normal JNI_On(Un)Load entry point
65
 */
66
static void *findJniFunction(JNIEnv *env, void *handle,
67
                                    const char *cname, jboolean isLoad) {
68
    const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
69
    const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
70
    const char **syms;
71
    int symsLen;
72
    void *entryName = NULL;
73
    char *jniFunctionName;
74
    int i;
75
    size_t len;
76

77
    // Check for JNI_On(Un)Load<_libname> function
78
    if (isLoad) {
79
        syms = onLoadSymbols;
80
        symsLen = sizeof(onLoadSymbols) / sizeof(char *);
81
    } else {
82
        syms = onUnloadSymbols;
83
        symsLen = sizeof(onUnloadSymbols) / sizeof(char *);
84
    }
85
    for (i = 0; i < symsLen; i++) {
86
        // cname + sym + '_' + '\0'
87
        if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >
88
            FILENAME_MAX) {
89
            goto done;
90
        }
91
        jniFunctionName = malloc(len);
92
        if (jniFunctionName == NULL) {
93
            JNU_ThrowOutOfMemoryError(env, NULL);
94
            goto done;
95
        }
96
        buildJniFunctionName(syms[i], cname, jniFunctionName);
97
        entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
98
        free(jniFunctionName);
99
        if(entryName) {
100
            break;
101
        }
102
    }
103

104
 done:
105
    return entryName;
106
}
107

108
/*
109
 * Class:     jdk_internal_loader_NativeLibraries
110
 * Method:    load
111
 * Signature: (Ljdk/internal/loader/NativeLibraries/NativeLibraryImpl;Ljava/lang/String;ZZ)Z
112
 */
113
JNIEXPORT jboolean JNICALL
114
Java_jdk_internal_loader_NativeLibraries_load
115
  (JNIEnv *env, jclass cls, jobject lib, jstring name,
116
   jboolean isBuiltin, jboolean throwExceptionIfFail)
117
{
118
    const char *cname;
119
    jint jniVersion;
120
    jthrowable cause;
121
    void * handle;
122
    jboolean loaded = JNI_FALSE;
123

124
    if (!initIDs(env))
125
        return JNI_FALSE;
126

127
    cname = JNU_GetStringPlatformChars(env, name, 0);
128
    if (cname == 0)
129
        return JNI_FALSE;
130
    handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
131
    if (handle) {
132
        JNI_OnLoad_t JNI_OnLoad;
133
        JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
134
                                                   isBuiltin ? cname : NULL,
135
                                                   JNI_TRUE);
136
        if (JNI_OnLoad) {
137
            JavaVM *jvm;
138
            (*env)->GetJavaVM(env, &jvm);
139
            jniVersion = (*JNI_OnLoad)(jvm, NULL);
140
        } else {
141
            jniVersion = 0x00010001;
142
        }
143

144
        cause = (*env)->ExceptionOccurred(env);
145
        if (cause) {
146
            (*env)->ExceptionClear(env);
147
            (*env)->Throw(env, cause);
148
            if (!isBuiltin) {
149
                JVM_UnloadLibrary(handle);
150
            }
151
            goto done;
152
        }
153

154
        if (!JVM_IsSupportedJNIVersion(jniVersion) ||
155
            (isBuiltin && jniVersion < JNI_VERSION_1_8)) {
156
            char msg[256];
157
            jio_snprintf(msg, sizeof(msg),
158
                         "unsupported JNI version 0x%08X required by %s",
159
                         jniVersion, cname);
160
            JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
161
            if (!isBuiltin) {
162
                JVM_UnloadLibrary(handle);
163
            }
164
            goto done;
165
        }
166
        (*env)->SetIntField(env, lib, jniVersionID, jniVersion);
167
    } else {
168
        cause = (*env)->ExceptionOccurred(env);
169
        if (cause) {
170
            (*env)->ExceptionClear(env);
171
            (*env)->SetLongField(env, lib, handleID, (jlong)0);
172
            (*env)->Throw(env, cause);
173
        }
174
        goto done;
175
    }
176

177
    (*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
178
    loaded = JNI_TRUE;
179

180
 done:
181
    JNU_ReleaseStringPlatformChars(env, name, cname);
182
    return loaded;
183
}
184

185
/*
186
 * Class:     jdk_internal_loader_NativeLibraries
187
 * Method:    unload
188
 * Signature: (Ljava/lang/String;ZJ)V
189
 */
190
JNIEXPORT void JNICALL
191
Java_jdk_internal_loader_NativeLibraries_unload
192
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jlong address)
193
{
194
    void *handle;
195
    JNI_OnUnload_t JNI_OnUnload;
196
    const char *cname;
197

198
    if (!initIDs(env))
199
        return;
200
    cname = JNU_GetStringPlatformChars(env, name, 0);
201
    if (cname == NULL) {
202
        return;
203
    }
204
    handle = jlong_to_ptr(address);
205

206
    JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
207
                                                    isBuiltin ? cname : NULL,
208
                                                    JNI_FALSE);
209
    if (JNI_OnUnload) {
210
        JavaVM *jvm;
211
        (*env)->GetJavaVM(env, &jvm);
212
        (*JNI_OnUnload)(jvm, NULL);
213
    }
214
    if (!isBuiltin) {
215
        JVM_UnloadLibrary(handle);
216
    }
217
    JNU_ReleaseStringPlatformChars(env, name, cname);
218
}
219

220
/*
221
 * Class:     jdk_internal_loader_NativeLibrary
222
 * Method:    findEntry0
223
 * Signature: (JLjava/lang/String;)J
224
 */
225
JNIEXPORT jlong JNICALL
226
Java_jdk_internal_loader_NativeLibrary_findEntry0
227
  (JNIEnv *env, jclass cls, jlong handle, jstring name)
228
{
229
    const char *cname;
230
    jlong res;
231

232
    cname = (*env)->GetStringUTFChars(env, name, 0);
233
    if (cname == 0)
234
        return jlong_zero;
235
    res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));
236
    (*env)->ReleaseStringUTFChars(env, name, cname);
237
    return res;
238
}
239

240
/*
241
 * Class:     jdk_internal_loader_NativeLibraries
242
 * Method:    findBuiltinLib
243
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
244
 */
245
JNIEXPORT jstring JNICALL
246
Java_jdk_internal_loader_NativeLibraries_findBuiltinLib
247
  (JNIEnv *env, jclass cls, jstring name)
248
{
249
    const char *cname;
250
    char *libName;
251
    size_t prefixLen = strlen(JNI_LIB_PREFIX);
252
    size_t suffixLen = strlen(JNI_LIB_SUFFIX);
253
    size_t len;
254
    jstring lib;
255
    void *ret;
256

257
    if (name == NULL) {
258
        JNU_ThrowInternalError(env, "NULL filename for native library");
259
        return NULL;
260
    }
261
    procHandle = getProcessHandle();
262
    cname = JNU_GetStringPlatformChars(env, name, 0);
263
    if (cname == NULL) {
264
        return NULL;
265
    }
266
    // Copy name Skipping PREFIX
267
    len = strlen(cname);
268
    if (len <= (prefixLen+suffixLen)) {
269
        JNU_ReleaseStringPlatformChars(env, name, cname);
270
        return NULL;
271
    }
272
    libName = malloc(len + 1); //+1 for null if prefix+suffix == 0
273
    if (libName == NULL) {
274
        JNU_ReleaseStringPlatformChars(env, name, cname);
275
        JNU_ThrowOutOfMemoryError(env, NULL);
276
        return NULL;
277
    }
278
    if (len > prefixLen) {
279
        strcpy(libName, cname+prefixLen);
280
    }
281
    JNU_ReleaseStringPlatformChars(env, name, cname);
282

283
    // Strip SUFFIX
284
    libName[strlen(libName)-suffixLen] = '\0';
285

286
    // Check for JNI_OnLoad_libname function
287
    ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
288
    if (ret != NULL) {
289
        lib = JNU_NewStringPlatform(env, libName);
290
        free(libName);
291
        return lib;
292
    }
293
    free(libName);
294
    return NULL;
295
}
296

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.