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:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,650 @@
/*
* Copyright (c) 2008, 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.
*/
/* @test
* @bug 4607272 5041655 6822643 6830721 6842687
* @summary Unit test for AsynchronousFileChannel
* @key randomness
* @run main/othervm Basic
*/
import java.io.File;
import java.io.IOException;
import java.lang.foreign.Arena;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.CompletionHandler;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static java.nio.file.StandardOpenOption.*;
public class Basic {
// Must be indeterministic
private static final Random rand = new Random();
public static void main(String[] args) throws IOException {
// create temporary file
File blah = File.createTempFile("blah", null);
blah.deleteOnExit();
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(blah.toPath(), READ, WRITE);
try {
// run tests
testUsingCompletionHandlers(ch);
testUsingWaitOnResult(ch);
testInterruptHandlerThread(ch);
} finally {
ch.close();
}
// run test that expects channel to be closed
testClosedChannel(ch);
// these tests open the file themselves
testLocking(blah.toPath());
testCustomThreadPool(blah.toPath());
testAsynchronousClose(blah.toPath());
testCancel(blah.toPath());
testTruncate(blah.toPath());
// eagerly clean-up
blah.delete();
}
/*
* Generate buffer with random contents
* Writes buffer to file using a CompletionHandler to consume the result
* of each write operation
* Reads file to EOF to a new buffer using a CompletionHandler to consume
* the result of each read operation
* Compares buffer contents
*/
static void testUsingCompletionHandlers(AsynchronousFileChannel ch)
throws IOException
{
System.out.println("testUsingCompletionHandlers");
ch.truncate(0L);
// generate buffer with random elements and write it to file
ByteBuffer src = genBuffer();
writeFully(ch, src, 0L);
// read to EOF or buffer is full
ByteBuffer dst = (rand.nextBoolean()) ?
ByteBuffer.allocateDirect(src.capacity()) :
ByteBuffer.allocate(src.capacity());
readAll(ch, dst, 0L);
// check buffers are the same
src.flip();
dst.flip();
if (!src.equals(dst)) {
throw new RuntimeException("Contents differ");
}
}
/*
* Generate buffer with random contents
* Writes buffer to file, invoking the Future's get method to wait for
* each write operation to complete
* Reads file to EOF to a new buffer, invoking the Future's get method to
* wait for each write operation to complete
* Compares buffer contents
*/
static void testUsingWaitOnResult(AsynchronousFileChannel ch)
throws IOException
{
System.out.println("testUsingWaitOnResult");
ch.truncate(0L);
// generate buffer
ByteBuffer src = genBuffer();
// write buffer completely to file
long position = 0L;
while (src.hasRemaining()) {
Future<Integer> result = ch.write(src, position);
try {
int n = result.get();
// update position
position += n;
} catch (ExecutionException x) {
throw new RuntimeException(x.getCause());
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
}
// read file into new buffer
ByteBuffer dst = (rand.nextBoolean()) ?
ByteBuffer.allocateDirect(src.capacity()) :
ByteBuffer.allocate(src.capacity());
position = 0L;
int n;
do {
Future<Integer> result = ch.read(dst, position);
try {
n = result.get();
// update position
if (n > 0) position += n;
} catch (ExecutionException x) {
throw new RuntimeException(x.getCause());
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
} while (n > 0);
// check buffers are the same
src.flip();
dst.flip();
if (!src.equals(dst)) {
throw new RuntimeException("Contents differ");
}
}
// exercise lock methods
static void testLocking(Path file) throws IOException {
System.out.println("testLocking");
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file, READ, WRITE);
FileLock fl;
try {
// test 1 - acquire lock and check that tryLock throws
// OverlappingFileLockException
try {
long pos = rand.nextInt(Integer.MAX_VALUE);
fl = ch.lock(pos, 0, false).get();
long expectedSize = Long.MAX_VALUE - pos;
if(fl.size() != expectedSize)
throw new RuntimeException("Lock size " + fl.size() +
" != " + expectedSize + " for position " + pos);
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException("Should not be interrupted");
}
if (!fl.acquiredBy().equals(ch))
throw new RuntimeException("FileLock#acquiredBy returned incorrect channel");
try {
ch.tryLock();
throw new RuntimeException("OverlappingFileLockException expected");
} catch (OverlappingFileLockException x) {
}
fl.release();
// test 2 - acquire try and check that lock throws OverlappingFileLockException
long pos = rand.nextInt(Integer.MAX_VALUE);
fl = ch.tryLock(pos, 0, false);
long expectedSize = Long.MAX_VALUE - pos;
if(fl.size() != expectedSize)
throw new RuntimeException("Lock size " + fl.size() + " != " +
expectedSize + " for position " + pos);
if (fl == null)
throw new RuntimeException("Unable to acquire lock");
try {
ch.lock((Void)null, new CompletionHandler<FileLock,Void> () {
public void completed(FileLock result, Void att) {
}
public void failed(Throwable exc, Void att) {
}
});
throw new RuntimeException("OverlappingFileLockException expected");
} catch (OverlappingFileLockException x) {
}
} finally {
ch.close();
}
// test 3 - channel is closed so FileLock should no longer be valid
if (fl.isValid())
throw new RuntimeException("FileLock expected to be invalid");
}
// interrupt should not close channel
static void testInterruptHandlerThread(final AsynchronousFileChannel ch) {
System.out.println("testInterruptHandlerThread");
ByteBuffer buf = ByteBuffer.allocateDirect(100);
final CountDownLatch latch = new CountDownLatch(1);
ch.read(buf, 0L, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer result, Void att) {
try {
Thread.currentThread().interrupt();
long size = ch.size();
latch.countDown();
} catch (IOException x) {
x.printStackTrace();
}
}
public void failed(Throwable exc, Void att) {
}
});
// wait for handler to complete
await(latch);
}
// invoke method on closed channel
static void testClosedChannel(AsynchronousFileChannel ch) {
System.out.println("testClosedChannel");
if (ch.isOpen())
throw new RuntimeException("Channel should be closed");
ByteBuffer buf = ByteBuffer.allocateDirect(100);
// check read fails with ClosedChannelException
try {
ch.read(buf, 0L).get();
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
// check write fails with ClosedChannelException
try {
ch.write(buf, 0L).get();
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
// check lock fails with ClosedChannelException
try {
ch.lock().get();
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
}
// exercise custom thread pool
static void testCustomThreadPool(Path file) throws IOException {
System.out.println("testCustomThreadPool");
// records threads that are created
final List<Thread> threads = new ArrayList<Thread>();
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
synchronized (threads) {
threads.add(t);
}
return t;
}
};
// exercise tests with varied number of threads
for (int nThreads=1; nThreads<=5; nThreads++) {
synchronized (threads) {
threads.clear();
}
ExecutorService executor = Executors.newFixedThreadPool(nThreads, threadFactory);
Set<StandardOpenOption> opts = EnumSet.of(WRITE);
AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, opts, executor);
try {
for (int i=0; i<10; i++) {
// do I/O operation to see which thread invokes the completion handler
final AtomicReference<Thread> invoker = new AtomicReference<Thread>();
final CountDownLatch latch = new CountDownLatch(1);
ch.write(genBuffer(), 0L, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer result, Void att) {
invoker.set(Thread.currentThread());
latch.countDown();
}
public void failed(Throwable exc, Void att) {
}
});
await(latch);
// check invoker
boolean found = false;
synchronized (threads) {
for (Thread t: threads) {
if (t == invoker.get()) {
found = true;
break;
}
}
}
if (!found)
throw new RuntimeException("Invoker thread not found");
}
} finally {
ch.close();
executor.shutdown();
}
}
// test sharing a thread pool between many channels
ExecutorService executor = Executors
.newFixedThreadPool(1+rand.nextInt(10), threadFactory);
final int n = 50 + rand.nextInt(50);
AsynchronousFileChannel[] channels = new AsynchronousFileChannel[n];
try {
for (int i=0; i<n; i++) {
Set<StandardOpenOption> opts = EnumSet.of(WRITE);
channels[i] = AsynchronousFileChannel.open(file, opts, executor);
final CountDownLatch latch = new CountDownLatch(1);
channels[i].write(genBuffer(), 0L, (Void)null, new CompletionHandler<Integer,Void>() {
public void completed(Integer result, Void att) {
latch.countDown();
}
public void failed(Throwable exc, Void att) {
}
});
await(latch);
// close ~half the channels
if (rand.nextBoolean())
channels[i].close();
}
} finally {
// close remaining channels
for (int i=0; i<n; i++) {
if (channels[i] != null) channels[i].close();
}
executor.shutdown();
}
}
// exercise asynchronous close
static void testAsynchronousClose(Path file) throws IOException {
System.out.println("testAsynchronousClose");
// create file
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file, WRITE, TRUNCATE_EXISTING);
long size = 0L;
do {
ByteBuffer buf = genBuffer();
int n = buf.remaining();
writeFully(ch, buf, size);
size += n;
} while (size < (50L * 1024L * 1024L));
ch.close();
ch = AsynchronousFileChannel.open(file, WRITE, SYNC);
// randomize number of writers, buffer size, and positions
int nwriters = 1 + rand.nextInt(8);
ByteBuffer[] buf = new ByteBuffer[nwriters];
long[] position = new long[nwriters];
for (int i=0; i<nwriters; i++) {
buf[i] = genBuffer();
position[i] = rand.nextInt((int)size);
}
// initiate I/O
Future[] result = new Future[nwriters];
for (int i=0; i<nwriters; i++) {
result[i] = ch.write(buf[i], position[i]);
}
// close file
ch.close();
// write operations should complete or fail with AsynchronousCloseException
for (int i=0; i<nwriters; i++) {
try {
result[i].get();
} catch (ExecutionException x) {
Throwable cause = x.getCause();
if (!(cause instanceof AsynchronousCloseException))
throw new RuntimeException(cause);
} catch (CancellationException x) {
throw new RuntimeException(x); // should not happen
} catch (InterruptedException x) {
throw new RuntimeException(x); // should not happen
}
}
}
// exercise cancel method
static void testCancel(Path file) throws IOException {
System.out.println("testCancel");
for (int i=0; i<2; i++) {
boolean mayInterruptIfRunning = (i == 0) ? false : true;
// open with SYNC option to improve chances that write will not
// complete immediately
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file, WRITE, SYNC);
// start write operation
Future<Integer> res = ch.write(genBuffer(), 0L);
// cancel operation
boolean cancelled = res.cancel(mayInterruptIfRunning);
// check post-conditions
if (!res.isDone())
throw new RuntimeException("isDone should return true");
if (res.isCancelled() != cancelled)
throw new RuntimeException("isCancelled not consistent");
try {
res.get();
if (cancelled)
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
if (!cancelled)
throw new RuntimeException("CancellationException not expected");
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
try {
res.get(1, TimeUnit.SECONDS);
if (cancelled)
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
if (!cancelled)
throw new RuntimeException("CancellationException not expected");
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (TimeoutException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
ch.close();
}
}
// exercise truncate method
static void testTruncate(Path file) throws IOException {
System.out.println("testTruncate");
// basic tests
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file, CREATE, WRITE, TRUNCATE_EXISTING);
try {
writeFully(ch, genBuffer(), 0L);
long size = ch.size();
// attempt to truncate to a size greater than the current size
if (ch.truncate(size + 1L).size() != size)
throw new RuntimeException("Unexpected size after truncation");
// truncate file
if (ch.truncate(size - 1L).size() != (size - 1L))
throw new RuntimeException("Unexpected size after truncation");
// invalid size
try {
ch.truncate(-1L);
throw new RuntimeException("IllegalArgumentException expected");
} catch (IllegalArgumentException e) { }
} finally {
ch.close();
}
// channel is closed
try {
ch.truncate(0L);
throw new RuntimeException("ClosedChannelException expected");
} catch (ClosedChannelException e) { }
// channel is read-only
ch = AsynchronousFileChannel.open(file, READ);
try {
try {
ch.truncate(0L);
throw new RuntimeException("NonWritableChannelException expected");
} catch (NonWritableChannelException e) { }
} finally {
ch.close();
}
}
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
rand.nextBytes(buf);
return switch (rand.nextInt(5)) {
case 0 -> ByteBuffer.allocateDirect(buf.length)
.put(buf)
.flip();
case 1 -> ByteBuffer.wrap(buf);
case 2 -> Arena.global().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 3 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 4 -> Arena.ofShared().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
default -> throw new InternalError("Should not reach here");
};
}
// writes all remaining bytes in the buffer to the given channel at the
// given position
static void writeFully(final AsynchronousFileChannel ch,
final ByteBuffer src,
long position)
{
final CountDownLatch latch = new CountDownLatch(1);
// use position as attachment
ch.write(src, position, position, new CompletionHandler<Integer,Long>() {
public void completed(Integer result, Long position) {
int n = result;
if (src.hasRemaining()) {
long p = position + n;
ch.write(src, p, p, this);
} else {
latch.countDown();
}
}
public void failed(Throwable exc, Long position) {
}
});
// wait for writes to complete
await(latch);
}
static void readAll(final AsynchronousFileChannel ch,
final ByteBuffer dst,
long position)
{
final CountDownLatch latch = new CountDownLatch(1);
// use position as attachment
ch.read(dst, position, position, new CompletionHandler<Integer,Long>() {
public void completed(Integer result, Long position) {
int n = result;
if (n > 0) {
long p = position + n;
ch.read(dst, p, p, this);
} else {
latch.countDown();
}
}
public void failed(Throwable exc, Long position) {
}
});
// wait for reads to complete
await(latch);
}
static void await(CountDownLatch latch) {
// wait until done
boolean done = false;
while (!done) {
try {
latch.await();
done = true;
} catch (InterruptedException x) { }
}
}
}

View file

@ -0,0 +1,189 @@
/*
* Copyright (c) 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.
*/
/*
* @test
* @bug 8374382
* @summary Test AsynchronousFileChannel.read/write with ByteBuffers and varied buffer positions
* @run junit/othervm BufferPositions
*/
import java.lang.foreign.Arena;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import static java.nio.file.StandardOpenOption.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.*;
class BufferPositions {
private static final int BUF_SIZE = 32;
/**
* The buffers to test.
*/
static Stream<ByteBuffer> buffers() {
List<ByteBuffer> buffers = List.of(
ByteBuffer.allocate(BUF_SIZE),
ByteBuffer.allocateDirect(BUF_SIZE),
ByteBuffer.wrap(new byte[BUF_SIZE]),
Arena.global().allocate(BUF_SIZE).asByteBuffer(),
Arena.ofAuto().allocate(BUF_SIZE).asByteBuffer(),
Arena.ofShared().allocate(BUF_SIZE).asByteBuffer()
);
Stream<ByteBuffer> slices = buffers.stream()
.map(bb -> bb.slice(1, bb.capacity() - 1));
return Stream.concat(buffers.stream(), slices);
}
/**
* Test using an AsynchronousFileChannel to read bytes into the given buffer.
*/
@ParameterizedTest
@MethodSource("buffers")
void testRead(ByteBuffer bb) throws Exception {
Path file = Files.createTempFile(Path.of("."), "test", "dat");
// populate temp file
int size = bb.capacity();
byte[] contents = new byte[size];
for (int i = 0; i < size; i++) {
contents[i] = (byte)(i + 1);
}
Files.write(file, contents);
// read bytes using the buffer as the destination and all possible buffer positions
try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ)) {
for (int filePosition = 0; filePosition < size; filePosition++) {
for (int bufPos = 0; bufPos < size; bufPos++) {
// read one byte
bb.position(bufPos);
bb.limit(bufPos + 1);
int n = read(ch, filePosition, bb);
assertEquals(1, n);
assertEquals(bufPos + 1, bb.position());
assertEquals(contents[filePosition], bb.get(bufPos));
}
}
}
}
/**
* Test using an AsynchronousFileChannel to write bytes from the given buffer.
*/
@ParameterizedTest
@MethodSource("buffers")
void testWrite(ByteBuffer bb) throws Exception {
Path file = Files.createTempFile(Path.of("."), "test", "dat");
// populate temp file, all zeros
int size = bb.capacity();
byte[] contents = new byte[size];
Files.write(file, contents);
// write bytes using the buffer as the source and all possible buffer positions
try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ, WRITE)) {
for (int filePosition = 0; filePosition < size; filePosition++) {
for (int bufPos = 0; bufPos < size; bufPos++) {
// write one byte
byte b = (byte) ThreadLocalRandom.current().nextInt();
bb.position(bufPos);
bb.limit(bufPos + 1);
bb.put(bufPos, b);
int n = write(ch, filePosition, bb);
assertEquals(1, n);
assertEquals(bufPos + 1, bb.position());
assertEquals(b, bb.get(bufPos));
// check byte was written at the expected file position
ByteBuffer dst = ByteBuffer.allocate(1);
int nread = ch.read(dst, filePosition).get();
assertEquals(1, nread);
assertEquals(b, dst.get(0));
}
}
}
}
/**
* Reads a byte from a channel into the given buffer, at the given file position.
*/
private int read(AsynchronousFileChannel ch, long position, ByteBuffer bb) throws Exception {
if (ThreadLocalRandom.current().nextBoolean()) {
return ch.read(bb, position).get();
} else {
var handler = new Handler<Integer>();
ch.read(bb, position, null, handler);
return handler.result();
}
}
/**
* Writes a byte to a channel from the given buffer, at the given file position.
*/
private int write(AsynchronousFileChannel ch, long position, ByteBuffer bb) throws Exception {
if (ThreadLocalRandom.current().nextBoolean()) {
return ch.write(bb, position).get();
} else {
var handler = new Handler<Integer>();
ch.write(bb, position, null, handler);
return handler.result();
}
}
/**
* CompletionHandler that defines a method to await the result of an I/O operation.
*/
private static class Handler<T> implements CompletionHandler<T, Void> {
T result;
Throwable ex;
final CountDownLatch done = new CountDownLatch(1);
@Override
public void completed(T result, Void att) {
this.result = result;
done.countDown();
}
@Override
public void failed(Throwable ex, Void att) {
this.ex = ex;
done.countDown();
}
T result() throws InterruptedException {
done.await();
assertNull(ex);
return result;
}
}
}

View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2008, 2009, 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 4607272 6842687
* @summary Unit test for java.nio.channels.AsynchronousFileChannel
* @build CustomThreadPool MyThreadFactory
* @run main/othervm -Djava.nio.channels.DefaultThreadPool.threadFactory=MyThreadFactory CustomThreadPool
*/
import java.io.File;
import static java.nio.file.StandardOpenOption.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.concurrent.atomic.AtomicReference;
public class CustomThreadPool {
public static void main(String[] args) throws Exception {
File blah = File.createTempFile("blah", null);
blah.deleteOnExit();
AsynchronousFileChannel ch =
AsynchronousFileChannel.open(blah.toPath(), READ, WRITE);
ByteBuffer src = ByteBuffer.wrap("Scooby Snacks".getBytes());
final AtomicReference<Thread> invoker = new AtomicReference<Thread>();
ch.write(src, 0, invoker,
new CompletionHandler<Integer,AtomicReference<Thread>>() {
public void completed(Integer result, AtomicReference<Thread> invoker) {
invoker.set(Thread.currentThread());
}
public void failed(Throwable exc, AtomicReference<Thread> invoker) {
}
});
Thread t;
while ((t = invoker.get()) == null) {
Thread.sleep(100);
}
ch.close();
// check handler was run by known thread
if (!MyThreadFactory.created(t))
throw new RuntimeException("Handler invoked by unknown thread");
}
}

View file

@ -0,0 +1,350 @@
/*
* Copyright (c) 2008, 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 4607272 6814948 6842687
* @summary Unit test for AsynchronousFileChannel#lock method
* @key randomness
*/
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.*;
import static java.nio.file.StandardOpenOption.*;
import java.nio.channels.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.concurrent.*;
public class Lock {
static final Random rand = new Random();
public static void main(String[] args) throws Exception {
if (args.length > 0 && args[0].equals("-lockworker")) {
int port = Integer.parseInt(args[1]);
runLockWorker(port);
System.exit(0);
}
LockWorkerMirror worker = startLockWorker();
try {
// create temporary file
File blah = File.createTempFile("blah", null);
blah.deleteOnExit();
// run tests
testLockProtocol(blah, worker);
testAsyncClose(blah, worker);
// eagerly clean-up
blah.delete();
} finally {
worker.shutdown();
}
}
// test locking protocol
static void testLockProtocol(File file, LockWorkerMirror worker)
throws Exception
{
FileLock fl;
// worker VM opens file and acquires exclusive lock
worker.open(file.getPath()).lock();
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file.toPath(), READ, WRITE);
// this VM tries to acquire lock
// (lock should not be acquire until released by worker VM)
Future<FileLock> result = ch.lock();
try {
result.get(2, TimeUnit.SECONDS);
throw new RuntimeException("Timeout expected");
} catch (TimeoutException x) {
}
// worker VM releases lock
worker.unlock();
// this VM should now acquire lock
fl = result.get();
fl.release();
// worker VM acquires lock on range
worker.lock(0, 10, false);
// this VM acquires lock on non-overlapping range
fl = ch.lock(10, 10, false).get();
fl.release();
// done
ch.close();
worker.close();
}
// test close of channel with outstanding lock operation
static void testAsyncClose(File file, LockWorkerMirror worker) throws Exception {
// worker VM opens file and acquires exclusive lock
worker.open(file.getPath()).lock();
for (int i=0; i<100; i++) {
AsynchronousFileChannel ch = AsynchronousFileChannel
.open(file.toPath(), READ, WRITE);
// try to lock file (should not complete because file is locked by worker)
Future<FileLock> result = ch.lock();
try {
result.get(rand.nextInt(100), TimeUnit.MILLISECONDS);
throw new RuntimeException("Timeout expected");
} catch (TimeoutException x) {
}
// close channel with lock operation outstanding
ch.close();
// operation should complete with AsynchronousCloseException
try {
result.get();
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof AsynchronousCloseException)) {
x.getCause().printStackTrace();
throw new RuntimeException("AsynchronousCloseException expected");
}
}
}
worker.close();
}
// starts a "lock worker" in another process, returning a mirror object to
// control the worker
static LockWorkerMirror startLockWorker() throws Exception {
ServerSocketChannel ssc = ServerSocketChannel.open()
.bind(new InetSocketAddress(0));
int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
String sep = FileSystems.getDefault().getSeparator();
String command = System.getProperty("java.home") +
sep + "bin" + sep + "java";
String testClasses = System.getProperty("test.classes");
if (testClasses != null)
command += " -cp " + testClasses;
command += " Lock -lockworker " + port;
Process p = Runtime.getRuntime().exec(command);
IOHandler.handle(p.getInputStream());
IOHandler.handle(p.getErrorStream());
// wait for worker to connect
SocketChannel sc = ssc.accept();
return new LockWorkerMirror(sc);
}
// commands that the worker understands
static final String OPEN_CMD = "open";
static final String CLOSE_CMD = "close";
static final String LOCK_CMD = "lock";
static final String UNLOCK_CMD = "unlock";
static final char TERMINATOR = ';';
// provides a proxy to a "lock worker"
static class LockWorkerMirror {
private final SocketChannel sc;
LockWorkerMirror(SocketChannel sc) {
this.sc = sc;
}
private void sendCommand(String cmd, String... params)
throws IOException
{
for (String s: params) {
cmd += " " + s;
}
cmd += TERMINATOR;
ByteBuffer buf = Charset.defaultCharset().encode(cmd);
while (buf.hasRemaining()) {
sc.write(buf);
}
// wait for ack
buf = ByteBuffer.allocate(1);
int n = sc.read(buf);
if (n != 1)
throw new RuntimeException("Reply expected");
if (buf.get(0) != TERMINATOR)
throw new RuntimeException("Terminated expected");
}
LockWorkerMirror open(String file) throws IOException {
sendCommand(OPEN_CMD, file);
return this;
}
void close() throws IOException {
sendCommand(CLOSE_CMD);
}
LockWorkerMirror lock() throws IOException {
sendCommand(LOCK_CMD);
return this;
}
LockWorkerMirror lock(long position, long size, boolean shared)
throws IOException
{
sendCommand(LOCK_CMD, position + "," + size + "," + shared);
return this;
}
LockWorkerMirror unlock() throws IOException {
sendCommand(UNLOCK_CMD);
return this;
}
void shutdown() throws IOException {
sc.close();
}
}
// Helper class to direct process output to the parent System.out
static class IOHandler implements Runnable {
private final InputStream in;
IOHandler(InputStream in) {
this.in = in;
}
static void handle(InputStream in) {
IOHandler handler = new IOHandler(in);
Thread thr = new Thread(handler);
thr.setDaemon(true);
thr.start();
}
public void run() {
try {
byte b[] = new byte[100];
for (;;) {
int n = in.read(b);
if (n < 0) return;
for (int i=0; i<n; i++) {
System.out.print((char)b[i]);
}
}
} catch (IOException ioe) { }
}
}
// worker process that responds to simple commands a socket connection
static void runLockWorker(int port) throws Exception {
// establish connection to parent
SocketChannel sc = SocketChannel.open(new InetSocketAddress(port));
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
FileChannel fc = null;
FileLock fl = null;
try {
for (;;) {
// read command (ends with ";")
buf.clear();
int n, last = 0;
do {
n = sc.read(buf);
if (n < 0)
return;
if (n == 0)
throw new AssertionError();
last += n;
} while (buf.get(last-1) != TERMINATOR);
// decode into command and optional parameter
buf.flip();
String s = Charset.defaultCharset().decode(buf).toString();
int sp = s.indexOf(" ");
String cmd = (sp < 0) ? s.substring(0, s.length()-1) :
s.substring(0, sp);
String param = (sp < 0) ? "" : s.substring(sp+1, s.length()-1);
// execute
if (cmd.equals(OPEN_CMD)) {
if (fc != null)
throw new RuntimeException("File already open");
fc = FileChannel.open(Paths.get(param),READ, WRITE);
}
if (cmd.equals(CLOSE_CMD)) {
if (fc == null)
throw new RuntimeException("No file open");
fc.close();
fc = null;
fl = null;
}
if (cmd.equals(LOCK_CMD)) {
if (fl != null)
throw new RuntimeException("Already holding lock");
if (param.length() == 0) {
fl = fc.lock();
} else {
String[] values = param.split(",");
if (values.length != 3)
throw new RuntimeException("Lock parameter invalid");
long position = Long.parseLong(values[0]);
long size = Long.parseLong(values[1]);
boolean shared = Boolean.parseBoolean(values[2]);
fl = fc.lock(position, size, shared);
}
}
if (cmd.equals(UNLOCK_CMD)) {
if (fl == null)
throw new RuntimeException("Not holding lock");
fl.release();
fl = null;
}
// send reply
byte[] reply = { TERMINATOR };
n = sc.write(ByteBuffer.wrap(reply));
}
} finally {
sc.close();
if (fc != null) fc.close();
}
}
}

View file

@ -0,0 +1,54 @@
/*
* 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
* @bug 8184157
* @summary Ensure that correct PendingFuture is used in Iocp completion status event handler
* @requires (os.family == "windows")
*/
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.*;
public class LockReadWriteStressTest {
public static void main(String[] args) throws Exception {
Path path = Path.of("blah");
ByteBuffer buf = ByteBuffer.allocate(16);
for (int i=0; i < 1000; i++) {
try (AsynchronousFileChannel ch = AsynchronousFileChannel.open(path,READ, WRITE, CREATE)) {
FileLock lock = ch.lock().get();
ch.read(buf, 0).get();
buf.rewind();
ch.write(buf, 0).get();
lock.release();
buf.clear();
}
}
}
}

View file

@ -0,0 +1,181 @@
/*
* Copyright (c) 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 6913877
* @summary Stress AsynchronousFileChannel.write
* @key randomness
*/
import java.io.*;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.nio.channels.*;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class LotsOfWrites {
static final Random rand = new Random();
/**
* Asynchronously writes a known pattern to a file up to a given size,
* counting down a latch to release waiters when done.
*/
static class Writer implements CompletionHandler<Integer,ByteBuffer> {
private final File file;
private final long size;
private final CountDownLatch latch;
private final AsynchronousFileChannel channel;
private volatile long position;
private volatile byte nextByte;
private long updatePosition(long nwrote) {
position += nwrote;
return position;
}
private ByteBuffer genNextBuffer() {
int n = Math.min(8192 + rand.nextInt(8192), (int)(size - position));
ByteBuffer buf = ByteBuffer.allocate(n);
for (int i=0; i<n; i++) {
buf.put(nextByte++);
}
buf.flip();
return buf;
}
// close channel and release any waiters
private void done() {
try {
channel.close();
} catch (IOException ignore) { }
latch.countDown();
}
Writer(File file, long size, CountDownLatch latch) throws IOException {
this.file = file;
this.size = size;
this.latch = latch;
this.channel = AsynchronousFileChannel.open(file.toPath(), WRITE);
}
File file() {
return file;
}
long size() {
return size;
}
// initiate first write
void start() {
ByteBuffer buf = genNextBuffer();
channel.write(buf, 0L, buf, this);
}
@Override
public void completed(Integer nwrote, ByteBuffer buf) {
long pos = updatePosition(nwrote);
if (!buf.hasRemaining()) {
// buffer has been completely written; decide if we need to
// write more
if (position >= size) {
done();
return;
}
buf = genNextBuffer();
}
channel.write(buf, pos, buf, this);
}
@Override
public void failed(Throwable exc, ByteBuffer buf) {
exc.printStackTrace();
done();
}
}
public static void main(String[] args) throws Exception {
// random number of writers
int count = 20 + rand.nextInt(16);
Writer[] writers = new Writer[count];
CountDownLatch latch = new CountDownLatch(count);
// initiate writing to each file
for (int i=0; i<count; i++) {
long size = 512*1024 + rand.nextInt(512*1024);
File blah = File.createTempFile("blah", null);
blah.deleteOnExit();
Writer writer = new Writer(blah, size, latch);
writers[i] = writer;
writer.start();
}
// wait for writing to complete
latch.await();
// verify content of each file
boolean failed = false;
byte[] buf = new byte[8192];
for (int i=0; i<count ;i++) {
Writer writer = writers[i];
FileInputStream in = new FileInputStream(writer.file());
try {
long size = 0L;
byte expected = 0;
int nread = in.read(buf);
while (nread > 0) {
for (int j=0; j<nread; j++) {
if (buf[j] != expected) {
System.err.println("Unexpected contents");
failed = true;
break;
}
expected++;
}
if (failed)
break;
size += nread;
nread = in.read(buf);
}
if (!failed && size != writer.size()) {
System.err.println("Unexpected size");
failed = true;
}
if (failed)
break;
} finally {
in.close();
}
}
// clean-up
for (int i=0; i<count; i++) {
writers[i].file().delete();
}
if (failed)
throw new RuntimeException("Test failed");
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2008, 2009, 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.
*/
import java.util.concurrent.ThreadFactory;
import java.util.*;
public class MyThreadFactory implements ThreadFactory {
private static final Set<Thread> threads = new HashSet<Thread>();
static boolean created(Thread t) {
synchronized (threads) {
return threads.contains(t);
}
}
public MyThreadFactory() {
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
synchronized (threads) {
threads.add(t);
}
return t;
}
}