Runtime.java: Comment fix.
* java/lang/Runtime.java: Comment fix. * java/lang/ClassLoader.java (isAncestorOf): New method. (getParent): Uncommented security check. Use isAncestorOf. * include/jvm.h (_Jv_CheckAccess): Declare. * java/lang/reflect/natConstructor.cc (newInstance): Perform access check. Include IllegalAccessException.h, ArrayIndexOutOfBoundsException.h. * java/lang/reflect/natArray.cc (newInstance): Pass caller's class loader to _Jv_GetArrayClass. Include ArrayIndexOutOfBoundsException.h. * java/lang/reflect/Field.java: Update comment to reflect status. (equals): Fixed indentation. * java/lang/Class.h (Class): Declare memberAccessCheck, not checkMemberAccess. Make _Jv_CheckAccess a friend. * java/lang/Class.java (memberAccessCheck): New method from Classpath. (checkMemberAccess): Removed. (getDeclaredMethod): Use memberAccessCheck. (getField): Likewise. (getMethod): Likewise. * resolve.cc (_Jv_ResolvePoolEntry): Use _Jv_CheckAccess. (_Jv_SearchMethodInClass): Likewise. * prims.cc (_Jv_CheckAccess): New function. * jni.cc (_Jv_JNI_FindClass): Use getClassLoaderInternal. (_Jv_JNI_GetAnyFieldID): Likewise. * java/lang/natClass.cc (forName): Use getClassLoaderInternal. (getClassLoader): Added security check. (getConstructor): Call memberAccessCheck. (getDeclaredClasses): Likewise. (getDeclaredField): Likewise. (getDeclaredFields): Likewise. (_getConstructors): Likewise. (getDeclaredConstructor): Likewise. (getDeclaredMethods): Likewise. (getFields): Likewise. (getMethods): Likewise. (newInstance): Likewise. (_Jv_MakeVTable): Put method name in exception. * java/lang/reflect/natMethod.cc (getType): Use getClassLoaderInternal. (_Jv_GetTypesFromSignature): Likewise. (invoke): Perform access check. (_Jv_CallAnyMethodA): Removed old FIXME comments. Include ArrayIndexOutOfBoundsException.h. * java/lang/reflect/natField.cc (getType): Use getClassLoaderInternal. (_Jv_CheckFieldAccessibility): Removed. (getAddr): Use _Jv_CheckAccess; find caller. Include ArrayIndexOutOfBoundsException.h. From-SVN: r69621
This commit is contained in:
parent
3c87bc22a9
commit
ffd94572f4
15 changed files with 286 additions and 152 deletions
|
@ -1,4 +1,4 @@
|
|||
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
||||
/* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation
|
||||
|
||||
This file is part of libgcj.
|
||||
|
||||
|
@ -12,15 +12,6 @@ package java.lang.reflect;
|
|||
* @author Per Bothner <bothner@cygnus.com>
|
||||
* @date September 1998; February 1999.
|
||||
*/
|
||||
/* Status: Mostly implemented.
|
||||
* However, access checks are not implemented. See natField.cc for
|
||||
* _Jv_CheckFieldAccessibility as well as the missing getCaller.
|
||||
* Note that the idea is to have to compiler convert calls to
|
||||
* setXXX(...) and getXXX(...) to setXXX(CALLER, ...) and getXXX(CALLER, ...),
|
||||
* where CALLER is reference to the class that contains the calls to
|
||||
* setXXX or getXXX. This is easy for the compiler, and replaces
|
||||
* expensive stack and table searching with a constant.
|
||||
*/
|
||||
|
||||
public final class Field extends AccessibleObject implements Member
|
||||
{
|
||||
|
@ -39,12 +30,12 @@ public final class Field extends AccessibleObject implements Member
|
|||
}
|
||||
|
||||
public boolean equals (Object fld)
|
||||
{
|
||||
if (! (fld instanceof Field))
|
||||
return false;
|
||||
Field f = (Field) fld;
|
||||
return declaringClass == f.declaringClass && offset == f.offset;
|
||||
}
|
||||
{
|
||||
if (! (fld instanceof Field))
|
||||
return false;
|
||||
Field f = (Field) fld;
|
||||
return declaringClass == f.declaringClass && offset == f.offset;
|
||||
}
|
||||
|
||||
public Class getDeclaringClass ()
|
||||
{
|
||||
|
@ -62,11 +53,6 @@ public final class Field extends AccessibleObject implements Member
|
|||
return (declaringClass.hashCode() ^ offset);
|
||||
}
|
||||
|
||||
// The idea is that the compiler will magically translate
|
||||
// fld.getShort(obj) to fld.getShort(THISCLASS, obj).
|
||||
// This makes checking assessiblity more efficient,
|
||||
// since we don't have to do any stack-walking.
|
||||
|
||||
public boolean getBoolean (Object obj)
|
||||
throws IllegalArgumentException, IllegalAccessException
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// natField.cc - Implementation of java.lang.reflect.Field native methods.
|
||||
|
||||
/* Copyright (C) 1999, 2000, 2001 Free Software Foundation
|
||||
/* Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation
|
||||
|
||||
This file is part of libgcj.
|
||||
|
||||
|
@ -15,6 +15,7 @@ details. */
|
|||
#include <jvm.h>
|
||||
#include <gcj/cni.h>
|
||||
#include <java/lang/reflect/Array.h>
|
||||
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
||||
#include <java/lang/IllegalArgumentException.h>
|
||||
#include <java/lang/Byte.h>
|
||||
#include <java/lang/Short.h>
|
||||
|
@ -38,8 +39,8 @@ java::lang::reflect::Array::newInstance (jclass componentType, jint length)
|
|||
return _Jv_NewPrimArray (componentType, length);
|
||||
}
|
||||
else
|
||||
// FIXME: class loader?
|
||||
return JvNewObjectArray (length, componentType, NULL);
|
||||
|
||||
}
|
||||
|
||||
jobject
|
||||
|
@ -52,10 +53,26 @@ java::lang::reflect::Array::newInstance (jclass componentType,
|
|||
jint* dims = elements (dimensions);
|
||||
if (ndims == 1)
|
||||
return newInstance (componentType, dims[0]);
|
||||
|
||||
gnu::gcj::runtime::StackTrace *t
|
||||
= new gnu::gcj::runtime::StackTrace(4);
|
||||
Class *caller = NULL;
|
||||
ClassLoader *caller_loader = NULL;
|
||||
try
|
||||
{
|
||||
for (int i = 1; !caller; i++)
|
||||
{
|
||||
caller = t->classAt (i);
|
||||
}
|
||||
caller_loader = caller->getClassLoaderInternal();
|
||||
}
|
||||
catch (::java::lang::ArrayIndexOutOfBoundsException *e)
|
||||
{
|
||||
}
|
||||
|
||||
jclass arrayType = componentType;
|
||||
for (int i = 0; i < ndims; i++) // FIXME 2nd arg should
|
||||
// be "current" loader
|
||||
arrayType = _Jv_GetArrayClass (arrayType, 0);
|
||||
for (int i = 0; i < ndims; i++)
|
||||
arrayType = _Jv_GetArrayClass (arrayType, caller_loader);
|
||||
|
||||
return _Jv_NewMultiArray (arrayType, ndims, dims);
|
||||
}
|
||||
|
@ -343,8 +360,10 @@ java::lang::reflect::Array::setBoolean (jobject array,
|
|||
|
||||
void
|
||||
java::lang::reflect::Array::set (jobject array, jint index,
|
||||
jobject value, jclass elType)
|
||||
jobject value, jclass elType)
|
||||
{
|
||||
// We don't have to call getElementType here, or check INDEX,
|
||||
// because it was already done in the Java wrapper.
|
||||
if (! _Jv_IsInstanceOf (value, elType))
|
||||
throw new java::lang::IllegalArgumentException;
|
||||
elements ((jobjectArray) array) [index] = value;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// natConstructor.cc - Native code for Constructor class.
|
||||
|
||||
/* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
|
||||
/* Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation
|
||||
|
||||
This file is part of libgcj.
|
||||
|
||||
|
@ -13,6 +13,8 @@ details. */
|
|||
#include <gcj/cni.h>
|
||||
#include <jvm.h>
|
||||
|
||||
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
||||
#include <java/lang/IllegalAccessException.h>
|
||||
#include <java/lang/reflect/Constructor.h>
|
||||
#include <java/lang/reflect/Method.h>
|
||||
#include <java/lang/reflect/InvocationTargetException.h>
|
||||
|
@ -46,6 +48,24 @@ java::lang::reflect::Constructor::newInstance (jobjectArray args)
|
|||
if (parameter_types == NULL)
|
||||
getType ();
|
||||
|
||||
gnu::gcj::runtime::StackTrace *t
|
||||
= new gnu::gcj::runtime::StackTrace(4);
|
||||
Class *caller = NULL;
|
||||
try
|
||||
{
|
||||
for (int i = 1; !caller; i++)
|
||||
{
|
||||
caller = t->classAt (i);
|
||||
}
|
||||
}
|
||||
catch (::java::lang::ArrayIndexOutOfBoundsException *e)
|
||||
{
|
||||
}
|
||||
|
||||
if (! isAccessible() && ! _Jv_CheckAccess(caller, declaringClass,
|
||||
declaringClass->getModifiers()))
|
||||
throw new java::lang::IllegalAccessException;
|
||||
|
||||
using namespace java::lang::reflect;
|
||||
if (Modifier::isAbstract (declaringClass->getModifiers()))
|
||||
throw new InstantiationException;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// natField.cc - Implementation of java.lang.reflect.Field native methods.
|
||||
|
||||
/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
|
||||
/* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation
|
||||
|
||||
This file is part of libgcj.
|
||||
|
||||
|
@ -15,6 +15,7 @@ details. */
|
|||
#include <jvm.h>
|
||||
#include <java/lang/reflect/Field.h>
|
||||
#include <java/lang/reflect/Modifier.h>
|
||||
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
||||
#include <java/lang/IllegalArgumentException.h>
|
||||
#include <java/lang/IllegalAccessException.h>
|
||||
#include <java/lang/NullPointerException.h>
|
||||
|
@ -46,31 +47,36 @@ java::lang::reflect::Field::getType ()
|
|||
{
|
||||
jfieldID fld = _Jv_FromReflectedField (this);
|
||||
JvSynchronize sync (declaringClass);
|
||||
_Jv_ResolveField (fld, declaringClass->getClassLoader ());
|
||||
_Jv_ResolveField (fld, declaringClass->getClassLoaderInternal ());
|
||||
return fld->type;
|
||||
}
|
||||
|
||||
static void
|
||||
_Jv_CheckFieldAccessibility (jfieldID /*fld*/, jclass /*caller*/)
|
||||
{
|
||||
#if 0
|
||||
if (caller == NULL)
|
||||
caller = getCaller();
|
||||
#endif
|
||||
#if 0
|
||||
_Jv_ushort flags = fld->getModifiers();
|
||||
check accesss;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void*
|
||||
getAddr (java::lang::reflect::Field* field, jclass caller, jobject obj)
|
||||
{
|
||||
// FIXME: we know CALLER is NULL here. At one point we planned to
|
||||
// have the compiler insert the caller as a hidden argument in some
|
||||
// calls. However, we never implemented that, so we have to find
|
||||
// the caller by hand instead.
|
||||
gnu::gcj::runtime::StackTrace *t
|
||||
= new gnu::gcj::runtime::StackTrace(4);
|
||||
try
|
||||
{
|
||||
for (int i = 1; !caller; i++)
|
||||
{
|
||||
caller = t->classAt (i);
|
||||
}
|
||||
}
|
||||
catch (::java::lang::ArrayIndexOutOfBoundsException *e)
|
||||
{
|
||||
}
|
||||
|
||||
jfieldID fld = _Jv_FromReflectedField (field);
|
||||
_Jv_ushort flags = fld->getModifiers();
|
||||
if (! (flags & java::lang::reflect::Modifier::PUBLIC)
|
||||
&& ! field->isAccessible ())
|
||||
_Jv_CheckFieldAccessibility (fld, caller);
|
||||
if (! field->isAccessible ()
|
||||
&& ! _Jv_CheckAccess (caller, field->getDeclaringClass(), flags))
|
||||
throw new java::lang::IllegalAccessException;
|
||||
|
||||
if (flags & java::lang::reflect::Modifier::STATIC)
|
||||
{
|
||||
jclass fldClass = field->getDeclaringClass ();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// natMethod.cc - Native code for Method class.
|
||||
|
||||
/* Copyright (C) 1998, 1999, 2000, 2001 , 2002 Free Software Foundation
|
||||
/* Copyright (C) 1998, 1999, 2000, 2001 , 2002, 2003 Free Software Foundation
|
||||
|
||||
This file is part of libgcj.
|
||||
|
||||
|
@ -30,6 +30,7 @@ details. */
|
|||
#include <java/lang/Double.h>
|
||||
#include <java/lang/IllegalArgumentException.h>
|
||||
#include <java/lang/NullPointerException.h>
|
||||
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
||||
#include <java/lang/Class.h>
|
||||
#include <gcj/method.h>
|
||||
#include <gnu/gcj/RawData.h>
|
||||
|
@ -142,19 +143,33 @@ java::lang::reflect::Method::invoke (jobject obj, jobjectArray args)
|
|||
if (parameter_types == NULL)
|
||||
getType ();
|
||||
|
||||
gnu::gcj::runtime::StackTrace *t
|
||||
= new gnu::gcj::runtime::StackTrace(4);
|
||||
Class *caller = NULL;
|
||||
try
|
||||
{
|
||||
for (int i = 1; !caller; i++)
|
||||
{
|
||||
caller = t->classAt (i);
|
||||
}
|
||||
}
|
||||
catch (::java::lang::ArrayIndexOutOfBoundsException *e)
|
||||
{
|
||||
}
|
||||
|
||||
jmethodID meth = _Jv_FromReflectedMethod (this);
|
||||
jclass klass;
|
||||
if (! java::lang::reflect::Modifier::isStatic(meth->accflags))
|
||||
{
|
||||
jclass k = obj ? obj->getClass() : NULL;
|
||||
if (! obj)
|
||||
throw new java::lang::NullPointerException;
|
||||
if (! declaringClass->isAssignableFrom(k))
|
||||
klass = obj->getClass();
|
||||
if (! declaringClass->isAssignableFrom(klass))
|
||||
throw new java::lang::IllegalArgumentException;
|
||||
// FIXME: access checks.
|
||||
|
||||
// Find the possibly overloaded method based on the runtime type
|
||||
// of the object.
|
||||
meth = _Jv_LookupDeclaredMethod (k, meth->name, meth->signature);
|
||||
meth = _Jv_LookupDeclaredMethod (klass, meth->name, meth->signature);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -162,8 +177,12 @@ java::lang::reflect::Method::invoke (jobject obj, jobjectArray args)
|
|||
// here and not in _Jv_CallAnyMethodA because JNI initializes a
|
||||
// class whenever a method lookup is done.
|
||||
_Jv_InitClass (declaringClass);
|
||||
klass = declaringClass;
|
||||
}
|
||||
|
||||
if (! isAccessible() && ! _Jv_CheckAccess(caller, klass, meth->accflags))
|
||||
throw new IllegalArgumentException;
|
||||
|
||||
return _Jv_CallAnyMethodA (obj, return_type, meth, false,
|
||||
parameter_types, args);
|
||||
}
|
||||
|
@ -207,7 +226,7 @@ java::lang::reflect::Method::getType ()
|
|||
jclass *elts = elements (exception_types);
|
||||
for (int i = 0; i < count; ++i)
|
||||
elts[i] = _Jv_FindClass (method->throws[i],
|
||||
declaringClass->getClassLoader ());
|
||||
declaringClass->getClassLoaderInternal ());
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -218,7 +237,7 @@ _Jv_GetTypesFromSignature (jmethodID method,
|
|||
{
|
||||
|
||||
_Jv_Utf8Const* sig = method->signature;
|
||||
java::lang::ClassLoader *loader = declaringClass->getClassLoader();
|
||||
java::lang::ClassLoader *loader = declaringClass->getClassLoaderInternal();
|
||||
char *ptr = sig->data;
|
||||
int numArgs = 0;
|
||||
/* First just count the number of parameters. */
|
||||
|
@ -344,19 +363,11 @@ _Jv_CallAnyMethodA (jobject obj,
|
|||
|
||||
jclass *paramelts = elements (parameter_types);
|
||||
|
||||
// FIXME: at some point the compiler is going to add extra arguments
|
||||
// to some functions. In particular we are going to do this for
|
||||
// handling access checks in reflection. We must add these hidden
|
||||
// arguments here.
|
||||
|
||||
// Special case for the `this' argument of a constructor. Note that
|
||||
// the JDK 1.2 docs specify that the new object must be allocated
|
||||
// before argument conversions are done.
|
||||
if (is_constructor)
|
||||
{
|
||||
// FIXME: must special-case String, arrays, maybe others here.
|
||||
obj = JvAllocObject (return_type);
|
||||
}
|
||||
obj = JvAllocObject (return_type);
|
||||
|
||||
const int size_per_arg = sizeof(jvalue);
|
||||
ffi_cif cif;
|
||||
|
@ -488,8 +499,6 @@ _Jv_CallAnyMethodA (jobject obj,
|
|||
JArray<jclass> *parameter_types,
|
||||
jobjectArray args)
|
||||
{
|
||||
// FIXME: access checks.
|
||||
|
||||
if (parameter_types->length == 0 && args == NULL)
|
||||
{
|
||||
// The JDK accepts this, so we do too.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue