[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
currentClassLoader problem
From: |
Andrew Haley |
Subject: |
currentClassLoader problem |
Date: |
Mon, 15 Mar 2004 10:38:54 +0000 |
David Holmes writes:
> I'm having a lot of trouble trying to understand what needs to be done to
> correctly set up class loaders in Classpath.
>
> Present point of confusion is SecurityManager.currentClassLoader and its
> helper VMSecurityManager.currentClassLoader.
>
> The docs for SecurityManager.currentClassLoader states that it returns:
>
> ClassLoader of the first non-system class on the execution
> stack. A non-system class is one whose ClassLoader is not equal to
> address@hidden ClassLoader#getSystemClassLoader()} or its ancestors.
>
> and it simply returns VMSecurityManager.currentClassLoader() *but* the docs
> for VMSecurityManager.currentClassLoader state that it returns:
>
> This is the first non-null class loader
> * on the stack, if one exists, stopping either at the end of the stack
> * or the first instance of a PrivilegedAction. In other words, this call
> * automatically unwinds past all classes loaded by the bootstrap loader,
> * where getClassLoader() returns null, to get to the user class that
> * really invoked the call that needs a classloader.
>
> These two descriptions do not match, so the delegation from SecurityManager
> to VMSecurityManager is incorrect as the latter would return the
> system/application classloader, while the former should not return that.
> There seems to be confusion between the bootstrap loader (which is often
> null) and the system/application loader (as returned by
> getSystemClassLoader()) which is never null.
What we do at the moment is wrong. I've needed to make quite a few
changes, and I'm sorry that I haven't folded them back into the main
sources.
Andrew.
Index: java/lang/ClassLoader.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/ClassLoader.java,v
retrieving revision 1.31
diff -c -2 -p -r1.31 ClassLoader.java
*** java/lang/ClassLoader.java 9 Oct 2003 16:24:55 -0000 1.31
--- java/lang/ClassLoader.java 15 Mar 2004 09:54:49 -0000
*************** public abstract class ClassLoader
*** 729,733 ****
{
Class c = VMSecurityManager.getClassContext()[1];
! ClassLoader cl = c.getClassLoader();
if (cl != null && cl != systemClassLoader)
sm.checkPermission(new RuntimePermission("getClassLoader"));
--- 742,746 ----
{
Class c = VMSecurityManager.getClassContext()[1];
! ClassLoader cl = getClassLoader0(c);
if (cl != null && cl != systemClassLoader)
sm.checkPermission(new RuntimePermission("getClassLoader"));
*************** public abstract class ClassLoader
*** 950,952 ****
--- 963,967 ----
return false;
}
+
+ static private final native ClassLoader getClassLoader0(Class c);
}
Index: java/lang/natClassLoader.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClassLoader.cc,v
retrieving revision 1.63
diff -c -2 -p -r1.63 natClassLoader.cc
*** java/lang/natClassLoader.cc 9 Jan 2004 17:10:43 -0000 1.63
--- java/lang/natClassLoader.cc 15 Mar 2004 09:54:56 -0000
*************** java::lang::VMClassLoader::loadClass(jst
*** 147,150 ****
--- 147,158 ----
}
+ // Return the true ClassLoader for a class, without doing security checks
+ ::java::lang::ClassLoader *
+ ::java::lang::ClassLoader::getClassLoader0 (::java::lang::Class *c)
+ {
+ return c->loader;
+ }
+
+
void
_Jv_WaitForState (jclass klass, int state)
Index: java/lang/VMSecurityManager.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/VMSecurityManager.java,v
retrieving revision 1.2
diff -c -2 -p -r1.2 VMSecurityManager.java
*** java/lang/VMSecurityManager.java 5 Dec 2002 00:49:29 -0000 1.2
--- java/lang/VMSecurityManager.java 15 Mar 2004 09:54:54 -0000
*************** class VMSecurityManager
*** 55,63 ****
// FIXME this implementation is a bit wrong too -- the docs say we
// must also consider ancestors of the system class loader.
Class[] classStack = getClassContext ();
for (int i = 0; i < classStack.length; i++)
{
ClassLoader loader = classStack[i].getClassLoader();
! if (loader != null)
return loader;
}
--- 55,64 ----
// FIXME this implementation is a bit wrong too -- the docs say we
// must also consider ancestors of the system class loader.
+ ClassLoader systemClassLoader = VMClassLoader.getSystemClassLoader();
Class[] classStack = getClassContext ();
for (int i = 0; i < classStack.length; i++)
{
ClassLoader loader = classStack[i].getClassLoader();
! if (loader != null && loader != systemClassLoader)
return loader;
}