8156209: Add argument checks to BasicImageReader calls

Reviewed-by: sundar
This commit is contained in:
Jim Laskey 2016-05-27 12:34:08 -03:00
parent 8cb25f3af9
commit a29401159f
7 changed files with 66 additions and 15 deletions

View File

@ -67,9 +67,9 @@ public class BasicImageReader implements AutoCloseable {
static private final boolean MAP_ALL =
isSystemProperty("jdk.image.map.all", "true", IS_64_BIT ? "true" : "false");
private final String name;
private final ByteOrder byteOrder;
private final Path imagePath;
private final ByteOrder byteOrder;
private final String name;
private final ByteBuffer memoryMap;
private final FileChannel channel;
private final ImageHeader header;
@ -83,11 +83,9 @@ public class BasicImageReader implements AutoCloseable {
protected BasicImageReader(Path path, ByteOrder byteOrder)
throws IOException {
Objects.requireNonNull(path);
Objects.requireNonNull(byteOrder);
this.name = path.toString();
this.byteOrder = byteOrder;
imagePath = path;
this.imagePath = Objects.requireNonNull(path);
this.byteOrder = Objects.requireNonNull(byteOrder);
this.name = this.imagePath.toString();
ByteBuffer map;
@ -211,6 +209,8 @@ public class BasicImageReader implements AutoCloseable {
}
public static void releaseByteBuffer(ByteBuffer buffer) {
Objects.requireNonNull(buffer);
if (!MAP_ALL) {
ImageBufferCache.releaseBuffer(buffer);
}
@ -240,10 +240,14 @@ public class BasicImageReader implements AutoCloseable {
}
public ImageLocation findLocation(String mn, String rn) {
Objects.requireNonNull(mn);
Objects.requireNonNull(rn);
return findLocation("/" + mn + "/" + rn);
}
public synchronized ImageLocation findLocation(String name) {
Objects.requireNonNull(name);
// Details of the algorithm used here can be found in
// jdk.tools.jlink.internal.PerfectHashBuilder.
byte[] bytes = ImageStringsReader.mutf8FromString(name);
@ -287,16 +291,25 @@ public class BasicImageReader implements AutoCloseable {
}
public long[] getAttributes(int offset) {
if (offset < 0 || offset >= locations.limit()) {
throw new IndexOutOfBoundsException("offset");
}
ByteBuffer buffer = slice(locations, offset, locations.limit() - offset);
return ImageLocation.decompress(buffer);
}
public String getString(int offset) {
if (offset < 0 || offset >= strings.limit()) {
throw new IndexOutOfBoundsException("offset");
}
ByteBuffer buffer = slice(strings, offset, strings.limit() - offset);
return ImageStringsReader.stringFromByteBuffer(buffer);
}
private byte[] getBufferBytes(ByteBuffer buffer) {
Objects.requireNonNull(buffer);
byte[] bytes = new byte[buffer.limit()];
buffer.get(bytes);
@ -343,6 +356,7 @@ public class BasicImageReader implements AutoCloseable {
}
public byte[] getResource(String name) {
Objects.requireNonNull(name);
ImageLocation location = findLocation(name);
return location != null ? getResource(location) : null;
@ -362,6 +376,7 @@ public class BasicImageReader implements AutoCloseable {
}
public ByteBuffer getResourceBuffer(ImageLocation loc) {
Objects.requireNonNull(loc);
long offset = loc.getContentOffset() + indexSize;
long compressedSize = loc.getCompressedSize();
long uncompressedSize = loc.getUncompressedSize();
@ -399,6 +414,7 @@ public class BasicImageReader implements AutoCloseable {
}
public InputStream getResourceStream(ImageLocation loc) {
Objects.requireNonNull(loc);
byte[] bytes = getResource(loc);
return new ByteArrayInputStream(bytes);

View File

@ -27,6 +27,7 @@ package jdk.internal.jimage;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Objects;
/**
* @implNote This class needs to maintain JDK 8 source compatibility.
@ -75,6 +76,8 @@ public final class ImageHeader {
}
static ImageHeader readFrom(IntBuffer buffer) {
Objects.requireNonNull(buffer);
if (buffer.capacity() != HEADER_SLOTS) {
throw new InternalError("jimage header not the correct size");
}
@ -94,11 +97,13 @@ public final class ImageHeader {
}
public void writeTo(ImageStream stream) {
Objects.requireNonNull(stream);
stream.ensure(getHeaderSize());
writeTo(stream.getBuffer());
}
public void writeTo(ByteBuffer buffer) {
Objects.requireNonNull(buffer);
buffer.putInt(magic);
buffer.putInt(majorVersion << 16 | minorVersion);
buffer.putInt(flags);

View File

@ -26,6 +26,7 @@
package jdk.internal.jimage;
import java.nio.ByteBuffer;
import java.util.Objects;
/**
* @implNote This class needs to maintain JDK 8 source compatibility.
@ -50,8 +51,8 @@ public class ImageLocation {
protected final ImageStrings strings;
public ImageLocation(long[] attributes, ImageStrings strings) {
this.attributes = attributes;
this.strings = strings;
this.attributes = Objects.requireNonNull(attributes);
this.strings = Objects.requireNonNull(strings);
}
ImageStrings getStrings() {
@ -67,6 +68,7 @@ public class ImageLocation {
}
static long[] decompress(ByteBuffer bytes) {
Objects.requireNonNull(bytes);
long[] attributes = new long[ATTRIBUTE_COUNT];
if (bytes != null) {
@ -103,6 +105,7 @@ public class ImageLocation {
}
public static byte[] compress(long[] attributes) {
Objects.requireNonNull(attributes);
ImageStream stream = new ImageStream(16);
for (int kind = ATTRIBUTE_END + 1; kind < ATTRIBUTE_COUNT; kind++) {
@ -124,6 +127,8 @@ public class ImageLocation {
}
public boolean verify(String name) {
Objects.requireNonNull(name);
return name.equals(getFullName());
}
@ -250,6 +255,7 @@ public class ImageLocation {
}
static ImageLocation readFrom(BasicImageReader reader, int offset) {
Objects.requireNonNull(reader);
long[] attributes = reader.getAttributes(offset);
ImageStringsReader strings = reader.getStrings();

View File

@ -59,6 +59,9 @@ public final class ImageReader implements AutoCloseable {
}
public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
Objects.requireNonNull(imagePath);
Objects.requireNonNull(byteOrder);
return SharedImageReader.open(imagePath, byteOrder);
}
@ -218,6 +221,9 @@ public final class ImageReader implements AutoCloseable {
}
public static ImageReader open(Path imagePath, ByteOrder byteOrder) throws IOException {
Objects.requireNonNull(imagePath);
Objects.requireNonNull(byteOrder);
synchronized (OPEN_FILES) {
SharedImageReader reader = OPEN_FILES.get(imagePath);
@ -237,6 +243,8 @@ public final class ImageReader implements AutoCloseable {
}
public void close(ImageReader image) throws IOException {
Objects.requireNonNull(image);
synchronized (OPEN_FILES) {
if (!openers.remove(image)) {
throw new IOException("image file already closed");

View File

@ -31,6 +31,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Map;
import java.util.Objects;
/**
* Factory to get ImageReader
@ -54,6 +55,7 @@ public class ImageReaderFactory {
* Returns an {@code ImageReader} to read from the given image file
*/
public static ImageReader get(Path jimage) throws IOException {
Objects.requireNonNull(jimage);
ImageReader reader = readers.get(jimage);
if (reader != null) {
return reader;

View File

@ -28,6 +28,7 @@ package jdk.internal.jimage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Objects;
/**
* @implNote This class needs to maintain JDK 8 source compatibility.
@ -57,16 +58,16 @@ public class ImageStream {
public ImageStream(int size, ByteOrder byteOrder) {
buffer = ByteBuffer.allocate(size);
buffer.order(byteOrder);
buffer.order(Objects.requireNonNull(byteOrder));
}
public ImageStream(byte[] bytes, ByteOrder byteOrder) {
buffer = ByteBuffer.wrap(bytes);
buffer.order(byteOrder);
buffer = ByteBuffer.wrap(Objects.requireNonNull(bytes));
buffer.order(Objects.requireNonNull(byteOrder));
}
public ImageStream(ByteBuffer buffer) {
this.buffer = buffer;
this.buffer = Objects.requireNonNull(buffer);
}
public ImageStream align(int alignment) {

View File

@ -27,6 +27,7 @@ package jdk.internal.jimage;
import java.io.UTFDataFormatException;
import java.nio.ByteBuffer;
import java.util.Objects;
/**
* @implNote This class needs to maintain JDK 8 source compatibility.
@ -40,7 +41,7 @@ public class ImageStringsReader implements ImageStrings {
private final BasicImageReader reader;
ImageStringsReader(BasicImageReader reader) {
this.reader = reader;
this.reader = Objects.requireNonNull(reader);
}
@Override
@ -54,7 +55,19 @@ public class ImageStringsReader implements ImageStrings {
}
private static int hashCode(byte[] bytes, int offset, int count, int seed) {
for (int i = offset, limit = offset + count; i < limit; i++) {
Objects.requireNonNull(bytes);
if (offset < 0 || offset >= bytes.length) {
throw new IndexOutOfBoundsException("offset");
}
int limit = offset + count;
if (limit < 0 || limit > bytes.length) {
throw new IndexOutOfBoundsException("limit");
}
for (int i = offset; i < limit; i++) {
seed = (seed * HASH_MULTIPLIER) ^ (bytes[i] & 0xFF);
}