8266109: More Resilient Classloading

Reviewed-by: lancea, rhalade, mschoene, bchristi
This commit is contained in:
Weijun Wang 2021-06-18 20:26:49 +00:00 committed by Henry Jen
parent a07a046c92
commit 3470e7b300
3 changed files with 27 additions and 0 deletions

View file

@ -427,6 +427,11 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
return defineClass(name, res);
} catch (IOException e) {
throw new ClassNotFoundException(name, e);
} catch (ClassFormatError e2) {
if (res.getDataError() != null) {
e2.addSuppressed(res.getDataError());
}
throw e2;
}
} else {
return null;

View file

@ -187,4 +187,12 @@ public abstract class Resource {
public CodeSigner[] getCodeSigners() {
return null;
}
/**
* Returns non-fatal reading error during data retrieval if there's any.
* For example, CRC error when reading a JAR entry.
*/
public Exception getDataError() {
return null;
}
}

View file

@ -60,6 +60,7 @@ import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.jar.JarFile;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
@ -875,6 +876,7 @@ public class URLClassPath {
}
return new Resource() {
private Exception dataError = null;
public String getName() { return name; }
public URL getURL() { return url; }
public URL getCodeSourceURL() { return csu; }
@ -890,6 +892,18 @@ public class URLClassPath {
{ return entry.getCertificates(); };
public CodeSigner[] getCodeSigners()
{ return entry.getCodeSigners(); };
public Exception getDataError()
{ return dataError; }
public byte[] getBytes() throws IOException {
byte[] bytes = super.getBytes();
CRC32 crc32 = new CRC32();
crc32.update(bytes);
if (crc32.getValue() != entry.getCrc()) {
dataError = new IOException(
"CRC error while extracting entry from JAR file");
}
return bytes;
}
};
}