8259028: ClassCastException when using custom filesystem with wrapper FileChannel impl

Reviewed-by: chegar, alanb, uschindler
This commit is contained in:
Maurizio Cimadamore 2021-01-11 16:14:56 +00:00
parent e05f36f463
commit d60a937e87
3 changed files with 19 additions and 6 deletions

View File

@ -876,10 +876,9 @@ allocateNative(bytesSize, 1);
* @param mapMode a file mapping mode, see {@link FileChannel#map(FileChannel.MapMode, long, long)}; the chosen mapping mode
* might affect the behavior of the returned memory mapped segment (see {@link MappedMemorySegments#force(MemorySegment)}).
* @return a new confined mapped memory segment.
* @throws IllegalArgumentException if {@code bytesOffset < 0}.
* @throws IllegalArgumentException if {@code bytesSize < 0}.
* @throws UnsupportedOperationException if an unsupported map mode is specified, or if the {@code path} is associated
* with a provider that does not support creating file channels.
* @throws IllegalArgumentException if {@code bytesOffset < 0}, {@code bytesSize < 0}, or if {@code path} is not associated
* with the default file system.
* @throws UnsupportedOperationException if an unsupported map mode is specified.
* @throws IOException if the specified path does not point to an existing file, or if some other I/O error occurs.
* @throws SecurityException If a security manager is installed and it denies an unspecified permission required by the implementation.
* In the case of the default provider, the {@link SecurityManager#checkRead(String)} method is invoked to check

View File

@ -34,6 +34,8 @@ import java.io.FileDescriptor;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
@ -114,8 +116,13 @@ public class MappedMemorySegmentImpl extends NativeMemorySegmentImpl {
Objects.requireNonNull(mapMode);
if (bytesSize < 0) throw new IllegalArgumentException("Requested bytes size must be >= 0.");
if (bytesOffset < 0) throw new IllegalArgumentException("Requested bytes offset must be >= 0.");
try (FileChannelImpl channelImpl = (FileChannelImpl)FileChannel.open(path, openOptions(mapMode))) {
UnmapperProxy unmapperProxy = channelImpl.mapInternal(mapMode, bytesOffset, bytesSize);
FileSystem fs = path.getFileSystem();
if (fs != FileSystems.getDefault() ||
fs.getClass().getModule() != Object.class.getModule()) {
throw new IllegalArgumentException("Unsupported file system");
}
try (FileChannel channelImpl = FileChannel.open(path, openOptions(mapMode))) {
UnmapperProxy unmapperProxy = ((FileChannelImpl)channelImpl).mapInternal(mapMode, bytesOffset, bytesSize);
int modes = defaultAccessModes(bytesSize);
if (mapMode == FileChannel.MapMode.READ_ONLY) {
modes &= ~WRITE;

View File

@ -47,6 +47,7 @@ import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -513,6 +514,12 @@ public class TestByteBuffer {
}
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMapCustomPath() throws IOException {
Path path = Path.of(URI.create("jrt:/"));
MemorySegment.mapFile(path, 0L, 0L, FileChannel.MapMode.READ_WRITE);
}
@Test(dataProvider="resizeOps")
public void testCopyHeapToNative(Consumer<MemorySegment> checker, Consumer<MemorySegment> initializer, SequenceLayout seq) {
checkByteArrayAlignment(seq.elementLayout());