Skip to content

Commit c6243fc

Browse files
committed
Merge
Reviewed-by: dfuchs
2 parents fe3bd5d + ed30fce commit c6243fc

File tree

11 files changed

+276
-78
lines changed

11 files changed

+276
-78
lines changed

src/hotspot/share/opto/addnode.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,14 @@ static bool can_overflow(const TypeInt* t, jint c) {
11791179
(c > 0 && (java_add(t_hi, c) < t_hi)));
11801180
}
11811181

1182+
// Check if addition of a long with type 't' and a constant 'c' can overflow.
1183+
static bool can_overflow(const TypeLong* t, jlong c) {
1184+
jlong t_lo = t->_lo;
1185+
jlong t_hi = t->_hi;
1186+
return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1187+
(c > 0 && (java_add(t_hi, c) < t_hi)));
1188+
}
1189+
11821190
// Let <x, x_off> = x_operands and <y, y_off> = y_operands.
11831191
// If x == y and neither add(x, x_off) nor add(y, y_off) overflow, return
11841192
// add(x, op(x_off, y_off)). Otherwise, return nullptr.
@@ -1363,6 +1371,31 @@ const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
13631371
//
13641372
// Note: we assume that SubL was already replaced by an AddL, and that the stride
13651373
// has its sign flipped: SubL(limit, stride) -> AddL(limit, -stride).
1374+
//
1375+
// Proof MaxL collapsed version equivalent to original (MinL version similar):
1376+
// is_sub_con ensures that con1, con2 ∈ [min_int, 0[
1377+
//
1378+
// Original:
1379+
// - AddL2 underflow => x + con2 ∈ ]max_long - min_int, max_long], ALWAYS BAILOUT as x + con1 + con2 surely fails can_overflow (*)
1380+
// - AddL2 no underflow => x + con2 ∈ [min_long, max_long]
1381+
// - MaxL2 clamp => min_int
1382+
// - AddL1 underflow: NOT POSSIBLE: cannot underflow since min_int + con1 ∈ [2 * min_int, min_int] always > min_long
1383+
// - AddL1 no underflow => min_int + con1 ∈ [2 * min_int, min_int]
1384+
// - MaxL1 clamp => min_int (RESULT 1)
1385+
// - MaxL1 no clamp: NOT POSSIBLE: min_int + con1 ∈ [2 * min_int, min_int] always <= min_int, so clamp always taken
1386+
// - MaxL2 no clamp => x + con2 ∈ [min_int, max_long]
1387+
// - AddL1 underflow: NOT POSSIBLE: cannot underflow since x + con2 + con1 ∈ [2 * min_int, max_long] always > min_long
1388+
// - AddL1 no underflow => x + con2 + con1 ∈ [2 * min_int, max_long]
1389+
// - MaxL1 clamp => min_int (RESULT 2)
1390+
// - MaxL1 no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1391+
//
1392+
// Collapsed:
1393+
// - AddL2 (cannot underflow) => con2 + con1 ∈ [2 * min_int, 0]
1394+
// - AddL1 underflow: NOT POSSIBLE: would have bailed out at can_overflow (*)
1395+
// - AddL1 no underflow => x + con2 + con1 ∈ [min_long, max_long]
1396+
// - MaxL clamp => min_int (RESULT 1 and RESULT 2)
1397+
// - MaxL no clamp => x + con2 + con1 ∈ ]min_int, max_long] (RESULT 3)
1398+
//
13661399
static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
13671400
assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
13681401
// Check that the two clamps have the correct values.
@@ -1392,6 +1425,10 @@ static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
13921425
Node* x = add2->in(1);
13931426
Node* con2 = add2->in(2);
13941427
if (is_sub_con(con2)) {
1428+
// Collapsed graph not equivalent if potential over/underflow -> bailing out (*)
1429+
if (can_overflow(phase->type(x)->is_long(), con1->get_long() + con2->get_long())) {
1430+
return nullptr;
1431+
}
13951432
Node* new_con = phase->transform(new AddLNode(con1, con2));
13961433
Node* new_sub = phase->transform(new AddLNode(x, new_con));
13971434
n->set_req_X(1, new_sub, phase);

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

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ protected void engineInit(int opmode, Key key,
236236
params.getParameterSpec(OAEPParameterSpec.class);
237237
init(opmode, key, random, spec);
238238
} catch (InvalidParameterSpecException ipse) {
239-
throw new InvalidAlgorithmParameterException("Wrong parameter", ipse);
239+
throw new InvalidAlgorithmParameterException("Wrong parameter",
240+
ipse);
240241
}
241242
}
242243
}
@@ -380,7 +381,7 @@ private byte[] doFinal() throws BadPaddingException,
380381
byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
381382
paddingCopy = RSACore.rsa(decryptBuffer, privateKey, false);
382383
result = padding.unpad(paddingCopy);
383-
if (result == null && !forTlsPremasterSecret) {
384+
if (!forTlsPremasterSecret && result == null) {
384385
throw new BadPaddingException
385386
("Padding error in decryption");
386387
}
@@ -400,6 +401,34 @@ private byte[] doFinal() throws BadPaddingException,
400401
}
401402
}
402403

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

470499
boolean isTlsRsaPremasterSecret =
471500
algorithm.equals("TlsRsaPremasterSecret");
472-
byte[] encoded;
501+
byte[] encoded = null;
473502

474503
update(wrappedKey, 0, wrappedKey.length);
475-
try {
476-
encoded = doFinal();
477-
} catch (BadPaddingException | IllegalBlockSizeException e) {
478-
// BadPaddingException cannot happen for TLS RSA unwrap.
479-
// In that case, padding error is indicated by returning null.
480-
// IllegalBlockSizeException cannot happen in any case,
481-
// because of the length check above.
482-
throw new InvalidKeyException("Unwrapping failed", e);
483-
}
484-
485504
try {
486505
if (isTlsRsaPremasterSecret) {
487506
if (!forTlsPremasterSecret) {
488507
throw new IllegalStateException(
489508
"No TlsRsaPremasterSecretParameterSpec specified");
490509
}
491-
492-
// polish the TLS premaster secret
493-
encoded = KeyUtil.checkTlsPreMasterSecretKey(
494-
((TlsRsaPremasterSecretParameterSpec) spec).getClientVersion(),
495-
((TlsRsaPremasterSecretParameterSpec) spec).getServerVersion(),
496-
random, encoded, encoded == null);
510+
TlsRsaPremasterSecretParameterSpec parameterSpec =
511+
(TlsRsaPremasterSecretParameterSpec) spec;
512+
encoded = doFinalForTls(parameterSpec.getClientVersion(),
513+
parameterSpec.getServerVersion());
514+
} else {
515+
encoded = doFinal();
497516
}
498-
499517
return ConstructKeys.constructKey(encoded, algorithm, type);
518+
519+
} catch (BadPaddingException | IllegalBlockSizeException e) {
520+
// BadPaddingException cannot happen for TLS RSA unwrap.
521+
// Neither padding error nor server version error is indicated
522+
// for TLS, but a fake unwrapped value is returned.
523+
// IllegalBlockSizeException cannot happen in any case,
524+
// because of the length check above.
525+
throw new InvalidKeyException("Unwrapping failed", e);
500526
} finally {
501527
if (encoded != null) {
502528
Arrays.fill(encoded, (byte) 0);
503529
}
504530
}
505531
}
506-
507532
// see JCE spec
508533
protected int engineGetKeySize(Key key) throws InvalidKeyException {
509534
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
@@ -409,7 +409,8 @@ private Manifest getManifestFromReference() throws IOException {
409409
jv = new JarVerifier(manEntry.getName(), b);
410410
} else {
411411
if (JarVerifier.debug != null) {
412-
JarVerifier.debug.println("Multiple MANIFEST.MF found. Treat JAR file as unsigned");
412+
JarVerifier.debug.println(
413+
JarVerifier.MULTIPLE_MANIFEST_WARNING);
413414
}
414415
}
415416
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@ private JarEntry checkManifest(JarEntry e)
151151
jv = new JarVerifier(e.getName(), bytes);
152152
mev = new ManifestEntryVerifier(man, jv.manifestName);
153153
}
154-
return (JarEntry)super.getNextEntry();
154+
JarEntry nextEntry = (JarEntry)super.getNextEntry();
155+
if (nextEntry != null &&
156+
JarFile.MANIFEST_NAME.equalsIgnoreCase(nextEntry.getName())) {
157+
if (JarVerifier.debug != null) {
158+
JarVerifier.debug.println(JarVerifier.MULTIPLE_MANIFEST_WARNING);
159+
}
160+
161+
jv = null;
162+
mev = null;
163+
}
164+
return nextEntry;
155165
}
156166
return e;
157167
}

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: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@
5757
* @since 1.1
5858
*/
5959
public class DeflaterOutputStream extends FilterOutputStream {
60+
61+
/*
62+
* The default size of the output buffer
63+
*/
64+
static final int DEFAULT_BUF_SIZE = 512;
65+
66+
/*
67+
* When calling Deflater.deflate() with Deflater.SYNC_FLUSH or Deflater.FULL_FLUSH,
68+
* the callers are expected to ensure that the size of the buffer is greater than 6.
69+
* This expectation comes from the underlying zlib library which in its zlib.h
70+
* states:
71+
* "If deflate returns with avail_out == 0, this function must be called again
72+
* with the same value of the flush parameter and more output space (updated
73+
* avail_out), until the flush is complete (deflate returns with non-zero
74+
* avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
75+
* avail_out is greater than six when the flush marker begins, in order to avoid
76+
* repeated flush markers upon calling deflate() again when avail_out == 0."
77+
*/
78+
private static final int SYNC_FLUSH_MIN_BUF_SIZE = 7;
79+
6080
/**
6181
* Compressor for this stream.
6282
*/
@@ -152,7 +172,7 @@ public DeflaterOutputStream(OutputStream out, Deflater def, int size) {
152172
public DeflaterOutputStream(OutputStream out,
153173
Deflater def,
154174
boolean syncFlush) {
155-
this(out, def, 512, syncFlush);
175+
this(out, def, DEFAULT_BUF_SIZE, syncFlush);
156176
}
157177

158178

@@ -171,7 +191,7 @@ public DeflaterOutputStream(OutputStream out,
171191
* @param def the compressor ("deflater")
172192
*/
173193
public DeflaterOutputStream(OutputStream out, Deflater def) {
174-
this(out, def, 512, false);
194+
this(out, def, DEFAULT_BUF_SIZE, false);
175195
}
176196

177197
boolean usesDefaultDeflater = false;
@@ -195,7 +215,7 @@ public DeflaterOutputStream(OutputStream out, Deflater def) {
195215
* @since 1.7
196216
*/
197217
public DeflaterOutputStream(OutputStream out, boolean syncFlush) {
198-
this(out, out != null ? new Deflater() : null, 512, syncFlush);
218+
this(out, out != null ? new Deflater() : null, DEFAULT_BUF_SIZE, syncFlush);
199219
usesDefaultDeflater = true;
200220
}
201221

@@ -342,10 +362,16 @@ protected void deflate() throws IOException {
342362
public void flush() throws IOException {
343363
if (syncFlush && !def.finished()) {
344364
int len = 0;
345-
while ((len = def.deflate(buf, 0, buf.length, Deflater.SYNC_FLUSH)) > 0)
346-
{
347-
out.write(buf, 0, len);
348-
if (len < buf.length)
365+
// For SYNC_FLUSH, the Deflater.deflate() expects the callers
366+
// to use a buffer whose length is greater than 6 to avoid
367+
// flush marker (5 bytes) being repeatedly output to the output buffer
368+
// every time it is invoked.
369+
final byte[] flushBuf = buf.length < SYNC_FLUSH_MIN_BUF_SIZE
370+
? new byte[DEFAULT_BUF_SIZE]
371+
: buf;
372+
while ((len = def.deflate(flushBuf, 0, flushBuf.length, Deflater.SYNC_FLUSH)) > 0) {
373+
out.write(flushBuf, 0, len);
374+
if (len < flushBuf.length)
349375
break;
350376
}
351377
}

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

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

119119
/**
@@ -135,7 +135,7 @@ public GZIPOutputStream(OutputStream out) throws IOException {
135135
public GZIPOutputStream(OutputStream out, boolean syncFlush)
136136
throws IOException
137137
{
138-
this(out, 512, syncFlush);
138+
this(out, DeflaterOutputStream.DEFAULT_BUF_SIZE, syncFlush);
139139
}
140140

141141
/**

0 commit comments

Comments
 (0)