Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8259027: NullPointerException in makeMappedSegment due to NULL Unmapp…
…er when length of segment is 0

Reviewed-by: chegar, uschindler
  • Loading branch information
mcimadamore committed Jan 5, 2021
1 parent bbc2e95 commit b7940aa1c4952e206162d4d46e0aca08adae52b9
Showing with 53 additions and 3 deletions.
  1. +35 −3 src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MappedMemorySegmentImpl.java
  2. +18 −0 test/jdk/java/foreign/TestByteBuffer.java
@@ -116,13 +116,17 @@ public static MemorySegment makeMappedSegment(Path path, long bytesOffset, long
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);
MemoryScope scope = MemoryScope.createConfined(null, unmapperProxy::unmap, null);
int modes = defaultAccessModes(bytesSize);
if (mapMode == FileChannel.MapMode.READ_ONLY) {
modes &= ~WRITE;
}
return new MappedMemorySegmentImpl(unmapperProxy.address(), unmapperProxy, bytesSize,
modes, scope);
if (unmapperProxy != null) {
MemoryScope scope = MemoryScope.createConfined(null, unmapperProxy::unmap, null);
return new MappedMemorySegmentImpl(unmapperProxy.address(), unmapperProxy, bytesSize,
modes, scope);
} else {
return new EmptyMappedMemorySegmentImpl(modes);
}
}
}

@@ -135,4 +139,32 @@ public static MemorySegment makeMappedSegment(Path path, long bytesOffset, long
throw new UnsupportedOperationException("Unsupported map mode: " + mapMode);
}
}

static class EmptyMappedMemorySegmentImpl extends MappedMemorySegmentImpl {

public EmptyMappedMemorySegmentImpl(int modes) {
super(0, null, 0, modes,
MemoryScope.createConfined(null, MemoryScope.DUMMY_CLEANUP_ACTION, null));
}

@Override
public void load() {
// do nothing
}

@Override
public void unload() {
// do nothing
}

@Override
public boolean isLoaded() {
return true;
}

@Override
public void force() {
// do nothing
}
};
}
@@ -464,12 +464,30 @@ public void testBadMapNegativeOffset() throws IOException {
MemorySegment.mapFile(f.toPath(), -1, 1, FileChannel.MapMode.READ_WRITE);
}

@Test
public void testMapZeroSize() throws IOException {
File f = new File("testPos1.out");
f.createNewFile();
f.deleteOnExit();
//RW
try (MemorySegment segment = MemorySegment.mapFile(f.toPath(), 0L, 0L, FileChannel.MapMode.READ_WRITE)) {
assertEquals(segment.byteSize(), 0);
assertEquals(segment.isMapped(), true);
assertTrue((segment.accessModes() & (READ | WRITE)) == (READ | WRITE));
MappedMemorySegments.force(segment);
MappedMemorySegments.load(segment);
MappedMemorySegments.isLoaded(segment);
MappedMemorySegments.unload(segment);
}
//RO
try (MemorySegment segment = MemorySegment.mapFile(f.toPath(), 0L, 0L, FileChannel.MapMode.READ_ONLY)) {
assertEquals(segment.byteSize(), 0);
assertEquals(segment.isMapped(), true);
assertTrue((segment.accessModes() & (READ | WRITE)) == READ);
MappedMemorySegments.force(segment);
MappedMemorySegments.load(segment);
MappedMemorySegments.isLoaded(segment);
MappedMemorySegments.unload(segment);
}
}

1 comment on commit b7940aa

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on b7940aa Jan 5, 2021

Please sign in to comment.