Skip to content

Commit 1aeea79

Browse files
committed
Merge
2 parents e13da98 + 4215779 commit 1aeea79

File tree

11 files changed

+280
-78
lines changed

11 files changed

+280
-78
lines changed

src/hotspot/share/opto/addnode.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 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
@@ -1124,6 +1124,14 @@ static bool can_overflow(const TypeInt* t, jint c) {
11241124
(c > 0 && (java_add(t_hi, c) < t_hi)));
11251125
}
11261126

1127+
// Check if addition of a long with type 't' and a constant 'c' can overflow.
1128+
static bool can_overflow(const TypeLong* t, jlong c) {
1129+
jlong t_lo = t->_lo;
1130+
jlong t_hi = t->_hi;
1131+
return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1132+
(c > 0 && (java_add(t_hi, c) < t_hi)));
1133+
}
1134+
11271135
// Let <x, x_off> = x_operands and <y, y_off> = y_operands.
11281136
// If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
11291137
// add(x, op(x_off, y_off)). Otherwise, return nullptr.
@@ -1280,6 +1288,31 @@ const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
12801288
//
12811289
// Note: we assume that SubL was already replaced by an AddL, and that the stride
12821290
// has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1291+
//
1292+
// Proof MaxL collapsed version equivalent to original (MinL version similar):
1293+
// is_sub_con ensures that con1, con2 ∈ [min_int, 0[
1294+
//
1295+
// Original:
1296+
// - AddL2 underflow => x + con2 ∈ ]max_long - min_int, max_long], ALWAYS BAILOUT as x + con1 + con2 surely fails can_overflow (*)
1297+
// - AddL2 no underflow => x + con2 ∈ [min_long, max_long]
1298+
// - MaxL2 clamp => min_int
1299+
// - AddL1 underflow: NOT POSSIBLE: cannot underflow since min_int + con1 ∈ [2 * min_int, min_int] always > min_long
1300+
// - AddL1 no underflow => min_int + con1 ∈ [2 * min_int, min_int]
1301+
// - MaxL1 clamp => min_int (RESULT 1)
1302+
// - MaxL1 no clamp: NOT POSSIBLE: min_int + con1 ∈ [2 * min_int, min_int] always <= min_int, so clamp always taken
1303+
// - MaxL2 no clamp => x + con2 ∈ [min_int, max_long]
1304+
// - AddL1 underflow: NOT POSSIBLE: cannot underflow since x + con2 + con1 ∈ [2 * min_int, max_long] always > min_long
1305+
// - AddL1 no underflow => x + con2 + con1 ∈ [2 * min_int, max_long]
1306+
// - MaxL1 clamp => min_int (RESULT 2)
1307+
// - MaxL1 no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1308+
//
1309+
// Collapsed:
1310+
// - AddL2 (cannot underflow) => con2 + con1 ∈ [2 * min_int, 0]
1311+
// - AddL1 underflow: NOT POSSIBLE: would have bailed out at can_overflow (*)
1312+
// - AddL1 no underflow => x + con2 + con1 ∈ [min_long, max_long]
1313+
// - MaxL clamp => min_int (RESULT 1 and RESULT 2)
1314+
// - MaxL no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1315+
//
12831316
Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
12841317
assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
12851318
// Check that the two clamps have the correct values.
@@ -1309,6 +1342,10 @@ Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
13091342
Node* x = add2->in(1);
13101343
Node* con2 = add2->in(2);
13111344
if (is_sub_con(con2)) {
1345+
// Collapsed graph not equivalent if potential over/underflow -> bailing out (*)
1346+
if (can_overflow(phase->type(x)->is_long(), con1->get_long() + con2->get_long())) {
1347+
return nullptr;
1348+
}
13121349
Node* new_con = phase->transform(new AddLNode(con1, con2));
13131350
Node* new_sub = phase->transform(new AddLNode(x, new_con));
13141351
n->set_req_X(1, new_sub, phase);

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ protected void engineInit(int opmode, Key key,
235235
params.getParameterSpec(OAEPParameterSpec.class);
236236
init(opmode, key, random, spec);
237237
} catch (InvalidParameterSpecException ipse) {
238-
throw new InvalidAlgorithmParameterException("Wrong parameter", ipse);
238+
throw new InvalidAlgorithmParameterException("Wrong parameter",
239+
ipse);
239240
}
240241
}
241242
}
@@ -379,7 +380,7 @@ private byte[] doFinal() throws BadPaddingException,
379380
byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
380381
paddingCopy = RSACore.rsa(decryptBuffer, privateKey, false);
381382
result = padding.unpad(paddingCopy);
382-
if (result == null && !forTlsPremasterSecret) {
383+
if (!forTlsPremasterSecret && result == null) {
383384
throw new BadPaddingException
384385
("Padding error in decryption");
385386
}
@@ -399,6 +400,34 @@ private byte[] doFinal() throws BadPaddingException,
399400
}
400401
}
401402

403+
// TLS master secret decode version of the doFinal() method.
404+
private byte[] doFinalForTls(int clientVersion, int serverVersion)
405+
throws BadPaddingException, IllegalBlockSizeException {
406+
if (bufOfs > buffer.length) {
407+
throw new IllegalBlockSizeException("Data must not be longer "
408+
+ "than " + buffer.length + " bytes");
409+
}
410+
byte[] paddingCopy = null;
411+
byte[] result = null;
412+
try {
413+
byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
414+
415+
paddingCopy = RSACore.rsa(decryptBuffer, privateKey, false);
416+
result = padding.unpadForTls(paddingCopy, clientVersion,
417+
serverVersion);
418+
419+
return result;
420+
} finally {
421+
Arrays.fill(buffer, 0, bufOfs, (byte)0);
422+
bufOfs = 0;
423+
if (paddingCopy != null
424+
&& paddingCopy != buffer // already cleaned
425+
&& paddingCopy != result) { // DO NOT CLEAN, THIS IS RESULT
426+
Arrays.fill(paddingCopy, (byte)0);
427+
}
428+
}
429+
}
430+
402431
// see JCE spec
403432
protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
404433
update(in, inOfs, inLen);
@@ -471,38 +500,34 @@ protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
471500
byte[] encoded = null;
472501

473502
update(wrappedKey, 0, wrappedKey.length);
474-
try {
475-
encoded = doFinal();
476-
} catch (BadPaddingException | IllegalBlockSizeException e) {
477-
// BadPaddingException cannot happen for TLS RSA unwrap.
478-
// In that case, padding error is indicated by returning null.
479-
// IllegalBlockSizeException cannot happen in any case,
480-
// because of the length check above.
481-
throw new InvalidKeyException("Unwrapping failed", e);
482-
}
483-
484503
try {
485504
if (isTlsRsaPremasterSecret) {
486505
if (!forTlsPremasterSecret) {
487506
throw new IllegalStateException(
488507
"No TlsRsaPremasterSecretParameterSpec specified");
489508
}
490-
491-
// polish the TLS premaster secret
492-
encoded = KeyUtil.checkTlsPreMasterSecretKey(
493-
((TlsRsaPremasterSecretParameterSpec) spec).getClientVersion(),
494-
((TlsRsaPremasterSecretParameterSpec) spec).getServerVersion(),
495-
random, encoded, encoded == null);
509+
TlsRsaPremasterSecretParameterSpec parameterSpec =
510+
(TlsRsaPremasterSecretParameterSpec) spec;
511+
encoded = doFinalForTls(parameterSpec.getClientVersion(),
512+
parameterSpec.getServerVersion());
513+
} else {
514+
encoded = doFinal();
496515
}
497-
498516
return ConstructKeys.constructKey(encoded, algorithm, type);
517+
518+
} catch (BadPaddingException | IllegalBlockSizeException e) {
519+
// BadPaddingException cannot happen for TLS RSA unwrap.
520+
// Neither padding error nor server version error is indicated
521+
// for TLS, but a fake unwrapped value is returned.
522+
// IllegalBlockSizeException cannot happen in any case,
523+
// because of the length check above.
524+
throw new InvalidKeyException("Unwrapping failed", e);
499525
} finally {
500526
if (encoded != null) {
501527
Arrays.fill(encoded, (byte) 0);
502528
}
503529
}
504530
}
505-
506531
// see JCE spec
507532
protected int engineGetKeySize(Key key) throws InvalidKeyException {
508533
RSAKey rsaKey = RSAKeyFactory.toRSAKey(key);

src/java.base/share/classes/java/util/jar/JarFile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ private Manifest getManifestFromReference() throws IOException {
421421
jv = new JarVerifier(manEntry.getName(), b);
422422
} else {
423423
if (JarVerifier.debug != null) {
424-
JarVerifier.debug.println("Multiple MANIFEST.MF found. Treat JAR file as unsigned");
424+
JarVerifier.debug.println(
425+
JarVerifier.MULTIPLE_MANIFEST_WARNING);
425426
}
426427
}
427428
}

src/java.base/share/classes/java/util/jar/JarInputStream.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,17 @@ private JarEntry checkManifest(JarEntry e)
146146
jv = new JarVerifier(e.getName(), bytes);
147147
mev = new ManifestEntryVerifier(man, jv.manifestName);
148148
}
149-
return (JarEntry)super.getNextEntry();
149+
JarEntry nextEntry = (JarEntry)super.getNextEntry();
150+
if (nextEntry != null &&
151+
JarFile.MANIFEST_NAME.equalsIgnoreCase(nextEntry.getName())) {
152+
if (JarVerifier.debug != null) {
153+
JarVerifier.debug.println(JarVerifier.MULTIPLE_MANIFEST_WARNING);
154+
}
155+
156+
jv = null;
157+
mev = null;
158+
}
159+
return nextEntry;
150160
}
151161
return e;
152162
}

src/java.base/share/classes/java/util/jar/JarVerifier.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, 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
@@ -46,6 +46,9 @@
4646
*/
4747
class JarVerifier {
4848

49+
public static final String MULTIPLE_MANIFEST_WARNING =
50+
"WARNING: Multiple MANIFEST.MF found. Treat JAR file as unsigned.";
51+
4952
/* Are we debugging ? */
5053
static final Debug debug = Debug.getInstance("jar");
5154

src/java.base/share/classes/java/util/zip/DeflaterOutputStream.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@
4040
* @since 1.1
4141
*/
4242
public class DeflaterOutputStream extends FilterOutputStream {
43+
44+
/*
45+
* The default size of the output buffer
46+
*/
47+
static final int DEFAULT_BUF_SIZE = 512;
48+
49+
/*
50+
* When calling Deflater.deflate() with Deflater.SYNC_FLUSH or Deflater.FULL_FLUSH,
51+
* the callers are expected to ensure that the size of the buffer is greater than 6.
52+
* This expectation comes from the underlying zlib library which in its zlib.h
53+
* states:
54+
* "If deflate returns with avail_out == 0, this function must be called again
55+
* with the same value of the flush parameter and more output space (updated
56+
* avail_out), until the flush is complete (deflate returns with non-zero
57+
* avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
58+
* avail_out is greater than six when the flush marker begins, in order to avoid
59+
* repeated flush markers upon calling deflate() again when avail_out == 0."
60+
*/
61+
private static final int SYNC_FLUSH_MIN_BUF_SIZE = 7;
62+
4363
/**
4464
* Compressor for this stream.
4565
*/
@@ -123,7 +143,7 @@ public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
123143
public DeflaterOutputStream(OutputStream out,
124144
Deflater def,
125145
boolean syncFlush) {
126-
this(out, def, 512, syncFlush);
146+
this(out, def, DEFAULT_BUF_SIZE, syncFlush);
127147
}
128148

129149

@@ -138,7 +158,7 @@ public DeflaterOutputStream(OutputStream out,
138158
* @param def the compressor ("deflater")
139159
*/
140160
public DeflaterOutputStream(OutputStream out, Deflater def) {
141-
this(out, def, 512, false);
161+
this(out, def, DEFAULT_BUF_SIZE, false);
142162
}
143163

144164
boolean usesDefaultDeflater = false;
@@ -158,7 +178,7 @@ public DeflaterOutputStream(OutputStream out, Deflater def) {
158178
* @since 1.7
159179
*/
160180
public DeflaterOutputStream(OutputStream out, boolean syncFlush) {
161-
this(out, out != null ? new Deflater() : null, 512, syncFlush);
181+
this(out, out != null ? new Deflater() : null, DEFAULT_BUF_SIZE, syncFlush);
162182
usesDefaultDeflater = true;
163183
}
164184

@@ -181,6 +201,7 @@ public DeflaterOutputStream(OutputStream out) {
181201
* @param b the byte to be written
182202
* @throws IOException if an I/O error has occurred
183203
*/
204+
@Override
184205
public void write(int b) throws IOException {
185206
byte[] buf = new byte[1];
186207
buf[0] = (byte)(b & 0xff);
@@ -195,6 +216,7 @@ public void write(int b) throws IOException {
195216
* @param len the length of the data
196217
* @throws IOException if an I/O error has occurred
197218
*/
219+
@Override
198220
public void write(byte[] b, int off, int len) throws IOException {
199221
if (def.finished()) {
200222
throw new IOException("write beyond end of stream");
@@ -238,6 +260,7 @@ public void finish() throws IOException {
238260
* underlying stream.
239261
* @throws IOException if an I/O error has occurred
240262
*/
263+
@Override
241264
public void close() throws IOException {
242265
if (!closed) {
243266
try {
@@ -277,13 +300,20 @@ protected void deflate() throws IOException {
277300
*
278301
* @since 1.7
279302
*/
303+
@Override
280304
public void flush() throws IOException {
281305
if (syncFlush && !def.finished()) {
282306
int len = 0;
283-
while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0)
284-
{
285-
out.write(buf, 0, len);
286-
if (len < buf.length)
307+
// For SYNC_FLUSH, the Deflater.deflate() expects the callers
308+
// to use a buffer whose length is greater than 6 to avoid
309+
// flush marker (5 bytes) being repeatedly output to the output buffer
310+
// every time it is invoked.
311+
final byte[] flushBuf = buf.length < SYNC_FLUSH_MIN_BUF_SIZE
312+
? new byte[DEFAULT_BUF_SIZE]
313+
: buf;
314+
while ((len = def.deflate(flushBuf, 0, flushBuf.length, Deflater.SYNC_FLUSH)) > 0) {
315+
out.write(flushBuf, 0, len);
316+
if (len < flushBuf.length)
287317
break;
288318
}
289319
}

src/java.base/share/classes/java/util/zip/GZIPOutputStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public GZIPOutputStream(OutputStream out, int size, boolean syncFlush)
109109
* @throws IOException If an I/O error has occurred.
110110
*/
111111
public GZIPOutputStream(OutputStream out) throws IOException {
112-
this(out, 512, false);
112+
this(out, DeflaterOutputStream.DEFAULT_BUF_SIZE, false);
113113
}
114114

115115
/**
@@ -131,7 +131,7 @@ public GZIPOutputStream(OutputStream out) throws IOException {
131131
public GZIPOutputStream(OutputStream out, boolean syncFlush)
132132
throws IOException
133133
{
134-
this(out, 512, syncFlush);
134+
this(out, DeflaterOutputStream.DEFAULT_BUF_SIZE, syncFlush);
135135
}
136136

137137
/**

0 commit comments

Comments
 (0)