The FFM API allows the creation of `MemorySegment` instances viewed as buffers. These buffers need to be guarded during use so they are not closed underneath. Also, it is better to use `dst.isDirect()` rather than instanceof testing.
While an asynchronous event is taking place, the buffer needs to remain under guard. It is only when the event completes, the guard can be lifted.
Guard: IOUtil.acquireScope(buf, true);
The implementation classes specifically affected are SimpleAsynchronousFileChannelImpl and WindowsAsynchronousFileChannelImpl in the sun.nio.ch package.
We could also add a buffer variant in the test:
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
return switch (rand.nextInt(4)) {
case 0 -> ByteBuffer.allocateDirect(buf.length)
.put(buf)
.flip();
case 1 -> ByteBuffer.wrap(buf);
case 2 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 3 -> Arena.ofShared().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
default -> throw new InternalError("Should not reach here");
};
}
While an asynchronous event is taking place, the buffer needs to remain under guard. It is only when the event completes, the guard can be lifted.
Guard: IOUtil.acquireScope(buf, true);
The implementation classes specifically affected are SimpleAsynchronousFileChannelImpl and WindowsAsynchronousFileChannelImpl in the sun.nio.ch package.
We could also add a buffer variant in the test:
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
return switch (rand.nextInt(4)) {
case 0 -> ByteBuffer.allocateDirect(buf.length)
.put(buf)
.flip();
case 1 -> ByteBuffer.wrap(buf);
case 2 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
case 3 -> Arena.ofShared().allocate(buf.length).asByteBuffer()
.put(buf)
.flip();
default -> throw new InternalError("Should not reach here");
};
}
- relates to
-
JDK-8358537 (bf) JavaNioAccess::getBufferAddress bypasses direct buffer MemorySession state check
-
- In Progress
-
-
JDK-8357268 Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()
-
- Resolved
-
-
JDK-8358958 (aio) AsynchronousByteChannel.read/write should throw IAE if buffer is thread-confined
-
- Resolved
-
- links to
-
Commit(master) openjdk/jdk/9a1c1f2e
-
Review(master) openjdk/jdk/25531