Author: mturk Date: Mon Sep 4 00:15:58 2006 New Revision: 439960 URL: http://svn.apache.org/viewvc?view=rev&rev=439960 Log: The JNIEnv used in function call could not be used in callback, because a callback might be called by the different thread, thus making core dump. Until we can use apr_threadkey_private make a less optimized call to get JNIEnv before doing JNI callback.
Modified: tomcat/connectors/trunk/jni/native/include/tcn.h tomcat/connectors/trunk/jni/native/include/tcn_api.h tomcat/connectors/trunk/jni/native/src/jnilib.c tomcat/connectors/trunk/jni/native/src/pool.c tomcat/connectors/trunk/jni/native/src/proc.c tomcat/connectors/trunk/jni/native/src/ssl.c tomcat/connectors/trunk/jni/native/src/sslutils.c Modified: tomcat/connectors/trunk/jni/native/include/tcn.h URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/include/tcn.h?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/include/tcn.h (original) +++ tomcat/connectors/trunk/jni/native/include/tcn.h Mon Sep 4 00:15:58 2006 @@ -231,7 +231,6 @@ #define TCN_MAX_METHODS 8 typedef struct { - JNIEnv *env; jobject obj; jmethodID mid[TCN_MAX_METHODS]; void *opaque; Modified: tomcat/connectors/trunk/jni/native/include/tcn_api.h URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/include/tcn_api.h?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/include/tcn_api.h (original) +++ tomcat/connectors/trunk/jni/native/include/tcn_api.h Mon Sep 4 00:15:58 2006 @@ -155,6 +155,10 @@ */ TCN_DECLARE(JavaVM *) tcn_get_java_vm(void); +/* Get current thread JNIEnv + */ +TCN_DECLARE(jint) tcn_get_java_env(JNIEnv **); + #ifdef __cplusplus } #endif Modified: tomcat/connectors/trunk/jni/native/src/jnilib.c URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/jnilib.c?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/src/jnilib.c (original) +++ tomcat/connectors/trunk/jni/native/src/jnilib.c Mon Sep 4 00:15:58 2006 @@ -458,3 +458,12 @@ { return tcn_global_vm; } + +TCN_DECLARE(jint) tcn_get_java_env(JNIEnv **env) +{ + if ((*tcn_global_vm)->GetEnv(tcn_global_vm, (void **)env, + JNI_VERSION_1_4)) { + return JNI_ERR; + } + return JNI_OK; +} Modified: tomcat/connectors/trunk/jni/native/src/pool.c URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/pool.c?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/src/pool.c (original) +++ tomcat/connectors/trunk/jni/native/src/pool.c Mon Sep 4 00:15:58 2006 @@ -29,10 +29,12 @@ tcn_callback_t *cb = (tcn_callback_t *)data; if (data) { - if (!TCN_IS_NULL(cb->env, cb->obj)) { - rv = (*(cb->env))->CallIntMethod(cb->env, cb->obj, cb->mid[0], - NULL); - TCN_UNLOAD_CLASS(cb->env, cb->obj); + JNIEnv *env; + tcn_get_java_env(&env); + if (!TCN_IS_NULL(env, cb->obj)) { + rv = (*(env))->CallIntMethod(env, cb->obj, cb->mid[0], + NULL); + TCN_UNLOAD_CLASS(env, cb->obj); } free(cb); } @@ -113,7 +115,6 @@ return 0; } cls = (*e)->GetObjectClass(e, obj); - cb->env = e; cb->obj = (*e)->NewGlobalRef(e, obj); cb->mid[0] = (*e)->GetMethodID(e, cls, "callback", "()I"); @@ -175,8 +176,11 @@ tcn_callback_t *cb = (tcn_callback_t *)data; if (data) { - if (!TCN_IS_NULL(cb->env, cb->obj)) { - TCN_UNLOAD_CLASS(cb->env, cb->obj); + JNIEnv *env; + tcn_get_java_env(&env); + + if (!TCN_IS_NULL(env, cb->obj)) { + TCN_UNLOAD_CLASS(env, cb->obj); } free(cb); } @@ -199,8 +203,9 @@ apr_pool_cleanup_run(p, old, generic_pool_data_cleanup); } if (data) { + JNIEnv *e; tcn_callback_t *cb = (tcn_callback_t *)malloc(sizeof(tcn_callback_t)); - cb->env = e; + tcn_get_java_env(&e); cb->obj = (*e)->NewGlobalRef(e, data); if ((rv = apr_pool_userdata_set(cb, J2S(key), generic_pool_data_cleanup, p)) != APR_SUCCESS) { Modified: tomcat/connectors/trunk/jni/native/src/proc.c URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/proc.c?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/src/proc.c (original) +++ tomcat/connectors/trunk/jni/native/src/proc.c Mon Sep 4 00:15:58 2006 @@ -34,10 +34,12 @@ apr_pool_userdata_get(&data, ERRFN_USERDATA_KEY, pool); cb = (tcn_callback_t *)data; if (cb) { - if (!TCN_IS_NULL(cb->env, cb->obj)) { - (*(cb->env))->CallVoidMethod(cb->env, cb->obj, cb->mid[0], + JNIEnv *env; + tcn_get_java_env(&env); + if (!TCN_IS_NULL(env, cb->obj)) { + (*(env))->CallVoidMethod(env, cb->obj, cb->mid[0], P2J(pool), (jint)err, - (*(cb->env))->NewStringUTF(cb->env, description), + (*(env))->NewStringUTF(env, description), NULL); } } @@ -48,8 +50,9 @@ tcn_callback_t *cb = (tcn_callback_t *)data; if (data) { - if (!TCN_IS_NULL(cb->env, cb->obj)) { - TCN_UNLOAD_CLASS(cb->env, cb->obj); + JNIEnv *env; + if (!TCN_IS_NULL(env, cb->obj)) { + TCN_UNLOAD_CLASS(env, cb->obj); } free(cb); } @@ -384,7 +387,6 @@ return; } cls = (*e)->GetObjectClass(e, obj); - cb->env = e; cb->obj = (*e)->NewGlobalRef(e, obj); cb->mid[0] = (*e)->GetMethodID(e, cls, "callback", "(JILjava/lang/String;)V"); Modified: tomcat/connectors/trunk/jni/native/src/ssl.c URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/ssl.c?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/src/ssl.c (original) +++ tomcat/connectors/trunk/jni/native/src/ssl.c Mon Sep 4 00:15:58 2006 @@ -111,7 +111,9 @@ ssl_initialized = 0; if (tcn_password_callback.cb.obj) { - TCN_UNLOAD_CLASS(tcn_password_callback.cb.env, + JNIEnv *env; + tcn_get_java_env(&env); + TCN_UNLOAD_CLASS(env, tcn_password_callback.cb.obj); } @@ -488,7 +490,6 @@ } TCN_FREE_CSTRING(file); } - /* OpenSSL Java Stream BIO */ typedef struct { @@ -560,8 +561,10 @@ if (bi->ptr != NULL) { BIO_JAVA *j = (BIO_JAVA *)bi->ptr; if (bi->init) { + JNIEnv *e = NULL; bi->init = 0; - TCN_UNLOAD_CLASS(j->cb.env, j->cb.obj); + tcn_get_java_env(&e); + TCN_UNLOAD_CLASS(e, j->cb.obj); } OPENSSL_free(bi->ptr); } @@ -574,8 +577,9 @@ jint ret = 0; if (b->init && in != NULL) { BIO_JAVA *j = (BIO_JAVA *)b->ptr; - JNIEnv *e = j->cb.env; + JNIEnv *e = NULL; jbyteArray jb = (*e)->NewByteArray(e, inl); + tcn_get_java_env(&e); if (!(*e)->ExceptionOccurred(e)) { (*e)->SetByteArrayRegion(e, jb, 0, inl, (jbyte *)in); ret = (*e)->CallIntMethod(e, j->cb.obj, @@ -592,8 +596,9 @@ jint ret = 0; if (b->init && out != NULL) { BIO_JAVA *j = (BIO_JAVA *)b->ptr; - JNIEnv *e = j->cb.env; + JNIEnv *e = NULL; jbyteArray jb = (*e)->NewByteArray(e, outl); + tcn_get_java_env(&e); if (!(*e)->ExceptionOccurred(e)) { ret = (*e)->CallIntMethod(e, j->cb.obj, j->cb.mid[1], jb); @@ -613,7 +618,8 @@ int ret = 0; if (b->init && in != NULL) { BIO_JAVA *j = (BIO_JAVA *)b->ptr; - JNIEnv *e = j->cb.env; + JNIEnv *e = NULL; + tcn_get_java_env(&e); ret = (*e)->CallIntMethod(e, j->cb.obj, j->cb.mid[2], tcn_new_string(e, in)); @@ -626,8 +632,9 @@ int ret = 0; if (b->init && out != NULL) { BIO_JAVA *j = (BIO_JAVA *)b->ptr; - JNIEnv *e = j->cb.env; + JNIEnv *e = NULL; jobject o; + tcn_get_java_env(&e); if ((o = (*e)->CallObjectMethod(e, j->cb.obj, j->cb.mid[3], (jint)(outl - 1)))) { TCN_ALLOC_CSTRING(o); @@ -693,7 +700,6 @@ } cls = (*e)->GetObjectClass(e, callback); - j->cb.env = e; j->cb.mid[0] = (*e)->GetMethodID(e, cls, "write", "([B)I"); j->cb.mid[1] = (*e)->GetMethodID(e, cls, "read", "([B)I"); j->cb.mid[2] = (*e)->GetMethodID(e, cls, "puts", "(Ljava/lang/String;)I"); @@ -723,11 +729,10 @@ UNREFERENCED(o); if (tcn_password_callback.cb.obj) { - TCN_UNLOAD_CLASS(tcn_password_callback.cb.env, + TCN_UNLOAD_CLASS(e, tcn_password_callback.cb.obj); } cls = (*e)->GetObjectClass(e, callback); - tcn_password_callback.cb.env = e; tcn_password_callback.cb.mid[0] = (*e)->GetMethodID(e, cls, "callback", "(Ljava/lang/String;)Ljava/lang/String;"); /* TODO: Check if method id is valid */ Modified: tomcat/connectors/trunk/jni/native/src/sslutils.c URL: http://svn.apache.org/viewvc/tomcat/connectors/trunk/jni/native/src/sslutils.c?view=diff&rev=439960&r1=439959&r2=439960 ============================================================================== --- tomcat/connectors/trunk/jni/native/src/sslutils.c (original) +++ tomcat/connectors/trunk/jni/native/src/sslutils.c Mon Sep 4 00:15:58 2006 @@ -77,10 +77,11 @@ { int rv = 0; data->password[0] = '\0'; - if (data->cb.env && data->cb.obj) { - JNIEnv *e = data->cb.env; + if (data->cb.obj) { + JNIEnv *e; jobject o; jstring prompt = AJP_TO_JSTRING(data->prompt); + tcn_get_java_env(&e); if ((o = (*e)->CallObjectMethod(e, data->cb.obj, data->cb.mid[0], prompt))) { TCN_ALLOC_CSTRING(o); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]