< prev index next >

src/java.base/share/native/libjava/NativeLibraries.c

Print this page




  92         jniFunctionName = malloc(len);
  93         if (jniFunctionName == NULL) {
  94             JNU_ThrowOutOfMemoryError(env, NULL);
  95             goto done;
  96         }
  97         buildJniFunctionName(syms[i], cname, jniFunctionName);
  98         entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
  99         free(jniFunctionName);
 100         if(entryName) {
 101             break;
 102         }
 103     }
 104 
 105  done:
 106     return entryName;
 107 }
 108 
 109 /*
 110  * Class:     jdk_internal_loader_NativeLibraries
 111  * Method:    load
 112  * Signature: (Ljava/lang/String;Z)Z
 113  */
 114 JNIEXPORT jboolean JNICALL
 115 Java_jdk_internal_loader_NativeLibraries_load
 116   (JNIEnv *env, jobject this, jobject lib, jstring name, jboolean isBuiltin)
 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);

 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             }


 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     (*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
 177     loaded = JNI_TRUE;
 178 
 179  done:
 180     JNU_ReleaseStringPlatformChars(env, name, cname);
 181     return loaded;
 182 }
 183 
 184 /*
 185  * Class:     jdk_internal_loader_NativeLibraries
 186  * Method:    unload
 187  * Signature: (Ljava/lang/String;ZJ)V
 188  */
 189 JNIEXPORT void JNICALL
 190 Java_jdk_internal_loader_NativeLibraries_unload
 191 (JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jlong address)
 192 {
 193     const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
 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     JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
 206                                                     isBuiltin ? cname : NULL,
 207                                                     JNI_FALSE);
 208     if (JNI_OnUnload) {
 209         JavaVM *jvm;
 210         (*env)->GetJavaVM(env, &jvm);
 211         (*JNI_OnUnload)(jvm, NULL);
 212     }

 213     if (!isBuiltin) {
 214         JVM_UnloadLibrary(handle);
 215     }
 216     JNU_ReleaseStringPlatformChars(env, name, cname);
 217 }
 218 
 219 
 220 /*
 221  * Class:     jdk_internal_loader_NativeLibraries
 222  * Method:    findEntry0
 223  * Signature: (Ljava/lang/String;)J
 224  */
 225 JNIEXPORT jlong JNICALL
 226 Java_jdk_internal_loader_NativeLibraries_findEntry0
 227   (JNIEnv *env, jobject this, jobject lib, jstring name)
 228 {
 229     jlong handle;
 230     const char *cname;
 231     jlong res;
 232 


 282         return NULL;
 283     }
 284     if (len > prefixLen) {
 285         strcpy(libName, cname+prefixLen);
 286     }
 287     JNU_ReleaseStringPlatformChars(env, name, cname);
 288 
 289     // Strip SUFFIX
 290     libName[strlen(libName)-suffixLen] = '\0';
 291 
 292     // Check for JNI_OnLoad_libname function
 293     ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
 294     if (ret != NULL) {
 295         lib = JNU_NewStringPlatform(env, libName);
 296         free(libName);
 297         return lib;
 298     }
 299     free(libName);
 300     return NULL;
 301 }
 302 
 303 


  92         jniFunctionName = malloc(len);
  93         if (jniFunctionName == NULL) {
  94             JNU_ThrowOutOfMemoryError(env, NULL);
  95             goto done;
  96         }
  97         buildJniFunctionName(syms[i], cname, jniFunctionName);
  98         entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
  99         free(jniFunctionName);
 100         if(entryName) {
 101             break;
 102         }
 103     }
 104 
 105  done:
 106     return entryName;
 107 }
 108 
 109 /*
 110  * Class:     jdk_internal_loader_NativeLibraries
 111  * Method:    load
 112  * Signature: (Ljava/lang/String;ZZ)Z
 113  */
 114 JNIEXPORT jboolean JNICALL
 115 Java_jdk_internal_loader_NativeLibraries_load
 116   (JNIEnv *env, jobject this, jobject lib, jstring name, jboolean isBuiltin, jboolean isJNI)
 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);
 131     if (isJNI) {
 132         if (handle) {
 133             JNI_OnLoad_t JNI_OnLoad;
 134             JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
 135                                                        isBuiltin ? cname : NULL,
 136                                                        JNI_TRUE);
 137             if (JNI_OnLoad) {
 138                 JavaVM *jvm;
 139                 (*env)->GetJavaVM(env, &jvm);
 140                 jniVersion = (*JNI_OnLoad)(jvm, NULL);
 141             } else {
 142                 jniVersion = 0x00010001;
 143             }
 144     
 145             cause = (*env)->ExceptionOccurred(env);
 146             if (cause) {
 147                 (*env)->ExceptionClear(env);
 148                 (*env)->Throw(env, cause);
 149                 if (!isBuiltin) {
 150                     JVM_UnloadLibrary(handle);
 151                 }


 157                 char msg[256];
 158                 jio_snprintf(msg, sizeof(msg),
 159                              "unsupported JNI version 0x%08X required by %s",
 160                              jniVersion, cname);
 161                 JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
 162                 if (!isBuiltin) {
 163                     JVM_UnloadLibrary(handle);
 164                 }
 165                 goto done;
 166             }
 167             (*env)->SetIntField(env, lib, jniVersionID, jniVersion);
 168         } else {
 169             cause = (*env)->ExceptionOccurred(env);
 170             if (cause) {
 171                 (*env)->ExceptionClear(env);
 172                 (*env)->SetLongField(env, lib, handleID, (jlong)0);
 173                 (*env)->Throw(env, cause);
 174             }
 175             goto done;
 176         }
 177     }
 178     (*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
 179     loaded = JNI_TRUE;
 180 
 181  done:
 182     JNU_ReleaseStringPlatformChars(env, name, cname);
 183     return loaded;
 184 }
 185 
 186 /*
 187  * Class:     jdk_internal_loader_NativeLibraries
 188  * Method:    unload
 189  * Signature: (Ljava/lang/String;ZZJ)V
 190  */
 191 JNIEXPORT void JNICALL
 192 Java_jdk_internal_loader_NativeLibraries_unload
 193 (JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jboolean isJNI, jlong address)
 194 {
 195     const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
 196     void *handle;
 197     JNI_OnUnload_t JNI_OnUnload;
 198     const char *cname;
 199 
 200     if (!initIDs(env))
 201         return;
 202     cname = JNU_GetStringPlatformChars(env, name, 0);
 203     if (cname == NULL) {
 204         return;
 205     }
 206     handle = jlong_to_ptr(address);
 207     if (isJNI) {
 208         JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
 209                                                         isBuiltin ? cname : NULL,
 210                                                         JNI_FALSE);
 211         if (JNI_OnUnload) {
 212             JavaVM *jvm;
 213             (*env)->GetJavaVM(env, &jvm);
 214             (*JNI_OnUnload)(jvm, NULL);
 215         }
 216     }
 217     if (!isBuiltin) {
 218         JVM_UnloadLibrary(handle);
 219     }
 220     JNU_ReleaseStringPlatformChars(env, name, cname);
 221 }
 222 
 223 
 224 /*
 225  * Class:     jdk_internal_loader_NativeLibraries
 226  * Method:    findEntry0
 227  * Signature: (Ljava/lang/String;)J
 228  */
 229 JNIEXPORT jlong JNICALL
 230 Java_jdk_internal_loader_NativeLibraries_findEntry0
 231   (JNIEnv *env, jobject this, jobject lib, jstring name)
 232 {
 233     jlong handle;
 234     const char *cname;
 235     jlong res;
 236 


 286         return NULL;
 287     }
 288     if (len > prefixLen) {
 289         strcpy(libName, cname+prefixLen);
 290     }
 291     JNU_ReleaseStringPlatformChars(env, name, cname);
 292 
 293     // Strip SUFFIX
 294     libName[strlen(libName)-suffixLen] = '\0';
 295 
 296     // Check for JNI_OnLoad_libname function
 297     ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
 298     if (ret != NULL) {
 299         lib = JNU_NewStringPlatform(env, libName);
 300         free(libName);
 301         return lib;
 302     }
 303     free(libName);
 304     return NULL;
 305 }


< prev index next >