Skip to content

Commit

Permalink
Merge pull request #628 from ibmruntimes/openj9
Browse files Browse the repository at this point in the history
Merge jdk8u352-b08 to 0.35
  • Loading branch information
JasonFengJ9 committed Oct 19, 2022
2 parents c79b603 + 033865a commit cc86560
Show file tree
Hide file tree
Showing 22 changed files with 844 additions and 280 deletions.
2 changes: 1 addition & 1 deletion closed/openjdk-tag.gmk
Original file line number Diff line number Diff line change
@@ -1 +1 @@
OPENJDK_TAG := jdk8u352-b07
OPENJDK_TAG := jdk8u352-b08
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2022, 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
Expand Down Expand Up @@ -30,6 +30,8 @@
import java.net.SocketException;
import java.net.InetSocketAddress;
import java.nio.channels.DatagramChannel;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Objects;
import java.util.Random;

Expand All @@ -50,6 +52,21 @@ private EphemeralPortRange() {}
static final int RANGE = UPPER - LOWER + 1;
}

private static int findFirstFreePort() {
PrivilegedExceptionAction<DatagramSocket> action = () -> new DatagramSocket(0);
int port;
try {
@SuppressWarnings({"deprecated", "removal"})
DatagramSocket ds = AccessController.doPrivileged(action);
try (DatagramSocket ds1 = ds) {
port = ds1.getLocalPort();
}
} catch (Exception x) {
port = 0;
}
return port;
}

// Records a subset of max {@code capacity} previously used ports
static final class PortHistory {
final int capacity;
Expand All @@ -74,7 +91,10 @@ public boolean contains(int port) {
public boolean add(int port) {
if (ports[index] != 0) { // at max capacity
// remove one port at random and store the new port there
ports[random.nextInt(capacity)] = port;
// don't remove the last port
int remove = random.nextInt(capacity);
if ((remove +1) % capacity == index) remove = index;
ports[index = remove] = port;
} else { // there's a free slot
ports[index] = port;
}
Expand All @@ -90,7 +110,8 @@ public boolean offer(int port) {
}
}

int lastport = 0;
int lastport = findFirstFreePort();
int lastSystemAllocated = lastport;
int suitablePortCount;
int unsuitablePortCount;
final ProtocolFamily family; // null (default) means dual stack
Expand Down Expand Up @@ -147,13 +168,16 @@ public synchronized DatagramSocket open() throws SocketException {
s = openDefault();
lastport = s.getLocalPort();
if (lastseen == 0) {
lastSystemAllocated = lastport;
history.offer(lastport);
return s;
}

thresholdCrossed = suitablePortCount > thresholdCount;
boolean farEnough = Integer.bitCount(lastseen ^ lastport) > BIT_DEVIATION
&& Math.abs(lastport - lastseen) > deviation;
boolean farEnough = farEnough(lastseen);
if (farEnough && lastSystemAllocated > 0) {
farEnough = farEnough(lastSystemAllocated);
}
boolean recycled = history.contains(lastport);
boolean suitable = (thresholdCrossed || farEnough && !recycled);
if (suitable && !recycled) history.add(lastport);
Expand All @@ -168,6 +192,7 @@ public synchronized DatagramSocket open() throws SocketException {
// Either the underlying stack supports random UDP port allocation,
// or the new port is sufficiently distant from last port to make
// it look like it is. Let's use it.
lastSystemAllocated = lastport;
return s;
}

Expand Down Expand Up @@ -218,24 +243,48 @@ synchronized boolean isUndecided() {
&& !isUsingNativePortRandomization();
}

private boolean farEnough(int port) {
return Integer.bitCount(port ^ lastport) > BIT_DEVIATION
&& Math.abs(port - lastport) > deviation;
}

private DatagramSocket openRandom() {
int maxtries = MAX_RANDOM_TRIES;
while (maxtries-- > 0) {
int port = EphemeralPortRange.LOWER
+ random.nextInt(EphemeralPortRange.RANGE);
int port;
boolean suitable;
boolean recycled;
int maxrandom = MAX_RANDOM_TRIES;
do {
port = EphemeralPortRange.LOWER
+ random.nextInt(EphemeralPortRange.RANGE);
recycled = history.contains(port);
suitable = lastport == 0 || (farEnough(port) && !recycled);
} while (maxrandom-- > 0 && !suitable);

// if no suitable port was found, try again
// this means we might call random MAX_RANDOM_TRIES x MAX_RANDOM_TRIES
// times - but that should be OK with MAX_RANDOM_TRIES = 5.
if (!suitable) continue;

try {
if (family != null) {
DatagramChannel c = DatagramChannel.open(family);
try {
DatagramSocket s = c.socket();
s.bind(new InetSocketAddress(port));
lastport = s.getLocalPort();
if (!recycled) history.add(port);
return s;
} catch (Throwable x) {
c.close();
throw x;
}
}
return new DatagramSocket(port);
DatagramSocket s = new DatagramSocket(port);
lastport = s.getLocalPort();
if (!recycled) history.add(port);
return s;
} catch (IOException x) {
// try again until maxtries == 0;
}
Expand Down
7 changes: 4 additions & 3 deletions jdk/src/share/classes/com/sun/security/ntlm/Client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022, 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
Expand Down Expand Up @@ -117,9 +117,10 @@ public byte[] type1() {
* {@code nonce} is null for NTLM v1.
*/
public byte[] type3(byte[] type2, byte[] nonce) throws NTLMException {
if (type2 == null || (v != Version.NTLM && nonce == null)) {
if (type2 == null || (v != Version.NTLM && nonce == null) ||
(nonce != null && nonce.length != 8)) {
throw new NTLMException(NTLMException.PROTOCOL,
"type2 and nonce cannot be null");
"type2 cannot be null, and nonce must be 8-byte long");
}
debug("NTLM Client: Type 2 received\n");
debug(type2);
Expand Down
12 changes: 8 additions & 4 deletions jdk/src/share/classes/com/sun/security/ntlm/NTLM.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,27 @@ void writeBytes(int offset, byte[] data) {
System.arraycopy(data, 0, internal, offset, data.length);
}

void writeSecurityBuffer(int offset, byte[] data) {
void writeSecurityBuffer(int offset, byte[] data) throws NTLMException {
if (data == null) {
writeShort(offset+4, current);
writeInt(offset+4, current);
} else {
int len = data.length;
if (len > 65535) {
throw new NTLMException(NTLMException.INVALID_INPUT,
"Invalid data length " + len);
}
if (current + len > internal.length) {
internal = Arrays.copyOf(internal, current + len + 256);
}
writeShort(offset, len);
writeShort(offset+2, len);
writeShort(offset+4, current);
writeInt(offset+4, current);
System.arraycopy(data, 0, internal, current, len);
current += len;
}
}

void writeSecurityBuffer(int offset, String str, boolean unicode) {
void writeSecurityBuffer(int offset, String str, boolean unicode) throws NTLMException {
try {
writeSecurityBuffer(offset, str == null ? null : str.getBytes(
unicode ? "UnicodeLittleUnmarked" : "ISO8859_1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public final class NTLMException extends GeneralSecurityException {
*/
public final static int PROTOCOL = 6;

/**
* If an invalid input is provided.
*/
public static final int INVALID_INPUT = 7;

private int errorCode;

/**
Expand Down
6 changes: 3 additions & 3 deletions jdk/src/share/classes/com/sun/security/ntlm/Server.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2022, 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
Expand Down Expand Up @@ -86,9 +86,9 @@ public Server(String version, String domain) throws NTLMException {
* {@code nonce} is null.
*/
public byte[] type2(byte[] type1, byte[] nonce) throws NTLMException {
if (nonce == null) {
if (nonce == null || nonce.length != 8) {
throw new NTLMException(NTLMException.PROTOCOL,
"nonce cannot be null");
"nonce must be 8-byte long");
}
debug("NTLM Server: Type 1 received\n");
if (type1 != null) debug(type1);
Expand Down
64 changes: 49 additions & 15 deletions jdk/src/share/classes/java/math/BigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
package java.math;

import static java.math.BigInteger.LONG_MASK;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.StreamCorruptedException;
import java.util.Arrays;

/**
Expand Down Expand Up @@ -975,6 +979,15 @@ public BigDecimal(double val, MathContext mc) {
this.precision = prec;
}

/**
* Accept no subclasses.
*/
private static BigInteger toStrictBigInteger(BigInteger val) {
return (val.getClass() == BigInteger.class) ?
val :
new BigInteger(val.toByteArray().clone());
}

/**
* Translates a {@code BigInteger} into a {@code BigDecimal}.
* The scale of the {@code BigDecimal} is zero.
Expand All @@ -984,8 +997,8 @@ public BigDecimal(double val, MathContext mc) {
*/
public BigDecimal(BigInteger val) {
scale = 0;
intVal = val;
intCompact = compactValFor(val);
intVal = toStrictBigInteger(val);
intCompact = compactValFor(intVal);
}

/**
Expand All @@ -1001,7 +1014,7 @@ public BigDecimal(BigInteger val) {
* @since 1.5
*/
public BigDecimal(BigInteger val, MathContext mc) {
this(val,0,mc);
this(toStrictBigInteger(val), 0, mc);
}

/**
Expand All @@ -1015,8 +1028,8 @@ public BigDecimal(BigInteger val, MathContext mc) {
*/
public BigDecimal(BigInteger unscaledVal, int scale) {
// Negative scales are now allowed
this.intVal = unscaledVal;
this.intCompact = compactValFor(unscaledVal);
this.intVal = toStrictBigInteger(unscaledVal);
this.intCompact = compactValFor(this.intVal);
this.scale = scale;
}

Expand All @@ -1036,6 +1049,7 @@ public BigDecimal(BigInteger unscaledVal, int scale) {
* @since 1.5
*/
public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
unscaledVal = toStrictBigInteger(unscaledVal);
long compactVal = compactValFor(unscaledVal);
int mcp = mc.precision;
int prec = 0;
Expand Down Expand Up @@ -3747,19 +3761,25 @@ private static class UnsafeHolder {
private static final sun.misc.Unsafe unsafe;
private static final long intCompactOffset;
private static final long intValOffset;
private static final long scaleOffset;
static {
try {
unsafe = sun.misc.Unsafe.getUnsafe();
intCompactOffset = unsafe.objectFieldOffset
(BigDecimal.class.getDeclaredField("intCompact"));
intValOffset = unsafe.objectFieldOffset
(BigDecimal.class.getDeclaredField("intVal"));
scaleOffset = unsafe.objectFieldOffset
(BigDecimal.class.getDeclaredField("scale"));
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
static void setIntCompactVolatile(BigDecimal bd, long val) {
unsafe.putLongVolatile(bd, intCompactOffset, val);

static void setIntValAndScaleVolatile(BigDecimal bd, BigInteger intVal, int scale) {
unsafe.putObjectVolatile(bd, intValOffset, intVal);
unsafe.putIntVolatile(bd, scaleOffset, scale);
unsafe.putLongVolatile(bd, intCompactOffset, compactValFor(intVal));
}

static void setIntValVolatile(BigDecimal bd, BigInteger val) {
Expand All @@ -3775,15 +3795,29 @@ static void setIntValVolatile(BigDecimal bd, BigInteger val) {
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in all fields
s.defaultReadObject();
// validate possibly bad fields
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
// prepare to read the fields
ObjectInputStream.GetField fields = s.readFields();
BigInteger serialIntVal = (BigInteger) fields.get("intVal", null);

// Validate field data
if (serialIntVal == null) {
throw new StreamCorruptedException("Null or missing intVal in BigDecimal stream");
}
UnsafeHolder.setIntCompactVolatile(this, compactValFor(intVal));
// Validate provenance of serialIntVal object
serialIntVal = toStrictBigInteger(serialIntVal);

// Any integer value is valid for scale
int serialScale = fields.get("scale", 0);

UnsafeHolder.setIntValAndScaleVolatile(this, serialIntVal, serialScale);
}

/**
* Serialization without data not supported for this class.
*/
private void readObjectNoData()
throws ObjectStreamException {
throw new InvalidObjectException("Deserialized BigDecimal objects need data");
}

/**
Expand Down
Loading

0 comments on commit cc86560

Please sign in to comment.