undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
84
test/jdk/java/nio/MappedByteBuffer/Basic.java
Normal file
84
test/jdk/java/nio/MappedByteBuffer/Basic.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4462336 6799037
|
||||
* @summary Simple MappedByteBuffer tests
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
public class Basic {
|
||||
public static void main(String[] args) throws Exception {
|
||||
byte[] srcData = new byte[20];
|
||||
for (int i=0; i<20; i++)
|
||||
srcData[i] = 3;
|
||||
File blah = File.createTempFile("blah", null);
|
||||
blah.deleteOnExit();
|
||||
FileOutputStream fos = new FileOutputStream(blah);
|
||||
FileChannel fc = fos.getChannel();
|
||||
fc.write(ByteBuffer.wrap(srcData));
|
||||
fc.close();
|
||||
fos.close();
|
||||
|
||||
FileInputStream fis = new FileInputStream(blah);
|
||||
fc = fis.getChannel();
|
||||
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
|
||||
mbb.load();
|
||||
mbb.isLoaded();
|
||||
mbb.force();
|
||||
if (!mbb.isReadOnly())
|
||||
throw new RuntimeException("Incorrect isReadOnly");
|
||||
|
||||
// repeat with unaligned position in file
|
||||
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 1, 10);
|
||||
mbb.load();
|
||||
mbb.isLoaded();
|
||||
mbb.force();
|
||||
fc.close();
|
||||
fis.close();
|
||||
|
||||
RandomAccessFile raf = new RandomAccessFile(blah, "r");
|
||||
fc = raf.getChannel();
|
||||
mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, 10);
|
||||
if (!mbb.isReadOnly())
|
||||
throw new RuntimeException("Incorrect isReadOnly");
|
||||
fc.close();
|
||||
raf.close();
|
||||
|
||||
raf = new RandomAccessFile(blah, "rw");
|
||||
fc = raf.getChannel();
|
||||
mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, 10);
|
||||
if (mbb.isReadOnly())
|
||||
throw new RuntimeException("Incorrect isReadOnly");
|
||||
fc.close();
|
||||
raf.close();
|
||||
|
||||
// clean-up
|
||||
mbb = null;
|
||||
System.gc();
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
75
test/jdk/java/nio/MappedByteBuffer/Force.java
Normal file
75
test/jdk/java/nio/MappedByteBuffer/Force.java
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4625907 8246729
|
||||
* @summary Testing force()
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.RandomFactory
|
||||
* @run main/othervm Force
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import static java.nio.file.StandardOpenOption.*;
|
||||
import java.util.Random;
|
||||
import jdk.test.lib.RandomFactory;
|
||||
|
||||
public class Force {
|
||||
public static void main(String[] args) throws Exception {
|
||||
test1();
|
||||
test2();
|
||||
}
|
||||
|
||||
private static void test1() throws Exception {
|
||||
Random random = RandomFactory.getRandom();
|
||||
long filesize = random.nextInt(3*1024*1024);
|
||||
int cut = random.nextInt((int)filesize);
|
||||
File file = File.createTempFile("Blah", null);
|
||||
file.deleteOnExit();
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
|
||||
raf.setLength(filesize);
|
||||
FileChannel fc = raf.getChannel();
|
||||
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, cut, filesize-cut);
|
||||
mbb.force();
|
||||
}
|
||||
|
||||
// improve chance that mapped buffer will be unmapped
|
||||
System.gc();
|
||||
Thread.sleep(500);
|
||||
}
|
||||
|
||||
private static void test2() throws Exception {
|
||||
var path = Files.createTempFile("test", "map");
|
||||
var channel = FileChannel.open(path, READ, WRITE);
|
||||
MappedByteBuffer buffer =
|
||||
channel.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
|
||||
buffer.putInt(1234);
|
||||
buffer.limit(4);
|
||||
buffer.force();
|
||||
}
|
||||
}
|
||||
75
test/jdk/java/nio/MappedByteBuffer/ForceException.java
Normal file
75
test/jdk/java/nio/MappedByteBuffer/ForceException.java
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 6539707
|
||||
* @summary Test behavior of force() with respect to throwing exceptions
|
||||
* @run main ForceException
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class ForceException {
|
||||
public static void main(String[] args) throws IOException {
|
||||
int blockSize = 2048 * 1024;
|
||||
int numberOfBlocks = 200;
|
||||
int fileLength = numberOfBlocks * blockSize;
|
||||
|
||||
File file = new File(".", "test.dat");
|
||||
file.deleteOnExit();
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
|
||||
raf.setLength(fileLength);
|
||||
|
||||
int pos = (numberOfBlocks - 1) * blockSize;
|
||||
int size = (int)Math.min(blockSize, fileLength - pos);
|
||||
MappedByteBuffer mbb =
|
||||
raf.getChannel().map(FileChannel.MapMode.READ_WRITE, pos, size);
|
||||
|
||||
System.out.printf("Write region 0x%s..0x%s%n",
|
||||
Long.toHexString(pos), Long.toHexString(size));
|
||||
for (int k = 0; k < mbb.limit(); k++) {
|
||||
mbb.put(k, (byte)65);
|
||||
}
|
||||
|
||||
// Catch and process UncheckedIOException; other Throwables fail
|
||||
try {
|
||||
System.out.println("Force");
|
||||
mbb.force();
|
||||
} catch (UncheckedIOException legal) {
|
||||
System.out.printf("Caught legal exception %s%n", legal);
|
||||
IOException cause = legal.getCause(); // can't be null
|
||||
// Throw the cause if flush failed (should be only on Windows)
|
||||
if (cause.getMessage().startsWith("Flush failed")) {
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
106
test/jdk/java/nio/MappedByteBuffer/ForceViews.java
Normal file
106
test/jdk/java/nio/MappedByteBuffer/ForceViews.java
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2026, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4833719
|
||||
* @summary Verify MappedByteBuffer force on compact, duplicate, and slice views
|
||||
* @run junit ForceViews
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.nio.channels.FileChannel.MapMode.*;
|
||||
import static java.nio.file.StandardOpenOption.*;
|
||||
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ForceViews {
|
||||
|
||||
static record Segment(int position, int length) {}
|
||||
|
||||
public static Stream<Arguments> provider() throws IOException {
|
||||
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> absSlice =
|
||||
(m, s) -> { return m.slice(s.position, s.length); };
|
||||
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> relSlice =
|
||||
(m, s) -> { m.position(s.position); m.limit(s.position + s.length);
|
||||
return m.slice(); };
|
||||
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> duplicate=
|
||||
(m, s) -> { return m.duplicate(); };
|
||||
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> compact =
|
||||
(m, s) -> { return m.compact(); };
|
||||
|
||||
return Stream.of
|
||||
(Arguments.of("Absolute slice", 256, 512, 128, 128, 32, 32, absSlice),
|
||||
Arguments.of("Relative slice", 256, 512, 0, 128, 32, 32, relSlice),
|
||||
Arguments.of("Duplicate", 256, 512, 0, 256, 32, 32, duplicate),
|
||||
Arguments.of("Compact", 256, 512, 0, 256, 32, 32, compact));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provider")
|
||||
public void test(String tst, int mapPosition, int mapLength,
|
||||
int sliceIndex, int sliceLength, int regionOffset,
|
||||
int regionLength,
|
||||
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> f)
|
||||
throws Exception {
|
||||
|
||||
Path file = Path.of(".", "junk");
|
||||
try (FileChannel fc = FileChannel.open(file, CREATE, READ, WRITE, DELETE_ON_CLOSE)) {
|
||||
fc.write(ByteBuffer.wrap(new byte[1024]));
|
||||
fc.position(0);
|
||||
|
||||
MappedByteBuffer mbb = fc.map(READ_WRITE, mapPosition, mapLength);
|
||||
mbb = f.apply(mbb, new Segment(sliceIndex, sliceLength));
|
||||
for (int i = regionOffset; i < regionOffset + regionLength; i++) {
|
||||
mbb.put(i, (byte)i);
|
||||
}
|
||||
mbb.force(regionOffset, regionOffset + regionLength);
|
||||
|
||||
int fcPos = mapPosition + sliceIndex + regionOffset;
|
||||
int mbbPos = regionOffset;
|
||||
int length = regionLength;
|
||||
|
||||
ByteBuffer buf = ByteBuffer.allocate(length);
|
||||
fc.position(fcPos);
|
||||
fc.read(buf);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int fcVal = buf.get(i);
|
||||
int mbbVal = mbb.get(mbbPos + i);
|
||||
int val = regionOffset + i;
|
||||
assertEquals(val, fcVal);
|
||||
assertEquals(val, mbbVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
81
test/jdk/java/nio/MappedByteBuffer/MapSyncFail.java
Normal file
81
test/jdk/java/nio/MappedByteBuffer/MapSyncFail.java
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @summary Test failure paths for MAP_SYNC FileChannel.map of non-DAX files
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @run main MapSyncFail true
|
||||
* @run main MapSyncFail false
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.util.*;
|
||||
import java.nio.channels.*;
|
||||
import jdk.nio.mapmode.*;
|
||||
import jdk.internal.misc.Unsafe;
|
||||
|
||||
public class MapSyncFail {
|
||||
|
||||
public static final int K = 1024;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length != 1) {
|
||||
throw new Exception("Expected true or false as argument");
|
||||
}
|
||||
boolean is_rw = Boolean.valueOf(args[0]);
|
||||
FileChannel.MapMode mode = (is_rw ? ExtendedMapMode.READ_WRITE_SYNC : ExtendedMapMode.READ_ONLY_SYNC);
|
||||
// it is assumed that /tmp is not a DAX file system
|
||||
File file = File.createTempFile("MapSyncFail", null);
|
||||
file.deleteOnExit();
|
||||
long filesize = (8 * K);
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
|
||||
raf.setLength(filesize);
|
||||
FileChannel fc = raf.getChannel();
|
||||
MappedByteBuffer mbb = fc.map(mode, 0, filesize);
|
||||
} catch(IOException ioe) {
|
||||
// when writeback is enabled for the current os/cpu
|
||||
// combination the underlying mmap should be attempted and
|
||||
// the map call should fail with IOException
|
||||
if (!Unsafe.isWritebackEnabled()) {
|
||||
throw new Exception("IOException not expected");
|
||||
}
|
||||
System.out.println("caught " + ioe);
|
||||
ioe.printStackTrace();
|
||||
return;
|
||||
} catch (UnsupportedOperationException uoe) {
|
||||
// when writeback is not enabled for the current os/cpu
|
||||
// combination the mmap should not be attempted and the
|
||||
// map call should fail with UnsupportedOperationException
|
||||
|
||||
if (Unsafe.isWritebackEnabled()) {
|
||||
throw new Exception("UnsupportedOperationException not expected");
|
||||
}
|
||||
System.out.println("caught " + uoe);
|
||||
uoe.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Exception("expected IOException or UnsupportedOperationException");
|
||||
}
|
||||
}
|
||||
175
test/jdk/java/nio/MappedByteBuffer/PmemTest.java
Normal file
175
test/jdk/java/nio/MappedByteBuffer/PmemTest.java
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This test is manually run because it requires an NVRAM device to be
|
||||
* mapped as DAX file system or, at least, to be simulated by a
|
||||
* volatile RAM mapped file system. Also, on AArch64 it requires an
|
||||
* ARMV8.2 CPU which implements the dc CVAP instruction (CPU feature
|
||||
* dcpop) and an OS that makes it available from user space.
|
||||
*
|
||||
* If the test runs on such a host without throwing an exception then
|
||||
* that confirms that NVRAM-backed byte buffers can be allocated,
|
||||
* updated and forced via cache line writeback.
|
||||
*/
|
||||
|
||||
/*
|
||||
* How to run this test:
|
||||
*
|
||||
* Ideally this test should be run on a x86_64/amd64 or aarch64 host
|
||||
* fitted with an NVRAM memory device. The NVRAM should appear as
|
||||
* /dev/pmem0 or some equivalent DAX file device. The file device
|
||||
* should be mounted at /mnt/pmem with a directory tmp created
|
||||
* directly under that mount point with a+rwx access.
|
||||
*
|
||||
* It is possible to run the test on x86_64 using a volatile RAM
|
||||
* backed device to simulate NVRAM, even though this does not provide
|
||||
* any guarantee of persistence of data across program runs. For the
|
||||
* latter case the following instructions explain how to set up the
|
||||
* simulated NVRAM device.
|
||||
*
|
||||
* https://developers.redhat.com/blog/2016/12/05/configuring-and-using-persistent-memory-rhel-7-3/
|
||||
* https://nvdimm.wiki.kernel.org/
|
||||
* TL;DR: add "memmap=1G!4G" to /etc/default/grub, eg. GRUB_CMDLINE_LINUX="memmap=1G!4G"
|
||||
* then ("sudo" may required)
|
||||
* for RHEL(BIOS-based): grub2-mkconfig -o /boot/grub2/grub.cfg
|
||||
* for RHEL(UEFI-based): grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
|
||||
* for Ubuntu: update-grub2
|
||||
* finally reboot
|
||||
* after the host been rebooted, a new /dev/pmem{N} device should exist,
|
||||
* naming conversion starts at /dev/pmem0
|
||||
*
|
||||
* Prepare test directory follow below commands, "sudo" may required
|
||||
* (if ndctl or mkfs.xfs not exist, install ndctl or xfsprogs package first)
|
||||
* (for RHEL8, when call mkfs.xfs, specify the -m reflink=0 option to disable reflink feature)
|
||||
*
|
||||
* ndctl create-namespace -f -e namespace0.0 -m memory -M mem
|
||||
* mkdir /mnt/pmem
|
||||
* mkfs.xfs -f /dev/pmem0; mount -o dax /dev/pmem0 /mnt/pmem/
|
||||
* mkdir /mnt/pmem/test; chmod a+rwx /mnt/pmem/test
|
||||
*
|
||||
* Now run the test program
|
||||
*
|
||||
* java PmemTest
|
||||
*
|
||||
* or
|
||||
*
|
||||
* make test TEST=jdk/java/nio/MappedByteBuffer/PmemTest.java
|
||||
*/
|
||||
|
||||
/* @test id=default_architecture_test_case
|
||||
* @summary Testing NVRAM mapped byte buffer support
|
||||
* @run main/manual PmemTest
|
||||
* @requires (os.family == "linux")
|
||||
* @requires ((os.arch == "x86_64")|(os.arch == "amd64"))
|
||||
*/
|
||||
|
||||
/* @test id=other_architectures_test_case
|
||||
* @summary Testing NVRAM mapped byte buffer support
|
||||
* @run main/manual PmemTest
|
||||
* @requires (os.family == "linux")
|
||||
* @requires ((os.arch == "aarch64")|(os.arch == "ppc64le"))
|
||||
* @ignore The test described here is currently disabled on systems that are not
|
||||
* x64-based and lack an external NVRAM memory device. In order to re-enable the
|
||||
* test, you will need to mount the NVRAM device, which will typically appear as
|
||||
* /dev/pmem0, to the directory /mnt/pmem. Once that is done, you can follow the
|
||||
* instructions above to create a test directory and remove the ignore tag.
|
||||
*/
|
||||
import java.io.File;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import jdk.nio.mapmode.ExtendedMapMode;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.BufferPoolMXBean;
|
||||
|
||||
public class PmemTest {
|
||||
|
||||
public static final int K = 1024;
|
||||
public static final int NUM_KBS = 16;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
System.out.println("test");
|
||||
|
||||
String dir = "/tmp"; // mapSync should fail
|
||||
dir = "/mnt/pmem/test"; // mapSync should work, since fs mount is -o dax
|
||||
|
||||
Path path = new File(dir, "pmemtest").toPath();
|
||||
|
||||
FileChannel fileChannel = (FileChannel) Files
|
||||
.newByteChannel(path, EnumSet.of(
|
||||
StandardOpenOption.READ,
|
||||
StandardOpenOption.WRITE,
|
||||
StandardOpenOption.CREATE));
|
||||
|
||||
MappedByteBuffer mappedByteBuffer = fileChannel.map(ExtendedMapMode.READ_WRITE_SYNC, 0, NUM_KBS * K);
|
||||
|
||||
|
||||
dumpBufferPoolBeans();
|
||||
|
||||
// for (int loops = 0; loops < 1000; loops++) {
|
||||
for (int loops = 0; loops < 100; loops++) {
|
||||
int base = K * (loops % NUM_KBS);
|
||||
for (int i = 0; i < K ; i++) {
|
||||
for (int j = 0; j < K ;j++) {
|
||||
testBuffer(mappedByteBuffer, base, (i << 3) + j);
|
||||
commitBuffer(mappedByteBuffer, base);
|
||||
}
|
||||
}
|
||||
}
|
||||
dumpBufferPoolBeans();
|
||||
}
|
||||
|
||||
public static void testBuffer(MappedByteBuffer mappedByteBuffer, int base, int start) {
|
||||
for (int k = 0; k < 8; k++) {
|
||||
int idx = (start + k) % K;
|
||||
byte z = mappedByteBuffer.get(base + idx);
|
||||
z++;
|
||||
mappedByteBuffer.put(base + idx, z);
|
||||
}
|
||||
}
|
||||
|
||||
public static void commitBuffer(MappedByteBuffer mappedByteBuffer, int base)
|
||||
{
|
||||
mappedByteBuffer.force(base, K);
|
||||
}
|
||||
|
||||
public static void dumpBufferPoolBeans()
|
||||
{
|
||||
List<BufferPoolMXBean> beansList = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
|
||||
for (BufferPoolMXBean bean : beansList) {
|
||||
System.out.println("BufferPoolMXBean {" +
|
||||
"\n\tname: " + bean.getName() +
|
||||
"\n\tcount: " + bean.getCount() +
|
||||
"\n\ttotalCapacity: " + bean.getTotalCapacity() +
|
||||
"\n\tmemoryUsed: " + bean.getMemoryUsed() +
|
||||
"\n}");
|
||||
}
|
||||
}
|
||||
}
|
||||
52
test/jdk/java/nio/MappedByteBuffer/SubclassCastUOE.java
Normal file
52
test/jdk/java/nio/MappedByteBuffer/SubclassCastUOE.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 8191416
|
||||
* @summary Verify no UnsupportedOperationException for cast subclass
|
||||
*/
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.MappedByteBuffer;
|
||||
|
||||
public class SubclassCastUOE {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ByteBuffer buf = ByteBuffer.allocateDirect(37);
|
||||
|
||||
if (!(buf instanceof MappedByteBuffer)) {
|
||||
throw new RuntimeException("Direct buffer not a MappedByteBuffer");
|
||||
}
|
||||
|
||||
if (((MappedByteBuffer)buf).load() != buf) {
|
||||
throw new RuntimeException("load() did not return same buffer");
|
||||
}
|
||||
|
||||
if (!((MappedByteBuffer)buf).isLoaded()) {
|
||||
throw new RuntimeException("isLoaded() returned false");
|
||||
}
|
||||
|
||||
if (((MappedByteBuffer)buf).force() != buf) {
|
||||
throw new RuntimeException("force() did not return same buffer");
|
||||
}
|
||||
}
|
||||
}
|
||||
99
test/jdk/java/nio/MappedByteBuffer/Truncate.java
Normal file
99
test/jdk/java/nio/MappedByteBuffer/Truncate.java
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 6934977
|
||||
* @summary Test MappedByteBuffer operations after mapped bye buffer becomes
|
||||
* inaccessible
|
||||
* @run main/othervm Truncate
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.nio.channels.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class Truncate {
|
||||
|
||||
static final long INITIAL_FILE_SIZE = 32000L;
|
||||
static final long TRUNCATED_FILE_SIZE = 512L;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File blah = File.createTempFile("blah", null);
|
||||
blah.deleteOnExit();
|
||||
|
||||
final FileChannel fc = new RandomAccessFile(blah, "rw").getChannel();
|
||||
fc.position(INITIAL_FILE_SIZE).write(ByteBuffer.wrap("THE END".getBytes()));
|
||||
final MappedByteBuffer mbb =
|
||||
fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
|
||||
boolean truncated;
|
||||
try {
|
||||
fc.truncate(TRUNCATED_FILE_SIZE);
|
||||
truncated = true;
|
||||
} catch (IOException ioe) {
|
||||
// probably on Windows where a file cannot be truncated when
|
||||
// there is a file mapping.
|
||||
truncated = false;
|
||||
}
|
||||
if (truncated) {
|
||||
// Test 1: access region that is no longer accessible
|
||||
execute(new Callable<Void>() {
|
||||
public Void call() {
|
||||
mbb.get((int)TRUNCATED_FILE_SIZE + 1);
|
||||
mbb.put((int)TRUNCATED_FILE_SIZE + 2, (byte)123);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
// Test 2: load buffer into memory
|
||||
execute(new Callable<Void>() {
|
||||
public Void call() throws IOException {
|
||||
mbb.load();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
fc.close();
|
||||
}
|
||||
|
||||
// Runs the given task in its own thread. If operating correcting the
|
||||
// the thread will terminate with an InternalError as the mapped buffer
|
||||
// is inaccessible.
|
||||
static void execute(final Callable<?> c) {
|
||||
Runnable r = new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Object ignore = c.call();
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
};
|
||||
Thread t = new Thread(r);
|
||||
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
try { t.join(); } catch (InterruptedException ignore) { }
|
||||
}
|
||||
}
|
||||
56
test/jdk/java/nio/MappedByteBuffer/ZeroMap.java
Normal file
56
test/jdk/java/nio/MappedByteBuffer/ZeroMap.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4802340
|
||||
* @summary Testing force(), load() isLoaded() of zero len MBB
|
||||
* @run main/othervm ZeroMap
|
||||
* @key randomness
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.*;
|
||||
import java.util.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
public class ZeroMap {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Random random = new Random();
|
||||
long filesize = random.nextInt(1024*1024);
|
||||
int cut = random.nextInt((int)filesize);
|
||||
File file = File.createTempFile("Blah", null);
|
||||
file.deleteOnExit();
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
|
||||
raf.setLength(filesize);
|
||||
FileChannel fc = raf.getChannel();
|
||||
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, cut, 0);
|
||||
mbb.force();
|
||||
mbb.load();
|
||||
mbb.isLoaded();
|
||||
}
|
||||
|
||||
// improve chance that mapped buffer will be unmapped
|
||||
System.gc();
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue