jni.cc (_Jv_JNI_check_types): New.

2003-06-11  Andrew Haley  <aph@redhat.com>

        * jni.cc (_Jv_JNI_check_types): New.
        (_Jv_JNI_SetPrimgitiveArrayRegion): Check array type.
        (_Jv_JNI_GetPrimitiveArrayRegion): Ditto.
        (_Jv_JNI_GetPrimitiveArrayElements): Ditto.
        (_Jv_JNI_ReleasePrimitiveArrayElements): Ditto.

        * java/lang/natVMSecurityManager.cc (getClassContext): Fix
        infinite loop.

From-SVN: r67835
This commit is contained in:
Andrew Haley 2003-06-12 15:39:17 +00:00 committed by Andrew Haley
parent 14b96a951a
commit e976ed37ef
3 changed files with 109 additions and 42 deletions

View File

@ -1,3 +1,14 @@
2003-06-11 Andrew Haley <aph@redhat.com>
* jni.cc (_Jv_JNI_check_types): New.
(_Jv_JNI_SetPrimgitiveArrayRegion): Check array type.
(_Jv_JNI_GetPrimitiveArrayRegion): Ditto.
(_Jv_JNI_GetPrimitiveArrayElements): Ditto.
(_Jv_JNI_ReleasePrimitiveArrayElements): Ditto.
* java/lang/natVMSecurityManager.cc (getClassContext): Fix
infinite loop.
2003-06-11 Tom Tromey <tromey@redhat.com> 2003-06-11 Tom Tromey <tromey@redhat.com>
* java/lang/ClassLoader.java (loadClass): Not deprecated. * java/lang/ClassLoader.java (loadClass): Not deprecated.

View File

@ -28,9 +28,9 @@ java::lang::VMSecurityManager::getClassContext ()
int maxlen = t->length(); int maxlen = t->length();
int len = 0; int len = 0;
while (len < maxlen) for (int i=0; i<len; i++)
{ {
jclass klass = t->classAt(len); jclass klass = t->classAt(i);
if (klass != NULL && klass != &java::lang::VMSecurityManager::class$ if (klass != NULL && klass != &java::lang::VMSecurityManager::class$
&& klass != &java::lang::SecurityManager::class$) && klass != &java::lang::SecurityManager::class$)
++len; ++len;
@ -41,9 +41,9 @@ java::lang::VMSecurityManager::getClassContext ()
NULL); NULL);
len = 0; len = 0;
while (len < maxlen) for (int i=0; i<len; i++)
{ {
jclass klass = t->classAt(len); jclass klass = t->classAt(i);
if (klass != NULL && klass != &java::lang::VMSecurityManager::class$ if (klass != NULL && klass != &java::lang::VMSecurityManager::class$
&& klass != &java::lang::SecurityManager::class$) && klass != &java::lang::SecurityManager::class$)
elements(result)[len++] = klass; elements(result)[len++] = klass;

View File

@ -39,6 +39,7 @@ details. */
#include <java/lang/Integer.h> #include <java/lang/Integer.h>
#include <java/lang/ThreadGroup.h> #include <java/lang/ThreadGroup.h>
#include <java/lang/Thread.h> #include <java/lang/Thread.h>
#include <java/lang/IllegalAccessError.h>
#include <gcj/method.h> #include <gcj/method.h>
#include <gcj/field.h> #include <gcj/field.h>
@ -384,6 +385,22 @@ static jobject
return _Jv_JNI_PopLocalFrame (env, result, MARK_USER); return _Jv_JNI_PopLocalFrame (env, result, MARK_USER);
} }
// Make sure an array's type is compatible with the type of the
// destination.
template<typename T>
static bool
_Jv_JNI_check_types (JNIEnv *env, JArray<T> *array, jclass K)
{
jclass klass = array->getClass()->getComponentType();
if (__builtin_expect (klass != K, false))
{
env->ex = new java::lang::IllegalAccessError ();
return false;
}
else
return true;
}
// Pop a `system' frame from the stack. This is `extern "C"' as it is // Pop a `system' frame from the stack. This is `extern "C"' as it is
// used by the compiler. // used by the compiler.
extern "C" void extern "C" void
@ -1446,12 +1463,14 @@ static JArray<T> *
} }
} }
template<typename T> template<typename T, jclass K>
static T * static T *
(JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *, JArray<T> *array, (JNICALL _Jv_JNI_GetPrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
jboolean *isCopy) jboolean *isCopy)
{ {
array = unwrap (array); array = unwrap (array);
if (! _Jv_JNI_check_types (env, array, K))
return NULL;
T *elts = elements (array); T *elts = elements (array);
if (isCopy) if (isCopy)
{ {
@ -1462,25 +1481,28 @@ static T *
return elts; return elts;
} }
template<typename T> template<typename T, jclass K>
static void static void
(JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *, JArray<T> *array, (JNICALL _Jv_JNI_ReleasePrimitiveArrayElements) (JNIEnv *env, JArray<T> *array,
T *, jint /* mode */) T *, jint /* mode */)
{ {
array = unwrap (array); array = unwrap (array);
_Jv_JNI_check_types (env, array, K);
// Note that we ignore MODE. We can do this because we never copy // Note that we ignore MODE. We can do this because we never copy
// the array elements. My reading of the JNI documentation is that // the array elements. My reading of the JNI documentation is that
// this is an option for the implementor. // this is an option for the implementor.
unmark_for_gc (array, global_ref_table); unmark_for_gc (array, global_ref_table);
} }
template<typename T> template<typename T, jclass K>
static void static void
(JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array, (JNICALL _Jv_JNI_GetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
jsize start, jsize len, jsize start, jsize len,
T *buf) T *buf)
{ {
array = unwrap (array); array = unwrap (array);
if (! _Jv_JNI_check_types (env, array, K))
return;
// The cast to unsigned lets us save a comparison. // The cast to unsigned lets us save a comparison.
if (start < 0 || len < 0 if (start < 0 || len < 0
@ -1504,12 +1526,14 @@ static void
} }
} }
template<typename T> template<typename T, jclass K>
static void static void
(JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array, (JNICALL _Jv_JNI_SetPrimitiveArrayRegion) (JNIEnv *env, JArray<T> *array,
jsize start, jsize len, T *buf) jsize start, jsize len, T *buf)
{ {
array = unwrap (array); array = unwrap (array);
if (! _Jv_JNI_check_types (env, array, K))
return;
// The cast to unsigned lets us save a comparison. // The cast to unsigned lets us save a comparison.
if (start < 0 || len < 0 if (start < 0 || len < 0
@ -2688,38 +2712,70 @@ struct JNINativeInterface _Jv_JNIFunctions =
_Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray _Jv_JNI_NewPrimitiveArray<jlong, JvPrimClass (long)>, // NewLongArray
_Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray _Jv_JNI_NewPrimitiveArray<jfloat, JvPrimClass (float)>, // NewFloatArray
_Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray _Jv_JNI_NewPrimitiveArray<jdouble, JvPrimClass (double)>, // NewDoubleArray
_Jv_JNI_GetPrimitiveArrayElements, // GetBooleanArrayElements _Jv_JNI_GetPrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
_Jv_JNI_GetPrimitiveArrayElements, // GetByteArrayElements // GetBooleanArrayElements
_Jv_JNI_GetPrimitiveArrayElements, // GetCharArrayElements _Jv_JNI_GetPrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
_Jv_JNI_GetPrimitiveArrayElements, // GetShortArrayElements // GetByteArrayElements
_Jv_JNI_GetPrimitiveArrayElements, // GetIntArrayElements _Jv_JNI_GetPrimitiveArrayElements<jchar, JvPrimClass (char)>,
_Jv_JNI_GetPrimitiveArrayElements, // GetLongArrayElements // GetCharArrayElements
_Jv_JNI_GetPrimitiveArrayElements, // GetFloatArrayElements _Jv_JNI_GetPrimitiveArrayElements<jshort, JvPrimClass (short)>,
_Jv_JNI_GetPrimitiveArrayElements, // GetDoubleArrayElements // GetShortArrayElements
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseBooleanArrayElements _Jv_JNI_GetPrimitiveArrayElements<jint, JvPrimClass (int)>,
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseByteArrayElements // GetIntArrayElements
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseCharArrayElements _Jv_JNI_GetPrimitiveArrayElements<jlong, JvPrimClass (long)>,
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseShortArrayElements // GetLongArrayElements
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseIntArrayElements _Jv_JNI_GetPrimitiveArrayElements<jfloat, JvPrimClass (float)>,
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseLongArrayElements // GetFloatArrayElements
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseFloatArrayElements _Jv_JNI_GetPrimitiveArrayElements<jdouble, JvPrimClass (double)>,
_Jv_JNI_ReleasePrimitiveArrayElements, // ReleaseDoubleArrayElements // GetDoubleArrayElements
_Jv_JNI_GetPrimitiveArrayRegion, // GetBooleanArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jboolean, JvPrimClass (boolean)>,
_Jv_JNI_GetPrimitiveArrayRegion, // GetByteArrayRegion // ReleaseBooleanArrayElements
_Jv_JNI_GetPrimitiveArrayRegion, // GetCharArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jbyte, JvPrimClass (byte)>,
_Jv_JNI_GetPrimitiveArrayRegion, // GetShortArrayRegion // ReleaseByteArrayElements
_Jv_JNI_GetPrimitiveArrayRegion, // GetIntArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jchar, JvPrimClass (char)>,
_Jv_JNI_GetPrimitiveArrayRegion, // GetLongArrayRegion // ReleaseCharArrayElements
_Jv_JNI_GetPrimitiveArrayRegion, // GetFloatArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jshort, JvPrimClass (short)>,
_Jv_JNI_GetPrimitiveArrayRegion, // GetDoubleArrayRegion // ReleaseShortArrayElements
_Jv_JNI_SetPrimitiveArrayRegion, // SetBooleanArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jint, JvPrimClass (int)>,
_Jv_JNI_SetPrimitiveArrayRegion, // SetByteArrayRegion // ReleaseIntArrayElements
_Jv_JNI_SetPrimitiveArrayRegion, // SetCharArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jlong, JvPrimClass (long)>,
_Jv_JNI_SetPrimitiveArrayRegion, // SetShortArrayRegion // ReleaseLongArrayElements
_Jv_JNI_SetPrimitiveArrayRegion, // SetIntArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jfloat, JvPrimClass (float)>,
_Jv_JNI_SetPrimitiveArrayRegion, // SetLongArrayRegion // ReleaseFloatArrayElements
_Jv_JNI_SetPrimitiveArrayRegion, // SetFloatArrayRegion _Jv_JNI_ReleasePrimitiveArrayElements<jdouble, JvPrimClass (double)>,
_Jv_JNI_SetPrimitiveArrayRegion, // SetDoubleArrayRegion // ReleaseDoubleArrayElements
_Jv_JNI_GetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
// GetBooleanArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
// GetByteArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
// GetCharArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
// GetShortArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
// GetIntArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
// GetLongArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
// GetFloatArrayRegion
_Jv_JNI_GetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
// GetDoubleArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jboolean, JvPrimClass (boolean)>,
// SetBooleanArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jbyte, JvPrimClass (byte)>,
// SetByteArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jchar, JvPrimClass (char)>,
// SetCharArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jshort, JvPrimClass (short)>,
// SetShortArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jint, JvPrimClass (int)>,
// SetIntArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jlong, JvPrimClass (long)>,
// SetLongArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jfloat, JvPrimClass (float)>,
// SetFloatArrayRegion
_Jv_JNI_SetPrimitiveArrayRegion<jdouble, JvPrimClass (double)>,
// SetDoubleArrayRegion
_Jv_JNI_RegisterNatives, // RegisterNatives _Jv_JNI_RegisterNatives, // RegisterNatives
_Jv_JNI_UnregisterNatives, // UnregisterNatives _Jv_JNI_UnregisterNatives, // UnregisterNatives
_Jv_JNI_MonitorEnter, // MonitorEnter _Jv_JNI_MonitorEnter, // MonitorEnter