Skip to content

Commit d4b923d

Browse files
committed
8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()
Reviewed-by: alanb, valeriep
1 parent c1f066e commit d4b923d

File tree

14 files changed

+78
-61
lines changed

14 files changed

+78
-61
lines changed

src/java.base/share/classes/com/sun/crypto/provider/GaloisCounterMode.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
2727

2828
import java.lang.invoke.MethodHandles;
2929
import java.lang.invoke.VarHandle;
30+
import java.nio.Buffer;
3031
import java.nio.ByteBuffer;
3132
import java.nio.ByteOrder;
3233
import java.security.*;
@@ -910,26 +911,26 @@ int doLastBlock(GCMOperation op, ByteBuffer buffer, ByteBuffer src,
910911
*/
911912
ByteBuffer overlapDetection(ByteBuffer src, ByteBuffer dst) {
912913
if (src.isDirect() && dst.isDirect()) {
913-
// The use of DirectBuffer::address below need not be guarded as
914+
// The use of addresses below need not be guarded as
914915
// no access is made to actual memory.
915916
DirectBuffer dsrc = (DirectBuffer) src;
916917
DirectBuffer ddst = (DirectBuffer) dst;
917918

918919
// Get the current memory address for the given ByteBuffers
919-
long srcaddr = dsrc.address();
920-
long dstaddr = ddst.address();
920+
long srcaddr = NIO_ACCESS.getBufferAddress(src);
921+
long dstaddr = NIO_ACCESS.getBufferAddress(dst);
921922

922923
// Find the lowest attachment that is the base memory address
923924
// of the shared memory for the src object
924925
while (dsrc.attachment() != null) {
925-
srcaddr = ((DirectBuffer) dsrc.attachment()).address();
926+
srcaddr = NIO_ACCESS.getBufferAddress((Buffer) dsrc.attachment());
926927
dsrc = (DirectBuffer) dsrc.attachment();
927928
}
928929

929930
// Find the lowest attachment that is the base memory address
930931
// of the shared memory for the dst object
931932
while (ddst.attachment() != null) {
932-
dstaddr = ((DirectBuffer) ddst.attachment()).address();
933+
dstaddr = NIO_ACCESS.getBufferAddress((Buffer) ddst.attachment());
933934
ddst = (DirectBuffer) ddst.attachment();
934935
}
935936

@@ -947,8 +948,8 @@ ByteBuffer overlapDetection(ByteBuffer src, ByteBuffer dst) {
947948
// side, we are not in overlap.
948949
// NOTE: inPlaceArray does not apply here as direct buffers run
949950
// through a byte[] to get to the combined intrinsic
950-
if (((DirectBuffer) src).address() - srcaddr + src.position() >=
951-
((DirectBuffer) dst).address() - dstaddr + dst.position()) {
951+
if (NIO_ACCESS.getBufferAddress(src) - srcaddr + src.position() >=
952+
NIO_ACCESS.getBufferAddress(dst) - dstaddr + dst.position()) {
952953
return dst;
953954
}
954955

@@ -1602,7 +1603,7 @@ public int doFinal(ByteBuffer src, ByteBuffer dst)
16021603
NIO_ACCESS.acquireSession(dst);
16031604
try {
16041605
Unsafe.getUnsafe().setMemory(
1605-
((DirectBuffer)dst).address(),
1606+
NIO_ACCESS.getBufferAddress(dst),
16061607
len + dst.position(), (byte) 0);
16071608
} finally {
16081609
NIO_ACCESS.releaseSession(dst);

src/java.base/share/classes/sun/nio/ch/IOUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -484,7 +484,7 @@ static void acquireScope(ByteBuffer bb, boolean async) {
484484
NIO_ACCESS.acquireSession(bb);
485485
}
486486

487-
private static void releaseScope(ByteBuffer bb) {
487+
static void releaseScope(ByteBuffer bb) {
488488
try {
489489
NIO_ACCESS.releaseSession(bb);
490490
} catch (Exception e) {

src/java.base/unix/classes/sun/nio/fs/UnixUserDefinedFileAttributeView.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ public int read(String name, ByteBuffer dst) throws IOException {
166166
assert (pos <= lim);
167167
int rem = (pos <= lim ? lim - pos : 0);
168168

169-
if (dst instanceof sun.nio.ch.DirectBuffer ddst) {
169+
if (dst.isDirect()) {
170170
NIO_ACCESS.acquireSession(dst);
171171
try {
172-
long address = ddst.address() + pos;
172+
long address = NIO_ACCESS.getBufferAddress(dst) + pos;
173173
int n = read(name, address, rem);
174174
dst.position(pos + n);
175175
return n;
@@ -225,10 +225,10 @@ public int write(String name, ByteBuffer src) throws IOException {
225225
assert (pos <= lim);
226226
int rem = (pos <= lim ? lim - pos : 0);
227227

228-
if (src instanceof sun.nio.ch.DirectBuffer buf) {
228+
if (src.isDirect()) {
229229
NIO_ACCESS.acquireSession(src);
230230
try {
231-
long address = buf.address() + pos;
231+
long address = NIO_ACCESS.getBufferAddress(src) + pos;
232232
write(name, address, rem);
233233
src.position(pos + rem);
234234
return rem;

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11AEADCipher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
2526
package sun.security.pkcs11;
2627

2728
import java.io.ByteArrayOutputStream;
@@ -743,8 +744,8 @@ private int implDoFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
743744
inOfs = 0;
744745
inLen = in.length;
745746
} else {
746-
if (inBuffer instanceof DirectBuffer dInBuffer) {
747-
inAddr = dInBuffer.address();
747+
if (inBuffer instanceof DirectBuffer) {
748+
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
748749
inOfs = inBuffer.position();
749750
} else {
750751
if (inBuffer.hasArray()) {
@@ -759,8 +760,8 @@ private int implDoFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
759760
long outAddr = 0;
760761
byte[] outArray = null;
761762
int outOfs = 0;
762-
if (outBuffer instanceof DirectBuffer dOutBuffer) {
763-
outAddr = dOutBuffer.address();
763+
if (outBuffer instanceof DirectBuffer) {
764+
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
764765
outOfs = outBuffer.position();
765766
} else {
766767
if (outBuffer.hasArray()) {

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
2526
package sun.security.pkcs11;
2627

2728
import java.nio.ByteBuffer;
@@ -742,8 +743,8 @@ private int implUpdate(ByteBuffer inBuffer, ByteBuffer outBuffer)
742743
int inOfs = 0;
743744
byte[] inArray = null;
744745

745-
if (inBuffer instanceof DirectBuffer dInBuffer) {
746-
inAddr = dInBuffer.address();
746+
if (inBuffer instanceof DirectBuffer) {
747+
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
747748
inOfs = origPos;
748749
} else if (inBuffer.hasArray()) {
749750
inArray = inBuffer.array();
@@ -753,8 +754,8 @@ private int implUpdate(ByteBuffer inBuffer, ByteBuffer outBuffer)
753754
long outAddr = 0;
754755
int outOfs = 0;
755756
byte[] outArray = null;
756-
if (outBuffer instanceof DirectBuffer dOutBuffer) {
757-
outAddr = dOutBuffer.address();
757+
if (outBuffer instanceof DirectBuffer) {
758+
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
758759
outOfs = outBuffer.position();
759760
} else {
760761
if (outBuffer.hasArray()) {
@@ -1012,8 +1013,8 @@ private int implDoFinal(ByteBuffer outBuffer)
10121013
long outAddr = 0;
10131014
byte[] outArray = null;
10141015
int outOfs = 0;
1015-
if (outBuffer instanceof DirectBuffer dOutBuffer) {
1016-
outAddr = dOutBuffer.address();
1016+
if (outBuffer instanceof DirectBuffer) {
1017+
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
10171018
outOfs = outBuffer.position();
10181019
} else {
10191020
if (outBuffer.hasArray()) {

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Digest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -271,7 +271,7 @@ protected void engineUpdate(ByteBuffer byteBuffer) {
271271
return;
272272
}
273273

274-
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
274+
if (!(byteBuffer instanceof DirectBuffer)) {
275275
super.engineUpdate(byteBuffer);
276276
return;
277277
}
@@ -289,7 +289,8 @@ protected void engineUpdate(ByteBuffer byteBuffer) {
289289
}
290290
NIO_ACCESS.acquireSession(byteBuffer);
291291
try {
292-
token.p11.C_DigestUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len);
292+
final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
293+
token.p11.C_DigestUpdate(session.id(), address + ofs, null, 0, len);
293294
} finally {
294295
NIO_ACCESS.releaseSession(byteBuffer);
295296
}

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyWrapCipher.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,6 +22,7 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
2526
package sun.security.pkcs11;
2627

2728
import java.io.ByteArrayOutputStream;
@@ -577,8 +578,8 @@ private int implDoFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
577578
inOfs = 0;
578579
inLen = in.length;
579580
} else {
580-
if (inBuffer instanceof DirectBuffer dInBuffer) {
581-
inAddr = dInBuffer.address();
581+
if (inBuffer instanceof DirectBuffer) {
582+
inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
582583
inOfs = inBuffer.position();
583584
} else {
584585
if (inBuffer.hasArray()) {
@@ -593,8 +594,8 @@ private int implDoFinal(ByteBuffer inBuffer, ByteBuffer outBuffer)
593594
long outAddr = 0;
594595
byte[] outArray = null;
595596
int outOfs = 0;
596-
if (outBuffer instanceof DirectBuffer dOutBuffer) {
597-
outAddr = dOutBuffer.address();
597+
if (outBuffer instanceof DirectBuffer) {
598+
outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
598599
outOfs = outBuffer.position();
599600
} else {
600601
if (outBuffer.hasArray()) {

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Mac.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,15 @@ protected void engineUpdate(ByteBuffer byteBuffer) {
279279
if (len <= 0) {
280280
return;
281281
}
282-
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
282+
if (!(byteBuffer instanceof DirectBuffer)) {
283283
super.engineUpdate(byteBuffer);
284284
return;
285285
}
286286
int ofs = byteBuffer.position();
287287
NIO_ACCESS.acquireSession(byteBuffer);
288288
try {
289-
token.p11.C_SignUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len);
289+
final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
290+
token.p11.C_SignUpdate(session.id(), address + ofs, null, 0, len);
290291
} finally {
291292
NIO_ACCESS.releaseSession(byteBuffer);
292293
}

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11PSSSignature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -614,15 +614,15 @@ protected void engineUpdate(ByteBuffer byteBuffer) {
614614
isActive = true;
615615
switch (type) {
616616
case T_UPDATE -> {
617-
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
617+
if (!(byteBuffer instanceof DirectBuffer)) {
618618
// cannot do better than default impl
619619
super.engineUpdate(byteBuffer);
620620
return;
621621
}
622622
int ofs = byteBuffer.position();
623623
NIO_ACCESS.acquireSession(byteBuffer);
624624
try {
625-
long addr = dByteBuffer.address();
625+
long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
626626
if (mode == M_SIGN) {
627627
if (DEBUG) System.out.println(this + ": Calling C_SignUpdate");
628628
token.p11.C_SignUpdate

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Signature.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -579,15 +579,15 @@ protected void engineUpdate(ByteBuffer byteBuffer) {
579579
}
580580
switch (type) {
581581
case T_UPDATE -> {
582-
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) {
582+
if (!(byteBuffer instanceof DirectBuffer)) {
583583
// cannot do better than default impl
584584
super.engineUpdate(byteBuffer);
585585
return;
586586
}
587587
int ofs = byteBuffer.position();
588588
NIO_ACCESS.acquireSession(byteBuffer);
589589
try {
590-
long addr = dByteBuffer.address();
590+
long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
591591
if (mode == M_SIGN) {
592592
token.p11.C_SignUpdate
593593
(session.id(), addr + ofs, null, 0, len);

0 commit comments

Comments
 (0)