jdk/src/share/native/sun/security/pkcs11/wrapper/p11_mutex.c
changeset 2180 9994f4f08a59
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
2173:519bbd41590c 2180:9994f4f08a59
     1 /*
     1 /*
     2  * Portions Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Portions Copyright 2003-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  */
     3  */
     4 
     4 
     5 /* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
     5 /* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
     6  *
     6  *
     7  * Redistribution and use in  source and binary forms, with or without
     7  * Redistribution and use in  source and binary forms, with or without
    74  *           the corresponding Java functions
    74  *           the corresponding Java functions
    75  */
    75  */
    76 CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
    76 CK_C_INITIALIZE_ARGS_PTR makeCKInitArgsAdapter(JNIEnv *env, jobject jInitArgs)
    77 {
    77 {
    78     CK_C_INITIALIZE_ARGS_PTR ckpInitArgs;
    78     CK_C_INITIALIZE_ARGS_PTR ckpInitArgs;
    79     jclass jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
    79     jclass jInitArgsClass;
    80     jfieldID fieldID;
    80     jfieldID fieldID;
    81     jlong jFlags;
    81     jlong jFlags;
    82     jobject jReserved;
    82     jobject jReserved;
    83     CK_ULONG ckReservedLength;
    83     CK_ULONG ckReservedLength;
    84 #ifndef NO_CALLBACKS
    84 #ifndef NO_CALLBACKS
    89         return NULL_PTR;
    89         return NULL_PTR;
    90     }
    90     }
    91 
    91 
    92     /* convert the Java InitArgs object to a pointer to a CK_C_INITIALIZE_ARGS structure */
    92     /* convert the Java InitArgs object to a pointer to a CK_C_INITIALIZE_ARGS structure */
    93     ckpInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
    93     ckpInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
       
    94     if (ckpInitArgs == NULL) {
       
    95         JNU_ThrowOutOfMemoryError(env, 0);
       
    96         return NULL_PTR;
       
    97     }
    94 
    98 
    95     /* Set the mutex functions that will call the Java mutex functions, but
    99     /* Set the mutex functions that will call the Java mutex functions, but
    96      * only set it, if the field is not null.
   100      * only set it, if the field is not null.
    97      */
   101      */
       
   102     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
       
   103     if (jInitArgsClass == NULL) {
       
   104         free(ckpInitArgs);
       
   105         return NULL;
       
   106     }
       
   107 
    98 #ifdef NO_CALLBACKS
   108 #ifdef NO_CALLBACKS
    99     ckpInitArgs->CreateMutex = NULL_PTR;
   109     ckpInitArgs->CreateMutex = NULL_PTR;
   100     ckpInitArgs->DestroyMutex = NULL_PTR;
   110     ckpInitArgs->DestroyMutex = NULL_PTR;
   101     ckpInitArgs->LockMutex = NULL_PTR;
   111     ckpInitArgs->LockMutex = NULL_PTR;
   102     ckpInitArgs->UnlockMutex = NULL_PTR;
   112     ckpInitArgs->UnlockMutex = NULL_PTR;
   103 #else
   113 #else
   104     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
   114     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
   105     assert(fieldID != 0);
   115     if (fieldID == NULL) { return NULL; }
   106     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   116     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   107     ckpInitArgs->CreateMutex = (jMutexHandler != NULL) ? &callJCreateMutex : NULL_PTR;
   117     ckpInitArgs->CreateMutex = (jMutexHandler != NULL) ? &callJCreateMutex : NULL_PTR;
   108 
   118 
   109     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
   119     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
   110     assert(fieldID != 0);
   120     if (fieldID == NULL) { return NULL; }
   111     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   121     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   112     ckpInitArgs->DestroyMutex = (jMutexHandler != NULL) ? &callJDestroyMutex : NULL_PTR;
   122     ckpInitArgs->DestroyMutex = (jMutexHandler != NULL) ? &callJDestroyMutex : NULL_PTR;
   113 
   123 
   114     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
   124     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
   115     assert(fieldID != 0);
   125     if (fieldID == NULL) { return NULL; }
   116     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   126     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   117     ckpInitArgs->LockMutex = (jMutexHandler != NULL) ? &callJLockMutex : NULL_PTR;
   127     ckpInitArgs->LockMutex = (jMutexHandler != NULL) ? &callJLockMutex : NULL_PTR;
   118 
   128 
   119     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
   129     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
   120     assert(fieldID != 0);
   130     if (fieldID == NULL) { return NULL; }
   121     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   131     jMutexHandler = (*env)->GetObjectField(env, jInitArgs, fieldID);
   122     ckpInitArgs->UnlockMutex = (jMutexHandler != NULL) ? &callJUnlockMutex : NULL_PTR;
   132     ckpInitArgs->UnlockMutex = (jMutexHandler != NULL) ? &callJUnlockMutex : NULL_PTR;
   123 
   133 
   124     if ((ckpInitArgs->CreateMutex != NULL_PTR)
   134     if ((ckpInitArgs->CreateMutex != NULL_PTR)
   125             || (ckpInitArgs->DestroyMutex != NULL_PTR)
   135             || (ckpInitArgs->DestroyMutex != NULL_PTR)
   127             || (ckpInitArgs->UnlockMutex != NULL_PTR)) {
   137             || (ckpInitArgs->UnlockMutex != NULL_PTR)) {
   128         /* we only need to keep a global copy, if we need callbacks */
   138         /* we only need to keep a global copy, if we need callbacks */
   129         /* set the global object jInitArgs so that the right Java mutex functions will be called */
   139         /* set the global object jInitArgs so that the right Java mutex functions will be called */
   130         jInitArgsObject = (*env)->NewGlobalRef(env, jInitArgs);
   140         jInitArgsObject = (*env)->NewGlobalRef(env, jInitArgs);
   131         ckpGlobalInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
   141         ckpGlobalInitArgs = (CK_C_INITIALIZE_ARGS_PTR) malloc(sizeof(CK_C_INITIALIZE_ARGS));
       
   142         if (ckpGlobalInitArgs == NULL) {
       
   143             free(ckpInitArgs);
       
   144             JNU_ThrowOutOfMemoryError(env, 0);
       
   145             return NULL_PTR;
       
   146         }
       
   147 
   132         memcpy(ckpGlobalInitArgs, ckpInitArgs, sizeof(CK_C_INITIALIZE_ARGS));
   148         memcpy(ckpGlobalInitArgs, ckpInitArgs, sizeof(CK_C_INITIALIZE_ARGS));
   133     }
   149     }
   134 #endif /* NO_CALLBACKS */
   150 #endif /* NO_CALLBACKS */
   135 
   151 
   136     /* convert and set the flags field */
   152     /* convert and set the flags field */
   137     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "flags", "J");
   153     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "flags", "J");
   138     assert(fieldID != 0);
   154     if (fieldID == NULL) { return NULL; }
   139     jFlags = (*env)->GetLongField(env, jInitArgs, fieldID);
   155     jFlags = (*env)->GetLongField(env, jInitArgs, fieldID);
   140     ckpInitArgs->flags = jLongToCKULong(jFlags);
   156     ckpInitArgs->flags = jLongToCKULong(jFlags);
   141 
   157 
   142     /* pReserved should be NULL_PTR in this version */
   158     /* pReserved should be NULL_PTR in this version */
   143     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "pReserved", "Ljava/lang/Object;");
   159     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "pReserved", "Ljava/lang/Object;");
   144     assert(fieldID != 0);
   160     if (fieldID == NULL) { return NULL; }
   145     jReserved = (*env)->GetObjectField(env, jInitArgs, fieldID);
   161     jReserved = (*env)->GetObjectField(env, jInitArgs, fieldID);
   146 
   162 
   147     /* we try to convert the reserved parameter also */
   163     /* we try to convert the reserved parameter also */
   148     jObjectToPrimitiveCKObjectPtrPtr(env, jReserved, &(ckpInitArgs->pReserved), &ckReservedLength);
   164     jObjectToPrimitiveCKObjectPtrPtr(env, jReserved, &(ckpInitArgs->pReserved), &ckReservedLength);
   149 
   165 
   199     } else {
   215     } else {
   200         /* attached */
   216         /* attached */
   201         wasAttached = 1;
   217         wasAttached = 1;
   202     }
   218     }
   203 
   219 
   204 
       
   205     jCreateMutexClass = (*env)->FindClass(env, CLASS_CREATEMUTEX);
   220     jCreateMutexClass = (*env)->FindClass(env, CLASS_CREATEMUTEX);
       
   221     if (jCreateMutexClass == NULL) { return rv; }
   206     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
   222     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
       
   223     if (jInitArgsClass == NULL) { return rv; }
   207 
   224 
   208     /* get the CreateMutex object out of the jInitArgs object */
   225     /* get the CreateMutex object out of the jInitArgs object */
   209     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
   226     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "CreateMutex", "Lsun/security/pkcs11/wrapper/CK_CREATEMUTEX;");
   210     assert(fieldID != 0);
   227     if (fieldID == NULL) { return rv; }
   211     jCreateMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   228     jCreateMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   212     assert(jCreateMutex != 0);
   229     assert(jCreateMutex != 0);
   213 
   230 
   214     /* call the CK_CREATEMUTEX function of the CreateMutex object */
   231     /* call the CK_CREATEMUTEX function of the CreateMutex object */
   215     /* and get the new Java mutex object */
   232     /* and get the new Java mutex object */
   216     methodID = (*env)->GetMethodID(env, jCreateMutexClass, "CK_CREATEMUTEX", "()Ljava/lang/Object;");
   233     methodID = (*env)->GetMethodID(env, jCreateMutexClass, "CK_CREATEMUTEX", "()Ljava/lang/Object;");
   217     assert(methodID != 0);
   234     if (methodID == NULL) { return rv; }
   218     jMutex = (*env)->CallObjectMethod(env, jCreateMutex, methodID);
   235     jMutex = (*env)->CallObjectMethod(env, jCreateMutex, methodID);
   219 
   236 
   220     /* set a global reference on the Java mutex */
   237     /* set a global reference on the Java mutex */
   221     jMutex = (*env)->NewGlobalRef(env, jMutex);
   238     jMutex = (*env)->NewGlobalRef(env, jMutex);
   222     /* convert the Java mutex to a CK mutex */
   239     /* convert the Java mutex to a CK mutex */
   225 
   242 
   226     /* check, if callback threw an exception */
   243     /* check, if callback threw an exception */
   227     pkcs11Exception = (*env)->ExceptionOccurred(env);
   244     pkcs11Exception = (*env)->ExceptionOccurred(env);
   228 
   245 
   229     if (pkcs11Exception != NULL) {
   246     if (pkcs11Exception != NULL) {
       
   247         /* TBD: clear the pending exception with ExceptionClear? */
   230         /* The was an exception thrown, now we get the error-code from it */
   248         /* The was an exception thrown, now we get the error-code from it */
   231         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
   249         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
       
   250         if (pkcs11ExceptionClass == NULL) { return rv; }
   232         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   251         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   233         assert(methodID != 0);
   252         if (methodID == NULL) { return rv; }
       
   253 
   234         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   254         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   235         rv = jLongToCKULong(errorCode);
   255         rv = jLongToCKULong(errorCode);
   236     }
   256     }
   237 
   257 
   238     /* if we attached this thread to the VM just for callback, we detach it now */
   258     /* if we attached this thread to the VM just for callback, we detach it now */
   290     } else {
   310     } else {
   291         /* attached */
   311         /* attached */
   292         wasAttached = 1;
   312         wasAttached = 1;
   293     }
   313     }
   294 
   314 
   295 
       
   296     jDestroyMutexClass = (*env)->FindClass(env, CLASS_DESTROYMUTEX);
   315     jDestroyMutexClass = (*env)->FindClass(env, CLASS_DESTROYMUTEX);
       
   316     if (jDestroyMutexClass == NULL) { return rv; }
   297     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
   317     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
       
   318     if (jInitArgsClass == NULL) { return rv; }
   298 
   319 
   299     /* convert the CK mutex to a Java mutex */
   320     /* convert the CK mutex to a Java mutex */
   300     jMutex = ckVoidPtrToJObject(pMutex);
   321     jMutex = ckVoidPtrToJObject(pMutex);
   301 
   322 
   302     /* get the DestroyMutex object out of the jInitArgs object */
   323     /* get the DestroyMutex object out of the jInitArgs object */
   303     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
   324     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "DestroyMutex", "Lsun/security/pkcs11/wrapper/CK_DESTROYMUTEX;");
   304     assert(fieldID != 0);
   325     if (fieldID == NULL) { return rv; }
   305     jDestroyMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   326     jDestroyMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   306     assert(jDestroyMutex != 0);
   327     assert(jDestroyMutex != 0);
   307 
   328 
   308     /* call the CK_DESTROYMUTEX method of the DestroyMutex object */
   329     /* call the CK_DESTROYMUTEX method of the DestroyMutex object */
   309     methodID = (*env)->GetMethodID(env, jDestroyMutexClass, "CK_DESTROYMUTEX", "(Ljava/lang/Object;)V");
   330     methodID = (*env)->GetMethodID(env, jDestroyMutexClass, "CK_DESTROYMUTEX", "(Ljava/lang/Object;)V");
   310     assert(methodID != 0);
   331     if (methodID == NULL) { return rv; }
   311     (*env)->CallVoidMethod(env, jDestroyMutex, methodID, jMutex);
   332     (*env)->CallVoidMethod(env, jDestroyMutex, methodID, jMutex);
   312 
   333 
   313     /* delete the global reference on the Java mutex */
   334     /* delete the global reference on the Java mutex */
   314     (*env)->DeleteGlobalRef(env, jMutex);
   335     (*env)->DeleteGlobalRef(env, jMutex);
   315 
   336 
   316 
   337 
   317     /* check, if callback threw an exception */
   338     /* check, if callback threw an exception */
   318     pkcs11Exception = (*env)->ExceptionOccurred(env);
   339     pkcs11Exception = (*env)->ExceptionOccurred(env);
   319 
   340 
   320     if (pkcs11Exception != NULL) {
   341     if (pkcs11Exception != NULL) {
       
   342         /* TBD: clear the pending exception with ExceptionClear? */
   321         /* The was an exception thrown, now we get the error-code from it */
   343         /* The was an exception thrown, now we get the error-code from it */
   322         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
   344         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
       
   345         if (pkcs11ExceptionClass == NULL) { return rv; }
   323         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   346         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   324         assert(methodID != 0);
   347         if (methodID == NULL) { return rv; }
   325         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   348         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   326         rv = jLongToCKULong(errorCode);
   349         rv = jLongToCKULong(errorCode);
   327     }
   350     }
   328 
   351 
   329     /* if we attached this thread to the VM just for callback, we detach it now */
   352     /* if we attached this thread to the VM just for callback, we detach it now */
   381     } else {
   404     } else {
   382         /* attached */
   405         /* attached */
   383         wasAttached = 1;
   406         wasAttached = 1;
   384     }
   407     }
   385 
   408 
   386 
       
   387     jLockMutexClass = (*env)->FindClass(env, CLASS_LOCKMUTEX);
   409     jLockMutexClass = (*env)->FindClass(env, CLASS_LOCKMUTEX);
       
   410     if (jLockMutexClass == NULL) { return rv; }
   388     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
   411     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
       
   412     if (jInitArgsClass == NULL) { return rv; }
   389 
   413 
   390     /* convert the CK mutex to a Java mutex */
   414     /* convert the CK mutex to a Java mutex */
   391     jMutex = ckVoidPtrToJObject(pMutex);
   415     jMutex = ckVoidPtrToJObject(pMutex);
   392 
   416 
   393     /* get the LockMutex object out of the jInitArgs object */
   417     /* get the LockMutex object out of the jInitArgs object */
   394     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
   418     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "LockMutex", "Lsun/security/pkcs11/wrapper/CK_LOCKMUTEX;");
   395     assert(fieldID != 0);
   419     if (fieldID == NULL) { return rv; }
   396     jLockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   420     jLockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   397     assert(jLockMutex != 0);
   421     assert(jLockMutex != 0);
   398 
   422 
   399     /* call the CK_LOCKMUTEX method of the LockMutex object */
   423     /* call the CK_LOCKMUTEX method of the LockMutex object */
   400     methodID = (*env)->GetMethodID(env, jLockMutexClass, "CK_LOCKMUTEX", "(Ljava/lang/Object;)V");
   424     methodID = (*env)->GetMethodID(env, jLockMutexClass, "CK_LOCKMUTEX", "(Ljava/lang/Object;)V");
   401     assert(methodID != 0);
   425     if (methodID == NULL) { return rv; }
   402     (*env)->CallVoidMethod(env, jLockMutex, methodID, jMutex);
   426     (*env)->CallVoidMethod(env, jLockMutex, methodID, jMutex);
   403 
       
   404 
   427 
   405     /* check, if callback threw an exception */
   428     /* check, if callback threw an exception */
   406     pkcs11Exception = (*env)->ExceptionOccurred(env);
   429     pkcs11Exception = (*env)->ExceptionOccurred(env);
   407 
   430 
   408     if (pkcs11Exception != NULL) {
   431     if (pkcs11Exception != NULL) {
       
   432         /* TBD: clear the pending exception with ExceptionClear? */
   409         /* The was an exception thrown, now we get the error-code from it */
   433         /* The was an exception thrown, now we get the error-code from it */
   410         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
   434         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
       
   435         if (pkcs11ExceptionClass == NULL) { return rv; }
   411         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   436         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   412         assert(methodID != 0);
   437         if (methodID == NULL) { return rv; }
   413         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   438         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   414         rv = jLongToCKULong(errorCode);
   439         rv = jLongToCKULong(errorCode);
   415     }
   440     }
   416 
   441 
   417     /* if we attached this thread to the VM just for callback, we detach it now */
   442     /* if we attached this thread to the VM just for callback, we detach it now */
   469     } else {
   494     } else {
   470         /* attached */
   495         /* attached */
   471         wasAttached = 1;
   496         wasAttached = 1;
   472     }
   497     }
   473 
   498 
   474 
       
   475     jUnlockMutexClass = (*env)->FindClass(env, CLASS_UNLOCKMUTEX);
   499     jUnlockMutexClass = (*env)->FindClass(env, CLASS_UNLOCKMUTEX);
       
   500     if (jUnlockMutexClass == NULL) { return rv; }
   476     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
   501     jInitArgsClass = (*env)->FindClass(env, CLASS_C_INITIALIZE_ARGS);
       
   502     if (jInitArgsClass == NULL) { return rv; }
   477 
   503 
   478     /* convert the CK-type mutex to a Java mutex */
   504     /* convert the CK-type mutex to a Java mutex */
   479     jMutex = ckVoidPtrToJObject(pMutex);
   505     jMutex = ckVoidPtrToJObject(pMutex);
   480 
   506 
   481     /* get the UnlockMutex object out of the jInitArgs object */
   507     /* get the UnlockMutex object out of the jInitArgs object */
   482     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
   508     fieldID = (*env)->GetFieldID(env, jInitArgsClass, "UnlockMutex", "Lsun/security/pkcs11/wrapper/CK_UNLOCKMUTEX;");
   483     assert(fieldID != 0);
   509     if (fieldID == NULL) { return rv; }
   484     jUnlockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   510     jUnlockMutex = (*env)->GetObjectField(env, jInitArgsObject, fieldID);
   485     assert(jUnlockMutex != 0);
   511     assert(jUnlockMutex != 0);
   486 
   512 
   487     /* call the CK_UNLOCKMUTEX method of the UnLockMutex object */
   513     /* call the CK_UNLOCKMUTEX method of the UnLockMutex object */
   488     methodID = (*env)->GetMethodID(env, jUnlockMutexClass, "CK_UNLOCKMUTEX", "(Ljava/lang/Object;)V");
   514     methodID = (*env)->GetMethodID(env, jUnlockMutexClass, "CK_UNLOCKMUTEX", "(Ljava/lang/Object;)V");
   489     assert(methodID != 0);
   515     if (methodID == NULL) { return rv; }
   490     (*env)->CallVoidMethod(env, jUnlockMutex, methodID, jMutex);
   516     (*env)->CallVoidMethod(env, jUnlockMutex, methodID, jMutex);
   491 
       
   492 
   517 
   493     /* check, if callback threw an exception */
   518     /* check, if callback threw an exception */
   494     pkcs11Exception = (*env)->ExceptionOccurred(env);
   519     pkcs11Exception = (*env)->ExceptionOccurred(env);
   495 
   520 
   496     if (pkcs11Exception != NULL) {
   521     if (pkcs11Exception != NULL) {
       
   522         /* TBD: clear the pending exception with ExceptionClear? */
   497         /* The was an exception thrown, now we get the error-code from it */
   523         /* The was an exception thrown, now we get the error-code from it */
   498         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
   524         pkcs11ExceptionClass = (*env)->FindClass(env, CLASS_PKCS11EXCEPTION);
       
   525         if (pkcs11ExceptionClass == NULL) { return rv; }
   499         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   526         methodID = (*env)->GetMethodID(env, pkcs11ExceptionClass, "getErrorCode", "()J");
   500         assert(methodID != 0);
   527         if (methodID == NULL) { return rv; }
   501         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   528         errorCode = (*env)->CallLongMethod(env, pkcs11Exception, methodID);
   502         rv = jLongToCKULong(errorCode);
   529         rv = jLongToCKULong(errorCode);
   503     }
   530     }
   504 
   531 
   505     /* if we attached this thread to the VM just for callback, we detach it now */
   532     /* if we attached this thread to the VM just for callback, we detach it now */