From ec858596f78dd4ddc318edd3d3f4ec537780c854 Mon Sep 17 00:00:00 2001 From: Archie Cobbs Date: Fri, 7 Jan 2005 15:08:21 +0000 Subject: Re-committing now that the 0.13 release has been tagged: * NEWS: Document changes. * java/lang/Class.java (newInstance(), getClassLoader(), forName(String), forName(String, boolean, ClassLoader)): Use new VMStackWalker methods. * java/lang/ClassLoader.java (getParent(), getSystemClassLoader()): Likewise. * java/lang/Package.java (getPackages()): Likewise. * java/lang/SecurityManager.java (getClassContext()): Likewise. * java/util/ResourceBundle.java (getBundle()): Likewise. * java/lang/Runtime.java (load(), loadLibrary()): Load the native library using the calling class' class loader. * java/lang/System.java (load(), loadLibrary()): Likewise. (currentClassLoader()): implement via currentLoadedClass(). * vm/reference/gnu/classpath/VMStackWalker.java: New class. * vm/reference/java/lang/VMRuntime.java (nativeLoad()): Add a ClassLoader parameter. * vm/reference/java/lang/VMSecurityManager.java: Removed. --- java/lang/Class.java | 19 +++++------ java/lang/ClassLoader.java | 7 ++-- java/lang/Package.java | 7 ++-- java/lang/Runtime.java | 72 ++++++++++++++++++++++++++++++------------ java/lang/SecurityManager.java | 10 ++++-- java/lang/System.java | 14 ++++++-- 6 files changed, 88 insertions(+), 41 deletions(-) (limited to 'java/lang') diff --git a/java/lang/Class.java b/java/lang/Class.java index 7bd3a3b09..d3bc1dea4 100644 --- a/java/lang/Class.java +++ b/java/lang/Class.java @@ -38,6 +38,8 @@ exception statement from your version. */ package java.lang; +import gnu.classpath.VMStackWalker; + import java.io.InputStream; import java.io.Serializable; import java.lang.reflect.Constructor; @@ -157,7 +159,7 @@ public final class Class implements Serializable Class result = VMClass.forName (name); if (result == null) result = Class.forName(name, true, - VMSecurityManager.getClassContext()[1].getClassLoader()); + VMStackWalker.getCallingClassLoader()); return result; } @@ -198,9 +200,8 @@ public final class Class implements Serializable SecurityManager sm = SecurityManager.current; if (sm != null) { - // Get the calling class and classloader - Class c = VMSecurityManager.getClassContext()[1]; - ClassLoader cl = c.getClassLoader(); + // Get the calling classloader + ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl != null) sm.checkPermission(new RuntimePermission("getClassLoader")); } @@ -278,9 +279,8 @@ public final class Class implements Serializable SecurityManager sm = SecurityManager.current; if (sm != null) { - // Get the calling class and classloader - Class c = VMSecurityManager.getClassContext()[1]; - ClassLoader cl = VMClass.getClassLoader(c); + // Get the calling classloader + ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl != null && !cl.isAncestorOf(loader)) sm.checkPermission(new RuntimePermission("getClassLoader")); } @@ -1132,8 +1132,9 @@ public final class Class implements Serializable int modifiers = constructor.getModifiers(); if (!Modifier.isPublic(modifiers)) { - Class caller = VMSecurityManager.getClassContext()[1]; - if (caller != this && + Class caller = VMStackWalker.getCallingClass(); + if (caller != null && + caller != this && (Modifier.isPrivate(modifiers) || getClassLoader() != caller.getClassLoader() || !getPackagePortion(getName()) diff --git a/java/lang/ClassLoader.java b/java/lang/ClassLoader.java index 943bab5ad..a5093136d 100644 --- a/java/lang/ClassLoader.java +++ b/java/lang/ClassLoader.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.lang; import gnu.classpath.SystemProperties; +import gnu.classpath.VMStackWalker; import gnu.java.util.DoubleEnumeration; import gnu.java.util.EmptyEnumeration; @@ -520,8 +521,7 @@ public abstract class ClassLoader SecurityManager sm = SecurityManager.current; if (sm != null) { - Class c = VMSecurityManager.getClassContext()[1]; - ClassLoader cl = c.getClassLoader(); + ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl != null && ! cl.isAncestorOf(this)) sm.checkPermission(new RuntimePermission("getClassLoader")); } @@ -763,8 +763,7 @@ public abstract class ClassLoader SecurityManager sm = SecurityManager.current; if (sm != null) { - Class c = VMSecurityManager.getClassContext()[1]; - ClassLoader cl = c.getClassLoader(); + ClassLoader cl = VMStackWalker.getCallingClassLoader(); if (cl != null && cl != StaticData.systemClassLoader) sm.checkPermission(new RuntimePermission("getClassLoader")); } diff --git a/java/lang/Package.java b/java/lang/Package.java index 89945cadd..23838d681 100644 --- a/java/lang/Package.java +++ b/java/lang/Package.java @@ -37,6 +37,8 @@ exception statement from your version. */ package java.lang; +import gnu.classpath.VMStackWalker; + import java.net.URL; import java.util.NoSuchElementException; import java.util.StringTokenizer; @@ -273,7 +275,7 @@ public class Package public static Package getPackage(String name) { // Get the caller's classloader - ClassLoader cl = VMSecurityManager.currentClassLoader(); + ClassLoader cl = VMStackWalker.getCallingClassLoader(); return cl != null ? cl.getPackage(name) : null; } @@ -286,8 +288,7 @@ public class Package public static Package[] getPackages() { // Get the caller's classloader - Class c = VMSecurityManager.getClassContext()[1]; - ClassLoader cl = c.getClassLoader(); + ClassLoader cl = VMStackWalker.getCallingClassLoader(); // Sun's implementation returns the packages loaded by the bootstrap // classloader if cl is null, but right now our bootstrap classloader // does not create any Packages. diff --git a/java/lang/Runtime.java b/java/lang/Runtime.java index 58d1f5c0d..d0e7ac593 100644 --- a/java/lang/Runtime.java +++ b/java/lang/Runtime.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.lang; import gnu.classpath.SystemProperties; +import gnu.classpath.VMStackWalker; import java.io.File; import java.io.IOException; @@ -628,16 +629,33 @@ public class Runtime * before the final ".so" if the VM was invoked by the name "java_g". There * may be a security check, of checkLink. * + *

+ * The library is loaded using the class loader associated with the + * class associated with the invoking method. + * * @param filename the file to load * @throws SecurityException if permission is denied * @throws UnsatisfiedLinkError if the library is not found */ public void load(String filename) + { + load(filename, VMStackWalker.getCallingClassLoader()); + } + + /** + * Same as load(String) but using the given loader. + * + * @param filename the file to load + * @param loader class loader, or null for the boot loader + * @throws SecurityException if permission is denied + * @throws UnsatisfiedLinkError if the library is not found + */ + void load(String filename, ClassLoader loader) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkLink(filename); - if (loadLib(filename) == 0) + if (loadLib(filename, loader) == 0) throw new UnsatisfiedLinkError("Could not load library " + filename); } @@ -645,15 +663,16 @@ public class Runtime * Do a security check on the filename and then load the native library. * * @param filename the file to load + * @param loader class loader, or null for the boot loader * @return 0 on failure, nonzero on success * @throws SecurityException if file read permission is denied */ - private static int loadLib(String filename) + private static int loadLib(String filename, ClassLoader loader) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkRead(filename); - return VMRuntime.nativeLoad(filename); + return VMRuntime.nativeLoad(filename, loader); } /** @@ -668,6 +687,10 @@ public class Runtime * System.mapLibraryName(libname). There may be a security * check, of checkLink. * + *

+ * The library is loaded using the class loader associated with the + * class associated with the invoking method. + * * @param libname the library to load * * @throws SecurityException if permission is denied @@ -677,31 +700,38 @@ public class Runtime * @see ClassLoader#findLibrary(String) */ public void loadLibrary(String libname) + { + loadLibrary(libname, VMStackWalker.getCallingClassLoader()); + } + + /** + * Same as loadLibrary(String) but using the given loader. + * + * @param libname the library to load + * @param loader class loader, or null for the boot loader + * @throws SecurityException if permission is denied + * @throws UnsatisfiedLinkError if the library is not found + */ + void loadLibrary(String libname, ClassLoader loader) { SecurityManager sm = SecurityManager.current; // Be thread-safe! if (sm != null) sm.checkLink(libname); - String filename; - ClassLoader cl = VMSecurityManager.currentClassLoader(); - if (cl != null) + if (loader != null && (filename = loader.findLibrary(libname)) != null) { - filename = cl.findLibrary(libname); - if (filename != null) - { - if (loadLib(filename) != 0) - return; - else - throw new UnsatisfiedLinkError("Could not load library " + filename); - } + if (loadLib(filename, loader) != 0) + return; } - - filename = VMRuntime.mapLibraryName(libname); - for (int i = 0; i < libpath.length; i++) - if (loadLib(libpath[i] + filename) != 0) - return; - - throw new UnsatisfiedLinkError("Could not find library " + libname + "."); + else + { + filename = VMRuntime.mapLibraryName(libname); + for (int i = 0; i < libpath.length; i++) + if (loadLib(libpath[i] + filename, loader) != 0) + return; + } + throw new UnsatisfiedLinkError("Native library `" + libname + + "' not found (as file `" + filename + "')"); } /** diff --git a/java/lang/SecurityManager.java b/java/lang/SecurityManager.java index d5c47a8d8..f47b2ff83 100644 --- a/java/lang/SecurityManager.java +++ b/java/lang/SecurityManager.java @@ -38,6 +38,8 @@ exception statement from your version. */ package java.lang; +import gnu.classpath.VMStackWalker; + import java.awt.AWTPermission; import java.io.File; import java.io.FileDescriptor; @@ -179,7 +181,10 @@ public class SecurityManager */ protected Class[] getClassContext() { - return VMSecurityManager.getClassContext(); + Class[] stack1 = VMStackWalker.getClassContext(); + Class[] stack2 = new Class[stack1.length - 1]; + System.arraycopy(stack1, 1, stack2, 0, stack1.length - 1); + return stack2; } /** @@ -201,7 +206,8 @@ public class SecurityManager */ protected ClassLoader currentClassLoader() { - return VMSecurityManager.currentClassLoader(); + Class cl = currentLoadedClass(); + return cl != null ? cl.getClassLoader() : null; } /** diff --git a/java/lang/System.java b/java/lang/System.java index a639cac40..d0e32a3e5 100644 --- a/java/lang/System.java +++ b/java/lang/System.java @@ -39,6 +39,7 @@ exception statement from your version. */ package java.lang; +import gnu.classpath.VMStackWalker; import gnu.classpath.SystemProperties; import java.io.InputStream; @@ -480,6 +481,10 @@ public final class System * check may be performed, checkLink. This just calls * Runtime.getRuntime().load(filename). * + *

+ * The library is loaded using the class loader associated with the + * class associated with the invoking method. + * * @param filename the code file to load * @throws SecurityException if permission is denied * @throws UnsatisfiedLinkError if the file cannot be loaded @@ -487,7 +492,7 @@ public final class System */ public static void load(String filename) { - Runtime.getRuntime().load(filename); + Runtime.getRuntime().load(filename, VMStackWalker.getCallingClassLoader()); } /** @@ -495,6 +500,10 @@ public final class System * check may be performed, checkLink. This just calls * Runtime.getRuntime().load(filename). * + *

+ * The library is loaded using the class loader associated with the + * class associated with the invoking method. + * * @param libname the library file to load * @throws SecurityException if permission is denied * @throws UnsatisfiedLinkError if the file cannot be loaded @@ -502,7 +511,8 @@ public final class System */ public static void loadLibrary(String libname) { - Runtime.getRuntime().loadLibrary(libname); + Runtime.getRuntime().loadLibrary(libname, + VMStackWalker.getCallingClassLoader()); } /** -- cgit v1.2.1