8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()

Reviewed-by: alanb, valeriep
This commit is contained in:
Per Minborg 2025-05-27 19:11:12 +00:00
parent c1f066e17e
commit d4b923d175
14 changed files with 78 additions and 61 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@ -27,6 +27,7 @@ package com.sun.crypto.provider;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.*;
@ -910,26 +911,26 @@ abstract class GaloisCounterMode extends CipherSpi {
*/
ByteBuffer overlapDetection(ByteBuffer src, ByteBuffer dst) {
if (src.isDirect() && dst.isDirect()) {
// The use of DirectBuffer::address below need not be guarded as
// The use of addresses below need not be guarded as
// no access is made to actual memory.
DirectBuffer dsrc = (DirectBuffer) src;
DirectBuffer ddst = (DirectBuffer) dst;
// Get the current memory address for the given ByteBuffers
long srcaddr = dsrc.address();
long dstaddr = ddst.address();
long srcaddr = NIO_ACCESS.getBufferAddress(src);
long dstaddr = NIO_ACCESS.getBufferAddress(dst);
// Find the lowest attachment that is the base memory address
// of the shared memory for the src object
while (dsrc.attachment() != null) {
srcaddr = ((DirectBuffer) dsrc.attachment()).address();
srcaddr = NIO_ACCESS.getBufferAddress((Buffer) dsrc.attachment());
dsrc = (DirectBuffer) dsrc.attachment();
}
// Find the lowest attachment that is the base memory address
// of the shared memory for the dst object
while (ddst.attachment() != null) {
dstaddr = ((DirectBuffer) ddst.attachment()).address();
dstaddr = NIO_ACCESS.getBufferAddress((Buffer) ddst.attachment());
ddst = (DirectBuffer) ddst.attachment();
}
@ -947,8 +948,8 @@ abstract class GaloisCounterMode extends CipherSpi {
// side, we are not in overlap.
// NOTE: inPlaceArray does not apply here as direct buffers run
// through a byte[] to get to the combined intrinsic
if (((DirectBuffer) src).address() - srcaddr + src.position() >=
((DirectBuffer) dst).address() - dstaddr + dst.position()) {
if (NIO_ACCESS.getBufferAddress(src) - srcaddr + src.position() >=
NIO_ACCESS.getBufferAddress(dst) - dstaddr + dst.position()) {
return dst;
}
@ -1602,7 +1603,7 @@ abstract class GaloisCounterMode extends CipherSpi {
NIO_ACCESS.acquireSession(dst);
try {
Unsafe.getUnsafe().setMemory(
((DirectBuffer)dst).address(),
NIO_ACCESS.getBufferAddress(dst),
len + dst.position(), (byte) 0);
} finally {
NIO_ACCESS.releaseSession(dst);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -484,7 +484,7 @@ public final class IOUtil {
NIO_ACCESS.acquireSession(bb);
}
private static void releaseScope(ByteBuffer bb) {
static void releaseScope(ByteBuffer bb) {
try {
NIO_ACCESS.releaseSession(bb);
} catch (Exception e) {

View File

@ -166,10 +166,10 @@ abstract class UnixUserDefinedFileAttributeView
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (dst instanceof sun.nio.ch.DirectBuffer ddst) {
if (dst.isDirect()) {
NIO_ACCESS.acquireSession(dst);
try {
long address = ddst.address() + pos;
long address = NIO_ACCESS.getBufferAddress(dst) + pos;
int n = read(name, address, rem);
dst.position(pos + n);
return n;
@ -225,10 +225,10 @@ abstract class UnixUserDefinedFileAttributeView
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (src instanceof sun.nio.ch.DirectBuffer buf) {
if (src.isDirect()) {
NIO_ACCESS.acquireSession(src);
try {
long address = buf.address() + pos;
long address = NIO_ACCESS.getBufferAddress(src) + pos;
write(name, address, rem);
src.position(pos + rem);
return rem;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* 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
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs11;
import java.io.ByteArrayOutputStream;
@ -743,8 +744,8 @@ final class P11AEADCipher extends CipherSpi {
inOfs = 0;
inLen = in.length;
} else {
if (inBuffer instanceof DirectBuffer dInBuffer) {
inAddr = dInBuffer.address();
if (inBuffer instanceof DirectBuffer) {
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = inBuffer.position();
} else {
if (inBuffer.hasArray()) {
@ -759,8 +760,8 @@ final class P11AEADCipher extends CipherSpi {
long outAddr = 0;
byte[] outArray = null;
int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) {
outAddr = dOutBuffer.address();
if (outBuffer instanceof DirectBuffer) {
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position();
} else {
if (outBuffer.hasArray()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs11;
import java.nio.ByteBuffer;
@ -742,8 +743,8 @@ final class P11Cipher extends CipherSpi {
int inOfs = 0;
byte[] inArray = null;
if (inBuffer instanceof DirectBuffer dInBuffer) {
inAddr = dInBuffer.address();
if (inBuffer instanceof DirectBuffer) {
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = origPos;
} else if (inBuffer.hasArray()) {
inArray = inBuffer.array();
@ -753,8 +754,8 @@ final class P11Cipher extends CipherSpi {
long outAddr = 0;
int outOfs = 0;
byte[] outArray = null;
if (outBuffer instanceof DirectBuffer dOutBuffer) {
outAddr = dOutBuffer.address();
if (outBuffer instanceof DirectBuffer) {
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position();
} else {
if (outBuffer.hasArray()) {
@ -1012,8 +1013,8 @@ final class P11Cipher extends CipherSpi {
long outAddr = 0;
byte[] outArray = null;
int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) {
outAddr = dOutBuffer.address();
if (outBuffer instanceof DirectBuffer) {
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position();
} else {
if (outBuffer.hasArray()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -271,7 +271,7 @@ final class P11Digest extends MessageDigestSpi implements Cloneable,
return;
}
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
if (!(byteBuffer instanceof DirectBuffer)) {
super.engineUpdate(byteBuffer);
return;
}
@ -289,7 +289,8 @@ final class P11Digest extends MessageDigestSpi implements Cloneable,
}
NIO_ACCESS.acquireSession(byteBuffer);
try {
token.p11.C_DigestUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len);
final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
token.p11.C_DigestUpdate(session.id(), address + ofs, null, 0, len);
} finally {
NIO_ACCESS.releaseSession(byteBuffer);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.security.pkcs11;
import java.io.ByteArrayOutputStream;
@ -577,8 +578,8 @@ final class P11KeyWrapCipher extends CipherSpi {
inOfs = 0;
inLen = in.length;
} else {
if (inBuffer instanceof DirectBuffer dInBuffer) {
inAddr = dInBuffer.address();
if (inBuffer instanceof DirectBuffer) {
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = inBuffer.position();
} else {
if (inBuffer.hasArray()) {
@ -593,8 +594,8 @@ final class P11KeyWrapCipher extends CipherSpi {
long outAddr = 0;
byte[] outArray = null;
int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) {
outAddr = dOutBuffer.address();
if (outBuffer instanceof DirectBuffer) {
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position();
} else {
if (outBuffer.hasArray()) {

View File

@ -279,14 +279,15 @@ final class P11Mac extends MacSpi {
if (len <= 0) {
return;
}
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
if (!(byteBuffer instanceof DirectBuffer)) {
super.engineUpdate(byteBuffer);
return;
}
int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer);
try {
token.p11.C_SignUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len);
final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
token.p11.C_SignUpdate(session.id(), address + ofs, null, 0, len);
} finally {
NIO_ACCESS.releaseSession(byteBuffer);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* 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
@ -614,7 +614,7 @@ final class P11PSSSignature extends SignatureSpi {
isActive = true;
switch (type) {
case T_UPDATE -> {
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
if (!(byteBuffer instanceof DirectBuffer)) {
// cannot do better than default impl
super.engineUpdate(byteBuffer);
return;
@ -622,7 +622,7 @@ final class P11PSSSignature extends SignatureSpi {
int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer);
try {
long addr = dByteBuffer.address();
long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
if (mode == M_SIGN) {
if (DEBUG) System.out.println(this + ": Calling C_SignUpdate");
token.p11.C_SignUpdate

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -579,7 +579,7 @@ final class P11Signature extends SignatureSpi {
}
switch (type) {
case T_UPDATE -> {
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
if (!(byteBuffer instanceof DirectBuffer)) {
// cannot do better than default impl
super.engineUpdate(byteBuffer);
return;
@ -587,7 +587,7 @@ final class P11Signature extends SignatureSpi {
int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer);
try {
long addr = dByteBuffer.address();
long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
if (mode == M_SIGN) {
token.p11.C_SignUpdate
(session.id(), addr + ofs, null, 0, len);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch.sctp;
import java.net.InetAddress;
@ -829,7 +830,7 @@ public class SctpChannelImpl extends SctpChannel
{
NIO_ACCESS.acquireSession(bb);
try {
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem, peek);
int n = receive0(fd, resultContainer, NIO_ACCESS.getBufferAddress(bb) + pos, rem, peek);
if (n > 0)
bb.position(pos + n);
@ -1012,7 +1013,7 @@ public class SctpChannelImpl extends SctpChannel
NIO_ACCESS.acquireSession(bb);
try {
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr,
int written = send0(fd, NIO_ACCESS.getBufferAddress(bb) + pos, rem, addr,
port, -1 /*121*/, streamNumber, unordered, ppid);
if (written > 0)
bb.position(pos + written);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 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
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch.sctp;
import java.net.InetAddress;
@ -561,7 +562,7 @@ public class SctpMultiChannelImpl extends SctpMultiChannel
throws IOException {
NIO_ACCESS.acquireSession(bb);
try {
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem);
int n = receive0(fd, resultContainer, NIO_ACCESS.getBufferAddress(bb) + pos, rem);
if (n > 0)
bb.position(pos + n);
return n;
@ -870,7 +871,7 @@ public class SctpMultiChannelImpl extends SctpMultiChannel
NIO_ACCESS.acquireSession(bb);
try {
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr,
int written = send0(fd, NIO_ACCESS.getBufferAddress(bb) + pos, rem, addr,
port, assocId, streamNumber, unordered, ppid);
if (written > 0)
bb.position(pos + written);

View File

@ -29,6 +29,7 @@
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;
@ -58,6 +59,7 @@ 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 {
@ -563,15 +565,16 @@ public class Basic {
static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size];
boolean useDirect = rand.nextBoolean();
if (useDirect) {
ByteBuffer bb = ByteBuffer.allocateDirect(buf.length);
bb.put(buf);
bb.flip();
return bb;
} else {
return ByteBuffer.wrap(buf);
}
return switch (rand.nextInt(3)) {
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();
default -> throw new InternalError("Should not reach here");
};
}
// writes all remaining bytes in the buffer to the given channel at the

View File

@ -31,6 +31,7 @@
*/
import java.io.IOException;
import java.lang.foreign.Arena;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@ -46,7 +47,8 @@ import jdk.test.lib.Platform;
public class Basic {
private static Random rand = new Random();
// Must be indeterministic
private static final Random rand = new Random();
private static final String ATTR_NAME = "mime_type";
private static final String ATTR_VALUE = "text/plain";
@ -88,8 +90,12 @@ public class Basic {
static void test(Path file, LinkOption... options) throws IOException {
final UserDefinedFileAttributeView view =
Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options);
ByteBuffer buf = rand.nextBoolean() ?
ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100);
final ByteBuffer buf = switch (rand.nextInt(3)) {
case 0 -> ByteBuffer.allocate(100);
case 1 -> ByteBuffer.allocateDirect(100);
case 2 -> Arena.ofAuto().allocate(100).asByteBuffer();
default -> throw new InternalError("Should not reach here");
};
// Test: write
buf.put(ATTR_VALUE.getBytes()).flip();