Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8259321

ClassCastException when using custom filesystem with wrapper FileChannel impl

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 16
    • core-libs
    • None
    • behavioral
    • minimal
    • `MemorySegment::mapFile` was already misbehaving when used with custom file system providers. This change should make the failure mode clearer.
    • Java API
    • JDK

      Summary

      The Foreign Memory Access API allows creation of mapped memory segments using the MemorySegment::mapFile factory, which takes a Path argument. The behavior of this method is unspecified when custom file system providers are used.

      Problem

      Currently, the implementation of MemorySegment::mapFile obtains a FileChannel from the supplied Path. It then downcast said channel to FileChannelImpl. However, if a custom file system is used, this cast can fail.

      Solution

      Ideally, MemorySegment::mapFile should work with any path. However doing so is very difficult, because the behavior of FileChannelImpl::map are completely tied to the JDK internals, and the file system API is currently not powerful enough to allow custom file system providers to specify their own mappings.

      As an interim solution, it is preferrable to clarify the javadoc of the MemorySegment::mapFile factory, so that it is clear that the factory will fail whenever a path associated with a custom file system provider is used.

      This is similar to what happens with UnixDomainSocketAddress::of(Path).

      Specification

      Proposed diff in the javadoc:

      diff --git a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java
      index d5360101dd3..64c8dc2b637 100644
      --- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java
      +++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MemorySegment.java
      @@ -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

            mcimadamore Maurizio Cimadamore
            uschindler Uwe Schindler
            Alan Bateman, Chris Hegarty
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: