Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
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 b7940aa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Expand Up @@ -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);
}
}
}

Expand All @@ -135,4 +139,32 @@ private static OpenOption[] openOptions(FileChannel.MapMode mapMode) {
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
}
};
}
18 changes: 18 additions & 0 deletions test/jdk/java/foreign/TestByteBuffer.java
Expand Up @@ -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);
}
}

Expand Down

1 comment on commit b7940aa

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.