src/jdk.crypto.cryptoki/unix/native/libj2pkcs11/j2secmod_md.c
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     1
/*
40563
4bff68330f39 8074838: Resolve disabled warnings for libj2pkcs11
ascarpino
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     4
 *
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    10
 *
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    15
 * accompanied this code).
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    16
 *
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    20
 *
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    23
 * questions.
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    24
 */
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    25
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    26
#include <stdio.h>
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    27
#include <stdlib.h>
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    28
#include <string.h>
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    29
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    30
#include <dlfcn.h>
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    31
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    32
#include <jni_util.h>
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    33
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    34
#include "j2secmod.h"
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    35
#include "pkcs11wrapper.h"
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    36
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    37
void *findFunction(JNIEnv *env, jlong jHandle, const char *functionName) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    38
    void *hModule = (void*)jlong_to_ptr(jHandle);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    39
    void *fAddress = dlsym(hModule, functionName);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    40
    if (fAddress == NULL) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    41
        char errorMessage[256];
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    42
        snprintf(errorMessage, sizeof(errorMessage), "Symbol not found: %s", functionName);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    43
        throwNullPointerException(env, errorMessage);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    44
        return NULL;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    45
    }
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    46
    return fAddress;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    47
}
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    48
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    49
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_Secmod_nssGetLibraryHandle
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    50
  (JNIEnv *env, jclass thisClass, jstring jLibName)
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    51
{
40563
4bff68330f39 8074838: Resolve disabled warnings for libj2pkcs11
ascarpino
parents: 25859
diff changeset
    52
    void *hModule;
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    53
    const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    54
    if (libName == NULL) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    55
        return 0L;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    56
    }
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    57
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    58
    // look up existing handle only, do not load
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    59
#if defined(AIX)
40563
4bff68330f39 8074838: Resolve disabled warnings for libj2pkcs11
ascarpino
parents: 25859
diff changeset
    60
    hModule = dlopen(libName, RTLD_LAZY);
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    61
#else
40563
4bff68330f39 8074838: Resolve disabled warnings for libj2pkcs11
ascarpino
parents: 25859
diff changeset
    62
    hModule = dlopen(libName, RTLD_NOLOAD);
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    63
#endif
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    64
    dprintf2("-handle for %s: %u\n", libName, hModule);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    65
    (*env)->ReleaseStringUTFChars(env, jLibName, libName);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    66
    return ptr_to_jlong(hModule);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    67
}
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    68
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    69
JNIEXPORT jlong JNICALL Java_sun_security_pkcs11_Secmod_nssLoadLibrary
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    70
  (JNIEnv *env, jclass thisClass, jstring jLibName)
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    71
{
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    72
    void *hModule;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    73
    const char *libName = (*env)->GetStringUTFChars(env, jLibName, NULL);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    74
    if (libName == NULL) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    75
       return 0L;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    76
    }
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    77
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    78
    dprintf1("-lib %s\n", libName);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    79
    hModule = dlopen(libName, RTLD_LAZY);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    80
    (*env)->ReleaseStringUTFChars(env, jLibName, libName);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    81
    dprintf2("-handle: %u (0X%X)\n", hModule, hModule);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    82
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    83
    if (hModule == NULL) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    84
        throwIOException(env, dlerror());
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    85
        return 0;
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    86
    }
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    87
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    88
    return ptr_to_jlong(hModule);
3317bb8137f4 8054834: Modular Source Code
chegar
parents:
diff changeset
    89
}