提交 5c89aa59 编写于 作者: C chegar

8008593: Better URLClassLoader resource management

Reviewed-by: alanb, sherman, hawtin
上级 104c1e52
...@@ -65,6 +65,7 @@ SUNWprivate_1.1 { ...@@ -65,6 +65,7 @@ SUNWprivate_1.1 {
Java_java_util_zip_ZipFile_initIDs; Java_java_util_zip_ZipFile_initIDs;
Java_java_util_zip_ZipFile_open; Java_java_util_zip_ZipFile_open;
Java_java_util_zip_ZipFile_read; Java_java_util_zip_ZipFile_read;
Java_java_util_zip_ZipFile_startsWithLOC;
ZIP_Close; ZIP_Close;
ZIP_CRC32; ZIP_CRC32;
......
...@@ -19,6 +19,7 @@ text: .text%ZIP_FreeEntry; ...@@ -19,6 +19,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry; ...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry; ...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -65,6 +65,7 @@ SUNWprivate_1.1 { ...@@ -65,6 +65,7 @@ SUNWprivate_1.1 {
Java_java_util_zip_ZipFile_initIDs; Java_java_util_zip_ZipFile_initIDs;
Java_java_util_zip_ZipFile_open; Java_java_util_zip_ZipFile_open;
Java_java_util_zip_ZipFile_read; Java_java_util_zip_ZipFile_read;
Java_java_util_zip_ZipFile_startsWithLOC;
ZIP_Close; ZIP_Close;
ZIP_CRC32; ZIP_CRC32;
......
...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry; ...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry; ...@@ -18,6 +18,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -19,6 +19,7 @@ text: .text%ZIP_FreeEntry; ...@@ -19,6 +19,7 @@ text: .text%ZIP_FreeEntry;
text: .text%Java_java_util_zip_ZipFile_initIDs; text: .text%Java_java_util_zip_ZipFile_initIDs;
text: .text%Java_java_util_zip_ZipFile_open; text: .text%Java_java_util_zip_ZipFile_open;
text: .text%Java_java_util_zip_ZipFile_getTotal; text: .text%Java_java_util_zip_ZipFile_getTotal;
text: .text%Java_java_util_zip_ZipFile_startsWithLOC;
text: .text%Java_java_util_zip_ZipFile_getEntry; text: .text%Java_java_util_zip_ZipFile_getEntry;
text: .text%Java_java_util_zip_ZipFile_freeEntry; text: .text%Java_java_util_zip_ZipFile_freeEntry;
text: .text%Java_java_util_zip_ZipFile_getEntryTime; text: .text%Java_java_util_zip_ZipFile_getEntryTime;
......
...@@ -54,9 +54,10 @@ import static java.util.zip.ZipConstants64.*; ...@@ -54,9 +54,10 @@ import static java.util.zip.ZipConstants64.*;
*/ */
public public
class ZipFile implements ZipConstants, Closeable { class ZipFile implements ZipConstants, Closeable {
private long jzfile; // address of jzfile data private long jzfile; // address of jzfile data
private String name; // zip file name private final String name; // zip file name
private int total; // total number of entries private final int total; // total number of entries
private final boolean locsig; // if zip file starts with LOCSIG (usually true)
private volatile boolean closeRequested = false; private volatile boolean closeRequested = false;
private static final int STORED = ZipEntry.STORED; private static final int STORED = ZipEntry.STORED;
...@@ -216,6 +217,7 @@ class ZipFile implements ZipConstants, Closeable { ...@@ -216,6 +217,7 @@ class ZipFile implements ZipConstants, Closeable {
sun.misc.PerfCounter.getZipFileCount().increment(); sun.misc.PerfCounter.getZipFileCount().increment();
this.name = name; this.name = name;
this.total = getTotal(jzfile); this.total = getTotal(jzfile);
this.locsig = startsWithLOC(jzfile);
} }
/** /**
...@@ -737,10 +739,28 @@ class ZipFile implements ZipConstants, Closeable { ...@@ -737,10 +739,28 @@ class ZipFile implements ZipConstants, Closeable {
} }
} }
static {
sun.misc.SharedSecrets.setJavaUtilZipFileAccess(
new sun.misc.JavaUtilZipFileAccess() {
public boolean startsWithLocHeader(ZipFile zip) {
return zip.startsWithLocHeader();
}
}
);
}
/**
* Returns {@code true} if, and only if, the zip file begins with {@code
* LOCSIG}.
*/
private boolean startsWithLocHeader() {
return locsig;
}
private static native long open(String name, int mode, long lastModified, private static native long open(String name, int mode, long lastModified,
boolean usemmap) throws IOException; boolean usemmap) throws IOException;
private static native int getTotal(long jzfile); private static native int getTotal(long jzfile);
private static native boolean startsWithLOC(long jzfile);
private static native int read(long jzfile, long jzentry, private static native int read(long jzfile, long jzentry,
long pos, byte[] b, int off, int len); long pos, byte[] b, int off, int len);
......
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.misc;
import java.util.zip.ZipFile;
public interface JavaUtilZipFileAccess {
public boolean startsWithLocHeader(ZipFile zip);
}
...@@ -52,6 +52,7 @@ public class SharedSecrets { ...@@ -52,6 +52,7 @@ public class SharedSecrets {
private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess; private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess; private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess;
private static JavaSecurityAccess javaSecurityAccess; private static JavaSecurityAccess javaSecurityAccess;
private static JavaUtilZipFileAccess javaUtilZipFileAccess;
private static JavaAWTAccess javaAWTAccess; private static JavaAWTAccess javaAWTAccess;
public static JavaUtilJarAccess javaUtilJarAccess() { public static JavaUtilJarAccess javaUtilJarAccess() {
...@@ -152,6 +153,16 @@ public class SharedSecrets { ...@@ -152,6 +153,16 @@ public class SharedSecrets {
return javaSecurityAccess; return javaSecurityAccess;
} }
public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
if (javaUtilZipFileAccess == null)
unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
return javaUtilZipFileAccess;
}
public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
javaUtilZipFileAccess = access;
}
public static void setJavaAWTAccess(JavaAWTAccess jaa) { public static void setJavaAWTAccess(JavaAWTAccess jaa) {
javaAWTAccess = jaa; javaAWTAccess = jaa;
} }
......
...@@ -64,6 +64,7 @@ public class URLClassPath { ...@@ -64,6 +64,7 @@ public class URLClassPath {
final static String USER_AGENT_JAVA_VERSION = "UA-Java-Version"; final static String USER_AGENT_JAVA_VERSION = "UA-Java-Version";
final static String JAVA_VERSION; final static String JAVA_VERSION;
private static final boolean DEBUG; private static final boolean DEBUG;
private static final boolean DISABLE_JAR_CHECKING;
/** /**
* Used by launcher to indicate that checking of the JAR file "Profile" * Used by launcher to indicate that checking of the JAR file "Profile"
...@@ -76,6 +77,9 @@ public class URLClassPath { ...@@ -76,6 +77,9 @@ public class URLClassPath {
new sun.security.action.GetPropertyAction("java.version")); new sun.security.action.GetPropertyAction("java.version"));
DEBUG = (java.security.AccessController.doPrivileged( DEBUG = (java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.debug")) != null); new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.debug")) != null);
String p = java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.disableJarChecking"));
DISABLE_JAR_CHECKING = p != null ? p.equals("true") || p.equals("") : false;
} }
/* The original search path of URLs. */ /* The original search path of URLs. */
...@@ -544,7 +548,7 @@ public class URLClassPath { ...@@ -544,7 +548,7 @@ public class URLClassPath {
* in a hurry. * in a hurry.
*/ */
JarURLConnection juc = (JarURLConnection)uc; JarURLConnection juc = (JarURLConnection)uc;
jarfile = juc.getJarFile(); jarfile = JarLoader.checkJar(juc.getJarFile());
} }
} catch (Exception e) { } catch (Exception e) {
return null; return null;
...@@ -609,6 +613,8 @@ public class URLClassPath { ...@@ -609,6 +613,8 @@ public class URLClassPath {
private URLStreamHandler handler; private URLStreamHandler handler;
private HashMap<String, Loader> lmap; private HashMap<String, Loader> lmap;
private boolean closed = false; private boolean closed = false;
private static final sun.misc.JavaUtilZipFileAccess zipAccess =
sun.misc.SharedSecrets.getJavaUtilZipFileAccess();
/* /*
* Creates a new JarLoader for the specified URL referring to * Creates a new JarLoader for the specified URL referring to
...@@ -713,6 +719,14 @@ public class URLClassPath { ...@@ -713,6 +719,14 @@ public class URLClassPath {
} }
} }
/* Throws if the given jar file is does not start with the correct LOC */
static JarFile checkJar(JarFile jar) throws IOException {
if (System.getSecurityManager() != null && !DISABLE_JAR_CHECKING
&& !zipAccess.startsWithLocHeader(jar))
throw new IOException("Invalid Jar file");
return jar;
}
private JarFile getJarFile(URL url) throws IOException { private JarFile getJarFile(URL url) throws IOException {
// Optimize case where url refers to a local jar file // Optimize case where url refers to a local jar file
if (isOptimizable(url)) { if (isOptimizable(url)) {
...@@ -720,11 +734,12 @@ public class URLClassPath { ...@@ -720,11 +734,12 @@ public class URLClassPath {
if (!p.exists()) { if (!p.exists()) {
throw new FileNotFoundException(p.getPath()); throw new FileNotFoundException(p.getPath());
} }
return new JarFile (p.getPath()); return checkJar(new JarFile(p.getPath()));
} }
URLConnection uc = getBaseURL().openConnection(); URLConnection uc = getBaseURL().openConnection();
uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION); uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
return ((JarURLConnection)uc).getJarFile(); JarFile jarFile = ((JarURLConnection)uc).getJarFile();
return checkJar(jarFile);
} }
/* /*
......
...@@ -137,6 +137,14 @@ Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile) ...@@ -137,6 +137,14 @@ Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
return zip->total; return zip->total;
} }
JNIEXPORT jboolean JNICALL
Java_java_util_zip_ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile)
{
jzfile *zip = jlong_to_ptr(zfile);
return zip->locsig;
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile) Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
{ {
......
...@@ -831,6 +831,14 @@ ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified, ...@@ -831,6 +831,14 @@ ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified,
return NULL; return NULL;
} }
// Assumption, zfd refers to start of file. Trivially, reuse errbuf.
if (readFully(zfd, errbuf, 4) != -1) { // errors will be handled later
if (GETSIG(errbuf) == LOCSIG)
zip->locsig = JNI_TRUE;
else
zip->locsig = JNI_FALSE;
}
len = zip->len = IO_Lseek(zfd, 0, SEEK_END); len = zip->len = IO_Lseek(zfd, 0, SEEK_END);
if (len <= 0) { if (len <= 0) {
if (len == 0) { /* zip file is empty */ if (len == 0) { /* zip file is empty */
......
...@@ -210,6 +210,7 @@ typedef struct jzfile { /* Zip file */ ...@@ -210,6 +210,7 @@ typedef struct jzfile { /* Zip file */
start of the file. */ start of the file. */
jboolean usemmap; /* if mmap is used. */ jboolean usemmap; /* if mmap is used. */
#endif #endif
jboolean locsig; /* if zip file starts with LOCSIG */
cencache cencache; /* CEN header cache */ cencache cencache; /* CEN header cache */
ZFILE zfd; /* open file descriptor */ ZFILE zfd; /* open file descriptor */
void *lock; /* read lock */ void *lock; /* read lock */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册