Skip to content

Commit

Permalink
Remove session exposure
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Nov 29, 2022
1 parent eedf41a commit 619473e
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1590,12 +1590,12 @@ public int doFinal(ByteBuffer src, ByteBuffer dst)
int ofs = dst.arrayOffset() + dst.position();
Arrays.fill(dst.array(), ofs , ofs + len, (byte)0);
} else {
var scope = NIO_ACCESS.acquireSession(dst);
NIO_ACCESS.acquireSession(dst);
try {
Unsafe.getUnsafe().setMemory(((DirectBuffer)dst).address(),
len + dst.position(), (byte) 0);
} finally {
NIO_ACCESS.releaseSession(dst, scope);
NIO_ACCESS.releaseSession(dst);
}
}
throw new AEADBadTagException("Tag mismatch");
Expand Down
17 changes: 10 additions & 7 deletions src/java.base/share/classes/java/nio/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -825,19 +825,17 @@ public MemorySegment bufferSegment(Buffer buffer) {
}

@Override
public MemorySessionImpl acquireSession(Buffer buffer) {
public void acquireSession(Buffer buffer) {
var scope = buffer.session();
if (scope == null) {
return null;
if (scope != null) {
scope.acquire0();
}
scope.acquire0();
return scope;
}

@Override
public void releaseSession(Buffer buffer, MemorySessionImpl scope) {
assert buffer.session() == scope;
public void releaseSession(Buffer buffer) {
try {
var scope = buffer.session();
if (scope != null) {
scope.release0();
}
Expand All @@ -852,6 +850,11 @@ public boolean isThreadConfined(Buffer buffer) {
return scope != null && scope.ownerThread() != null;
}

@Override
public boolean hasSession(Buffer buffer) {
return buffer.session() != null;
}

@Override
public void force(FileDescriptor fd, long address, boolean isSync, long offset, long size) {
MappedMemoryUtils.force(fd, address, isSync, offset, size);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/Adler32.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public void update(ByteBuffer buffer) {
if (rem <= 0)
return;
if (buffer.isDirect()) {
var scope = NIO_ACCESS.acquireSession(buffer);
NIO_ACCESS.acquireSession(buffer);
try {
adler = updateByteBuffer(adler, ((DirectBuffer)buffer).address(), pos, rem);
} finally {
NIO_ACCESS.releaseSession(buffer, scope);
NIO_ACCESS.releaseSession(buffer);
}
} else if (buffer.hasArray()) {
adler = updateBytes(adler, buffer.array(), pos + buffer.arrayOffset(), rem);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/CRC32.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public void update(ByteBuffer buffer) {
if (rem <= 0)
return;
if (buffer.isDirect()) {
var scope = NIO_ACCESS.acquireSession(buffer);
NIO_ACCESS.acquireSession(buffer);
try {
crc = updateByteBuffer(crc, ((DirectBuffer)buffer).address(), pos, rem);
} finally {
NIO_ACCESS.releaseSession(buffer, scope);
NIO_ACCESS.releaseSession(buffer);
}
} else if (buffer.hasArray()) {
crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(), rem);
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/util/zip/CRC32C.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public void update(ByteBuffer buffer) {
}

if (buffer.isDirect()) {
var scope = NIO_ACCESS.acquireSession(buffer);
NIO_ACCESS.acquireSession(buffer);
try {
crc = updateDirectByteBuffer(crc, ((DirectBuffer)buffer).address(),
pos, limit);
} finally {
NIO_ACCESS.releaseSession(buffer, scope);
NIO_ACCESS.releaseSession(buffer);
}
} else if (buffer.hasArray()) {
crc = updateBytes(crc, buffer.array(), pos + buffer.arrayOffset(),
Expand Down
24 changes: 12 additions & 12 deletions src/java.base/share/classes/java/util/zip/Deflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,12 @@ public void setDictionary(ByteBuffer dictionary) {
int remaining = Math.max(dictionary.limit() - position, 0);
ensureOpen();
if (dictionary.isDirect()) {
var scope = NIO_ACCESS.acquireSession(dictionary);
NIO_ACCESS.acquireSession(dictionary);
try {
long address = ((DirectBuffer) dictionary).address();
setDictionaryBuffer(zsRef.address(), address + position, remaining);
} finally {
NIO_ACCESS.releaseSession(dictionary, scope);
NIO_ACCESS.releaseSession(dictionary);
}
} else {
byte[] array = ZipUtils.getBufferArray(dictionary);
Expand Down Expand Up @@ -589,15 +589,15 @@ public int deflate(byte[] output, int off, int len, int flush) {
inputPos = input.position();
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
var scope = NIO_ACCESS.acquireSession(input);
NIO_ACCESS.acquireSession(input);
try {
long inputAddress = ((DirectBuffer) input).address();
result = deflateBufferBytes(zsRef.address(),
inputAddress + inputPos, inputRem,
output, off, len,
flush, params);
} finally {
NIO_ACCESS.releaseSession(input, scope);
NIO_ACCESS.releaseSession(input);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
Expand Down Expand Up @@ -712,15 +712,15 @@ public int deflate(ByteBuffer output, int flush) {
if (input == null) {
inputPos = this.inputPos;
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = ((DirectBuffer) output).address();
result = deflateBytesBuffer(zsRef.address(),
inputArray, inputPos, inputLim - inputPos,
outputAddress + outputPos, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -734,19 +734,19 @@ public int deflate(ByteBuffer output, int flush) {
inputPos = input.position();
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
var inScope = NIO_ACCESS.acquireSession(input);
NIO_ACCESS.acquireSession(input);
try {
long inputAddress = ((DirectBuffer) input).address();
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = outputPos + ((DirectBuffer) output).address();
result = deflateBufferBuffer(zsRef.address(),
inputAddress + inputPos, inputRem,
outputAddress, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -757,21 +757,21 @@ public int deflate(ByteBuffer output, int flush) {
flush, params);
}
} finally {
NIO_ACCESS.releaseSession(input, inScope);
NIO_ACCESS.releaseSession(input);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
int inputOffset = ZipUtils.getBufferOffset(input);
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = ((DirectBuffer) output).address();
result = deflateBytesBuffer(zsRef.address(),
inputArray, inputOffset + inputPos, inputRem,
outputAddress + outputPos, outputRem,
flush, params);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand Down
24 changes: 12 additions & 12 deletions src/java.base/share/classes/java/util/zip/Inflater.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ public void setDictionary(ByteBuffer dictionary) {
int remaining = Math.max(dictionary.limit() - position, 0);
ensureOpen();
if (dictionary.isDirect()) {
var scope = NIO_ACCESS.acquireSession(dictionary);
NIO_ACCESS.acquireSession(dictionary);
try {
long address = ((DirectBuffer) dictionary).address();
setDictionaryBuffer(zsRef.address(), address + position, remaining);
} finally {
NIO_ACCESS.releaseSession(dictionary, scope);
NIO_ACCESS.releaseSession(dictionary);
}
} else {
byte[] array = ZipUtils.getBufferArray(dictionary);
Expand Down Expand Up @@ -385,14 +385,14 @@ public int inflate(byte[] output, int off, int len)
try {
int inputRem = Math.max(input.limit() - inputPos, 0);
if (input.isDirect()) {
var inScope = NIO_ACCESS.acquireSession(input);
NIO_ACCESS.acquireSession(input);
try {
long inputAddress = ((DirectBuffer) input).address();
result = inflateBufferBytes(zsRef.address(),
inputAddress + inputPos, inputRem,
output, off, len);
} finally {
NIO_ACCESS.releaseSession(input, inScope);
NIO_ACCESS.releaseSession(input);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
Expand Down Expand Up @@ -520,14 +520,14 @@ public int inflate(ByteBuffer output) throws DataFormatException {
inputPos = this.inputPos;
try {
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = ((DirectBuffer) output).address();
result = inflateBytesBuffer(zsRef.address(),
inputArray, inputPos, inputLim - inputPos,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -545,18 +545,18 @@ public int inflate(ByteBuffer output) throws DataFormatException {
int inputRem = Math.max(input.limit() - inputPos, 0);
try {
if (input.isDirect()) {
var inScope = NIO_ACCESS.acquireSession(input);
NIO_ACCESS.acquireSession(input);
try {
long inputAddress = ((DirectBuffer) input).address();
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = ((DirectBuffer) output).address();
result = inflateBufferBuffer(zsRef.address(),
inputAddress + inputPos, inputRem,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand All @@ -566,20 +566,20 @@ public int inflate(ByteBuffer output) throws DataFormatException {
outputArray, outputOffset + outputPos, outputRem);
}
} finally {
NIO_ACCESS.releaseSession(input, inScope);
NIO_ACCESS.releaseSession(input);
}
} else {
byte[] inputArray = ZipUtils.getBufferArray(input);
int inputOffset = ZipUtils.getBufferOffset(input);
if (output.isDirect()) {
var outScope = NIO_ACCESS.acquireSession(output);
NIO_ACCESS.acquireSession(output);
try {
long outputAddress = ((DirectBuffer) output).address();
result = inflateBytesBuffer(zsRef.address(),
inputArray, inputOffset + inputPos, inputRem,
outputAddress + outputPos, outputRem);
} finally {
NIO_ACCESS.releaseSession(output, outScope);
NIO_ACCESS.releaseSession(output);
}
} else {
byte[] outputArray = ZipUtils.getBufferArray(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package jdk.internal.access;

import jdk.internal.access.foreign.UnmapperProxy;
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.misc.VM.BufferPool;

import java.lang.foreign.MemorySegment;
Expand Down Expand Up @@ -89,24 +88,25 @@ public interface JavaNioAccess {
/**
* Used by operations to make a buffer's session non-closeable
* (for the duration of the operation) by acquiring the session.
* The buffers scope is returned if it has one, otherwise {@code null} is returned.
* {@snippet lang = java:
* var guard = acquireSession(buffer);
* acquireSession(buffer);
* try {
* performOperation(buffer);
* } finally {
* releaseSession(buffer, guard);
* releaseSession(buffer);
* }
*}
*
* @see #releaseSession(Buffer, MemorySessionImpl)
* @see #releaseSession(Buffer)
*/
MemorySessionImpl acquireSession(Buffer buffer);
void acquireSession(Buffer buffer);

void releaseSession(Buffer buffer, MemorySessionImpl scope);
void releaseSession(Buffer buffer);

boolean isThreadConfined(Buffer buffer);

boolean hasSession(Buffer buffer);

/**
* Used by {@code jdk.internal.foreign.MappedMemorySegmentImpl} and byte buffer var handle views.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ private int receiveIntoNativeBuffer(ByteBuffer bb, int rem, int pos,
boolean connected)
throws IOException
{
var scope = NIO_ACCESS.acquireSession(bb);
NIO_ACCESS.acquireSession(bb);
try {
int n = receive0(fd,
((DirectBuffer)bb).address() + pos, rem,
Expand All @@ -794,7 +794,7 @@ private int receiveIntoNativeBuffer(ByteBuffer bb, int rem, int pos,
bb.position(pos + n);
return n;
} finally {
NIO_ACCESS.releaseSession(bb, scope);
NIO_ACCESS.releaseSession(bb);
}
}

Expand Down Expand Up @@ -939,7 +939,7 @@ private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
int rem = (pos <= lim ? lim - pos : 0);

int written;
var scope = NIO_ACCESS.acquireSession(bb);
NIO_ACCESS.acquireSession(bb);
try {
int addressLen = targetSocketAddress(target);
written = send0(fd, ((DirectBuffer)bb).address() + pos, rem,
Expand All @@ -949,7 +949,7 @@ private int sendFromNativeBuffer(FileDescriptor fd, ByteBuffer bb,
throw pue;
written = rem;
} finally {
NIO_ACCESS.releaseSession(bb, scope);
NIO_ACCESS.releaseSession(bb);
}
if (written > 0)
bb.position(pos + written);
Expand Down
Loading

0 comments on commit 619473e

Please sign in to comment.