jdk/src/share/native/java/lang/ClassLoader.c
author mchung
Tue, 16 Apr 2013 21:39:52 -0700
changeset 16906 44dfee24cb71
parent 16506 2a43ffb8a318
child 22641 e47f8892133e
permissions -rw-r--r--
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive Reviewed-by: jrose, alanb, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3833
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3833
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3833
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3833
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3833
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "java_lang_ClassLoader.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_lang_ClassLoader_NativeLibrary.h"
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
    35
#include <string.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* defined in libverify.so/verify.dll (src file common/check_format.c) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
extern jboolean VerifyFixClassname(char *utf_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
static JNINativeMethod methods[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    {"retrieveDirectives",  "()Ljava/lang/AssertionStatusDirectives;", (void *)&JVM_AssertionStatusDirectives}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
Java_java_lang_ClassLoader_registerNatives(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    (*env)->RegisterNatives(env, cls, methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                            sizeof(methods)/sizeof(JNINativeMethod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* Convert java string to UTF char*. Use local buffer if possible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
   otherwise malloc new memory. Returns null IFF malloc failed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
getUTF(JNIEnv *env, jstring str, char* localBuf, int bufSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    char* utfStr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    int len = (*env)->GetStringUTFLength(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int unicode_len = (*env)->GetStringLength(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    if (len >= bufSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        utfStr = malloc(len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (utfStr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        utfStr = localBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    (*env)->GetStringUTFRegion(env, str, 0, unicode_len, utfStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    return utfStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
// The existence or signature of this method is not guaranteed since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
// supports a private method.  This method will be changed in 1.7.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
Java_java_lang_ClassLoader_defineClass0(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                        jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                        jstring name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                        jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                        jint offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                        jint length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                        jobject pd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    return Java_java_lang_ClassLoader_defineClass1(env, loader, name, data, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                   length, pd, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
Java_java_lang_ClassLoader_defineClass1(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                        jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                        jstring name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                        jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                        jint offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                        jint length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                        jobject pd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                        jstring source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    jbyte *body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    char *utfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jclass result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    char* utfSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    char sourceBuf[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (data == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /* Work around 4153825. malloc crashes on Solaris when passed a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * negative size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if (length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        JNU_ThrowArrayIndexOutOfBoundsException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    body = (jbyte *)malloc(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    if (body == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    (*env)->GetByteArrayRegion(env, data, offset, length, body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if ((*env)->ExceptionOccurred(env))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        goto free_body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    if (name != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        utfName = getUTF(env, name, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (utfName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            goto free_body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        VerifyFixClassname(utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        utfName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    if (source != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (utfSource == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            goto free_utfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        utfSource = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    if (utfSource && utfSource != sourceBuf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        free(utfSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 free_utfName:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (utfName && utfName != buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        free(utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 free_body:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    free(body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
Java_java_lang_ClassLoader_defineClass2(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                        jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                        jstring name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                        jobject data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                        jint offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                        jint length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                        jobject pd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                        jstring source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    jbyte *body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    char *utfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    jclass result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    char* utfSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    char sourceBuf[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    assert(data != NULL); // caller fails if data is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    assert(length >= 0);  // caller passes ByteBuffer.remaining() for length, so never neg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // caller passes ByteBuffer.position() for offset, and capacity() >= position() + remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    assert((*env)->GetDirectBufferCapacity(env, data) >= (offset + length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    body = (*env)->GetDirectBufferAddress(env, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (body == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    body += offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    if (name != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        utfName = getUTF(env, name, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (utfName == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        VerifyFixClassname(utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        utfName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    if (source != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (utfSource == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            goto free_utfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        utfSource = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    if (utfSource && utfSource != sourceBuf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        free(utfSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 free_utfName:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if (utfName && utfName != buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        free(utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
Java_java_lang_ClassLoader_resolveClass0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                         jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    JVM_ResolveClass(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 2448
diff changeset
   241
/*
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 2448
diff changeset
   242
 * Returns NULL if class not found.
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 2448
diff changeset
   243
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                              jstring classname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    char *clname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    if (classname == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    clname = getUTF(env, classname, buf, sizeof(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    if (clname == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    VerifyFixClassname(clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if (!VerifyClassname(clname, JNI_TRUE)) {  /* expects slashed name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
3833
b666ed188811 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents: 2448
diff changeset
   267
    cls = JVM_FindClassFromBootLoader(env, clname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if (clname != buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        free(clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
Java_java_lang_ClassLoader_findLoadedClass0(JNIEnv *env, jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                           jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    if (name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return JVM_FindLoadedClass(env, loader, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
static jfieldID handleID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
static jfieldID jniVersionID;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   290
static jfieldID loadedID;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   291
static void *procHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
static jboolean initIDs(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    if (handleID == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        jclass this =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            (*env)->FindClass(env, "java/lang/ClassLoader$NativeLibrary");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (this == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        handleID = (*env)->GetFieldID(env, this, "handle", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (handleID == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        jniVersionID = (*env)->GetFieldID(env, this, "jniVersion", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (jniVersionID == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return JNI_FALSE;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   306
        loadedID = (*env)->GetFieldID(env, this, "loaded", "Z");
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   307
        if (loadedID == 0)
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   308
             return JNI_FALSE;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   309
        procHandle = getProcessHandle();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
/*
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   318
 * Support for finding JNI_On(Un)Load_<lib_name> if it exists.
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   319
 * If cname == NULL then just find normal JNI_On(Un)Load entry point
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   320
 */
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   321
static void *findJniFunction(JNIEnv *env, void *handle,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   322
                                    const char *cname, jboolean isLoad) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   323
    const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   324
    const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   325
    const char **syms;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   326
    int symsLen;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   327
    void *entryName = NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   328
    char *jniFunctionName;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   329
    int i;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   330
    int len;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   331
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   332
    // Check for JNI_On(Un)Load<_libname> function
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   333
    if (isLoad) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   334
        syms = onLoadSymbols;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   335
        symsLen = sizeof(onLoadSymbols) / sizeof(char *);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   336
    } else {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   337
        syms = onUnloadSymbols;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   338
        symsLen = sizeof(onUnloadSymbols) / sizeof(char *);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   339
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   340
    for (i = 0; i < symsLen; i++) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   341
        // cname + sym + '_' + '\0'
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   342
        if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   343
            FILENAME_MAX) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   344
            goto done;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   345
        }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   346
        jniFunctionName = malloc(len);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   347
        if (jniFunctionName == NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   348
            JNU_ThrowOutOfMemoryError(env, NULL);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   349
            goto done;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   350
        }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   351
        buildJniFunctionName(syms[i], cname, jniFunctionName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   352
        entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   353
        free(jniFunctionName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   354
        if(entryName) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   355
            break;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   356
        }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   357
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   358
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   359
 done:
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   360
    return entryName;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   361
}
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   362
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   363
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 * Class:     java_lang_ClassLoader_NativeLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 * Method:    load
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   366
 * Signature: (Ljava/lang/String;Z)V
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
Java_java_lang_ClassLoader_00024NativeLibrary_load
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   370
  (JNIEnv *env, jobject this, jstring name, jboolean isBuiltin)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    const char *cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    jint jniVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    jthrowable cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    void * handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    if (!initIDs(env))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    cname = JNU_GetStringPlatformChars(env, name, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    if (cname == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   383
    handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    if (handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        JNI_OnLoad_t JNI_OnLoad;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   386
        JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   387
                                               isBuiltin ? cname : NULL,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   388
                                               JNI_TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (JNI_OnLoad) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            (*env)->GetJavaVM(env, &jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            jniVersion = (*JNI_OnLoad)(jvm, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            jniVersion = 0x00010001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        cause = (*env)->ExceptionOccurred(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            (*env)->ExceptionClear(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            (*env)->Throw(env, cause);
16506
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   401
            if (!isBuiltin) {
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   402
                JVM_UnloadLibrary(handle);
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   403
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   407
        if (!JVM_IsSupportedJNIVersion(jniVersion) ||
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   408
            (isBuiltin && jniVersion < JNI_VERSION_1_8)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            char msg[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            jio_snprintf(msg, sizeof(msg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                         "unsupported JNI version 0x%08X required by %s",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                         jniVersion, cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
16506
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   414
            if (!isBuiltin) {
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   415
                JVM_UnloadLibrary(handle);
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   416
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        (*env)->SetIntField(env, this, jniVersionID, jniVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        cause = (*env)->ExceptionOccurred(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            (*env)->ExceptionClear(env);
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   424
            (*env)->SetLongField(env, this, handleID, (jlong)0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            (*env)->Throw(env, cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    (*env)->SetLongField(env, this, handleID, ptr_to_jlong(handle));
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   430
    (*env)->SetBooleanField(env, this, loadedID, JNI_TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    JNU_ReleaseStringPlatformChars(env, name, cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 * Class:     java_lang_ClassLoader_NativeLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 * Method:    unload
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   439
 * Signature: (Z)V
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
Java_java_lang_ClassLoader_00024NativeLibrary_unload
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   443
(JNIEnv *env, jobject this, jstring name, jboolean isBuiltin)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    void *handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    JNI_OnUnload_t JNI_OnUnload;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   448
     const char *cname;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    if (!initIDs(env))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return;
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   452
    cname = JNU_GetStringPlatformChars(env, name, 0);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   453
    if (cname == NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   454
        return;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   455
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    handle = jlong_to_ptr((*env)->GetLongField(env, this, handleID));
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   457
    JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   458
                                                isBuiltin ? cname : NULL,
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   459
                                                JNI_FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    if (JNI_OnUnload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        JavaVM *jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        (*env)->GetJavaVM(env, &jvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        (*JNI_OnUnload)(jvm, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
16506
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   465
    if (!isBuiltin) {
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   466
        JVM_UnloadLibrary(handle);
2a43ffb8a318 8010668: builtin JNI libraries should not be unloaded
chegar
parents: 16479
diff changeset
   467
    }
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   468
    JNU_ReleaseStringPlatformChars(env, name, cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 * Class:     java_lang_ClassLoader_NativeLibrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * Method:    find
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   474
 * Signature: (Ljava/lang/String;)J
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
Java_java_lang_ClassLoader_00024NativeLibrary_find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
  (JNIEnv *env, jobject this, jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    jlong handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    const char *cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    jlong res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    if (!initIDs(env))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    handle = (*env)->GetLongField(env, this, handleID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    cname = (*env)->GetStringUTFChars(env, name, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    if (cname == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return jlong_zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    (*env)->ReleaseStringUTFChars(env, name, cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
16479
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   495
/*
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   496
 * Class:     java_lang_ClassLoader_NativeLibrary
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   497
 * Method:    findBuiltinLib
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   498
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   499
 */
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   500
JNIEXPORT jstring JNICALL
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   501
Java_java_lang_ClassLoader_00024NativeLibrary_findBuiltinLib
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   502
  (JNIEnv *env, jclass cls, jstring name)
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   503
{
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   504
    const char *cname;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   505
    char *libName;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   506
    int prefixLen = (int) strlen(JNI_LIB_PREFIX);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   507
    int suffixLen = (int) strlen(JNI_LIB_SUFFIX);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   508
    int len;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   509
    jstring lib;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   510
    void *ret;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   511
    const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   512
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   513
    if (name == NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   514
        JNU_ThrowInternalError(env, "NULL filename for native library");
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   515
        return NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   516
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   517
    // Can't call initIDs because it will recurse into NativeLibrary via
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   518
    // FindClass to check context so set prochandle here as well.
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   519
    procHandle = getProcessHandle();
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   520
    cname = JNU_GetStringPlatformChars(env, name, 0);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   521
    if (cname == NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   522
        JNU_ThrowOutOfMemoryError(env, NULL);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   523
        return NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   524
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   525
    // Copy name Skipping PREFIX
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   526
    len = strlen(cname);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   527
    if (len <= (prefixLen+suffixLen)) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   528
        JNU_ReleaseStringPlatformChars(env, name, cname);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   529
        return NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   530
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   531
    libName = malloc(len + 1); //+1 for null if prefix+suffix == 0
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   532
    if (libName == NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   533
        JNU_ReleaseStringPlatformChars(env, name, cname);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   534
        JNU_ThrowOutOfMemoryError(env, NULL);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   535
        return NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   536
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   537
    if (len > prefixLen) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   538
        strcpy(libName, cname+prefixLen);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   539
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   540
    JNU_ReleaseStringPlatformChars(env, name, cname);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   541
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   542
    // Strip SUFFIX
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   543
    libName[strlen(libName)-suffixLen] = '\0';
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   544
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   545
    // Check for JNI_OnLoad_libname function
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   546
    ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   547
    if (ret != NULL) {
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   548
        lib = JNU_NewStringPlatform(env, libName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   549
        free(libName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   550
        return lib;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   551
    }
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   552
    free(libName);
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   553
    return NULL;
d845c18d13f2 8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents: 7668
diff changeset
   554
}