undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
78
test/jdk/java/net/Socket/asyncClose/AsyncClose.java
Normal file
78
test/jdk/java/net/Socket/asyncClose/AsyncClose.java
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2021, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
import static java.util.concurrent.CompletableFuture.*;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4344135
|
||||
* @library /test/lib
|
||||
* @summary Check that {Socket,ServerSocket,DatagramSocket}.close will
|
||||
* cause any thread blocked on the socket to throw a SocketException.
|
||||
* @run main AsyncClose
|
||||
* @run main/othervm -Djava.net.preferIPv4Stack=true AsyncClose
|
||||
*/
|
||||
|
||||
public class AsyncClose {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
IPSupport.throwSkippedExceptionIfNonOperational();
|
||||
|
||||
AsyncCloseTest tests[] = {
|
||||
new Socket_getInputStream_read(),
|
||||
new Socket_getInputStream_read(20000),
|
||||
new Socket_getOutputStream_write(),
|
||||
new DatagramSocket_receive(),
|
||||
new DatagramSocket_receive(20000),
|
||||
new ServerSocket_accept(),
|
||||
new ServerSocket_accept(20000),
|
||||
};
|
||||
|
||||
int failures = 0;
|
||||
|
||||
List<CompletableFuture<AsyncCloseTest>> cfs = new ArrayList<>();
|
||||
for (AsyncCloseTest test : tests)
|
||||
cfs.add( supplyAsync(() -> test.go()));
|
||||
|
||||
for (CompletableFuture<AsyncCloseTest> cf : cfs) {
|
||||
AsyncCloseTest test = cf.get();
|
||||
|
||||
System.out.println("******************************");
|
||||
System.out.println("Test: " + test.description());
|
||||
if (test.hasPassed()) {
|
||||
System.out.println("Passed.");
|
||||
} else {
|
||||
System.out.println("Failed: " + test.failureReason());
|
||||
failures++;
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
if (failures > 0)
|
||||
throw new Exception(failures + " sub-tests failed - see log.");
|
||||
}
|
||||
}
|
||||
66
test/jdk/java/net/Socket/asyncClose/AsyncCloseTest.java
Normal file
66
test/jdk/java/net/Socket/asyncClose/AsyncCloseTest.java
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2018, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Abstract class representing an asynchronous close test - subclasses
|
||||
* should override description() and go() methods.
|
||||
*/
|
||||
public abstract class AsyncCloseTest {
|
||||
|
||||
public abstract String description();
|
||||
|
||||
public abstract AsyncCloseTest go();
|
||||
|
||||
public synchronized boolean hasPassed() {
|
||||
return passed;
|
||||
}
|
||||
|
||||
protected synchronized AsyncCloseTest passed() {
|
||||
if (failureReason() == null) {
|
||||
passed = true;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected synchronized AsyncCloseTest failed(String r) {
|
||||
passed = false;
|
||||
reason.append(String.format("%n - %s", r));
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized String failureReason() {
|
||||
return reason.length() > 0 ? reason.toString() : null;
|
||||
}
|
||||
|
||||
protected synchronized void closed() {
|
||||
closed = true;
|
||||
}
|
||||
|
||||
protected synchronized boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
|
||||
private boolean passed;
|
||||
private final StringBuilder reason = new StringBuilder();
|
||||
private boolean closed;
|
||||
}
|
||||
84
test/jdk/java/net/Socket/asyncClose/BrokenPipe.java
Normal file
84
test/jdk/java/net/Socket/asyncClose/BrokenPipe.java
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2019, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4511404
|
||||
* @library /test/lib
|
||||
* @summary Check that a broken pipe error doesn't throw an exception
|
||||
* indicating the socket is closed.
|
||||
* @run main BrokenPipe
|
||||
* @run main/othervm -Djava.net.preferIPv4Stack=true BrokenPipe
|
||||
*/
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import jdk.test.lib.net.IPSupport;
|
||||
|
||||
public class BrokenPipe {
|
||||
|
||||
private static class Closer implements Runnable {
|
||||
private final Socket s;
|
||||
|
||||
Closer(Socket s) {
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
/* gives time for 'write' to block */
|
||||
Thread.sleep(5000);
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
IPSupport.throwSkippedExceptionIfNonOperational();
|
||||
|
||||
ServerSocket ss = new ServerSocket(0, 0, InetAddress.getLocalHost());
|
||||
Socket client = new Socket(InetAddress.getLocalHost(),
|
||||
ss.getLocalPort());
|
||||
Socket server = ss.accept();
|
||||
ss.close();
|
||||
new Thread(new Closer(server)).start();
|
||||
|
||||
try {
|
||||
client.getOutputStream().write(new byte[1000000]);
|
||||
} catch (IOException ioe) {
|
||||
/*
|
||||
* Check that the exception text doesn't indicate the
|
||||
* socket is closed. In tiger we should be able to
|
||||
* replace this by catching a more specific exception.
|
||||
*/
|
||||
String text = ioe.getMessage();
|
||||
if (text.toLowerCase().indexOf("closed") >= 0) {
|
||||
throw ioe;
|
||||
}
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
104
test/jdk/java/net/Socket/asyncClose/DatagramSocket_receive.java
Normal file
104
test/jdk/java/net/Socket/asyncClose/DatagramSocket_receive.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2019, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests that a thread blocked in DatagramSocket.receive
|
||||
* throws a SocketException if the socket is asynchronously closed.
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class DatagramSocket_receive extends AsyncCloseTest implements Runnable {
|
||||
private final DatagramSocket s;
|
||||
private final int timeout;
|
||||
private final CountDownLatch latch;
|
||||
private final AtomicBoolean readyToClose = new AtomicBoolean(false);
|
||||
|
||||
public DatagramSocket_receive() throws SocketException {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public DatagramSocket_receive(int timeout) throws SocketException {
|
||||
this.timeout = timeout;
|
||||
latch = new CountDownLatch(1);
|
||||
s = new DatagramSocket(0, InetAddress.getLoopbackAddress());
|
||||
}
|
||||
|
||||
public String description() {
|
||||
String s = "DatagramSocket.receive(DatagramPacket)";
|
||||
if (timeout > 0) {
|
||||
s += " (timeout specified)";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
byte b[] = new byte[1024];
|
||||
DatagramPacket p = new DatagramPacket(b, b.length);
|
||||
if (timeout > 0) {
|
||||
s.setSoTimeout(timeout);
|
||||
}
|
||||
latch.countDown();
|
||||
do {
|
||||
// if readyToClose is still false it means some other
|
||||
// process on the system attempted to send datagram packet:
|
||||
// just ignore it, and go back to accept again.
|
||||
s.receive(p);
|
||||
} while (!readyToClose.get());
|
||||
failed("DatagramSocket.receive(DatagramPacket) returned unexpectedly!!" + " - " + p.getAddress());
|
||||
} catch (SocketException se) {
|
||||
if (latch.getCount() != 1) {
|
||||
closed();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failed(e.getMessage());
|
||||
} finally {
|
||||
if (latch.getCount() == 1) {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncCloseTest go() {
|
||||
try {
|
||||
Thread thr = new Thread(this);
|
||||
thr.start();
|
||||
latch.await();
|
||||
Thread.sleep(5000); //sleep, so receive(DatagramPacket) can block
|
||||
readyToClose.set(true);
|
||||
s.close();
|
||||
thr.join();
|
||||
|
||||
if (isClosed()) {
|
||||
return passed();
|
||||
} else {
|
||||
return failed("DatagramSocket.receive(DatagramPacket) wasn't preempted");
|
||||
}
|
||||
} catch (Exception x) {
|
||||
failed(x.getMessage());
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
244
test/jdk/java/net/Socket/asyncClose/Leaky.java
Normal file
244
test/jdk/java/net/Socket/asyncClose/Leaky.java
Normal file
|
|
@ -0,0 +1,244 @@
|
|||
/*
|
||||
* Copyright (c) 2023, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8278326
|
||||
* @modules java.base/java.net:+open
|
||||
* @run junit Leaky
|
||||
* @summary Test async close when binding, connecting, or reading a socket option
|
||||
*/
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketImpl;
|
||||
import java.net.SocketOption;
|
||||
import java.net.StandardSocketOptions;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class Leaky {
|
||||
private static ScheduledExecutorService executor;
|
||||
private static ServerSocket listener;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws Exception {
|
||||
listener = new ServerSocket();
|
||||
listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
||||
executor = Executors.newScheduledThreadPool(2);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void finish() throws Exception {
|
||||
executor.close();
|
||||
listener.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Race Socket bind and close.
|
||||
*/
|
||||
@RepeatedTest(100)
|
||||
public void raceBindAndClose() throws Exception {
|
||||
Socket socket = new Socket();
|
||||
|
||||
race(socket::close, () -> {
|
||||
try {
|
||||
socket.bind(new InetSocketAddress(0));
|
||||
} catch (IOException ioe) {
|
||||
if (!socket.isClosed()) {
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// check that there isn't an open socket
|
||||
SocketImpl psi = getPlatformSocketImpl(socket);
|
||||
assertFalse(isSocketOpen(psi));
|
||||
}
|
||||
|
||||
/**
|
||||
* Race Socket connect and close.
|
||||
*/
|
||||
@RepeatedTest(100)
|
||||
public void raceConnectAndClose() throws Exception {
|
||||
Socket socket = new Socket();
|
||||
|
||||
race(socket::close, () -> {
|
||||
try {
|
||||
socket.connect(listener.getLocalSocketAddress());
|
||||
// if connected, need to close other end
|
||||
listener.accept().close();
|
||||
} catch (IOException ioe) {
|
||||
if (!socket.isClosed()) {
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// check that there isn't an open socket
|
||||
SocketImpl psi = getPlatformSocketImpl(socket);
|
||||
assertFalse(isSocketOpen(psi));
|
||||
}
|
||||
|
||||
/**
|
||||
* Race Socket getOption and close.
|
||||
*/
|
||||
@RepeatedTest(100)
|
||||
public void raceGetOptionAndClose() throws Exception {
|
||||
Socket socket = new Socket();
|
||||
|
||||
race(socket::close, () -> {
|
||||
try {
|
||||
socket.getOption(StandardSocketOptions.SO_REUSEADDR);
|
||||
} catch (IOException ioe) {
|
||||
if (!socket.isClosed()) {
|
||||
throw ioe;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// check that there isn't an open socket
|
||||
SocketImpl psi = getPlatformSocketImpl(socket);
|
||||
assertFalse(isSocketOpen(psi));
|
||||
}
|
||||
|
||||
/**
|
||||
* A task that may throw an exception.
|
||||
*/
|
||||
private interface ThrowingRunnable {
|
||||
void run() throws Exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Staggers two tasks to execute after random delays.
|
||||
*/
|
||||
private void race(ThrowingRunnable task1, ThrowingRunnable task2) throws Exception {
|
||||
int delay1 = ThreadLocalRandom.current().nextInt(10);
|
||||
int delay2 = ThreadLocalRandom.current().nextInt(10);
|
||||
|
||||
Future<Void> future1 = executor.schedule(() -> {
|
||||
task1.run();
|
||||
return null;
|
||||
}, delay1, TimeUnit.MILLISECONDS);
|
||||
|
||||
Future<Void> future2 = executor.schedule(() -> {
|
||||
task2.run();
|
||||
return null;
|
||||
}, delay2, TimeUnit.MILLISECONDS);
|
||||
|
||||
ExecutionException e = null;
|
||||
try {
|
||||
future1.get();
|
||||
} catch (ExecutionException e1) {
|
||||
e = e1;
|
||||
}
|
||||
try {
|
||||
future2.get();
|
||||
} catch (ExecutionException e2) {
|
||||
if (e == null) {
|
||||
e = e2;
|
||||
} else {
|
||||
e.addSuppressed(e2);
|
||||
}
|
||||
}
|
||||
if (e != null) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the underlying PlatformSocketImpl for the given socket.
|
||||
*/
|
||||
private static SocketImpl getPlatformSocketImpl(Socket s) {
|
||||
SocketImpl si = getSocketImpl(s);
|
||||
return getDelegate(si);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the socket's SocketImpl.
|
||||
*/
|
||||
private static SocketImpl getSocketImpl(Socket s) {
|
||||
try {
|
||||
Field f = Socket.class.getDeclaredField("impl");
|
||||
f.setAccessible(true);
|
||||
return (SocketImpl) f.get(s);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SocketImpl that the given SocketImpl delegates to.
|
||||
*/
|
||||
private static SocketImpl getDelegate(SocketImpl si) {
|
||||
try {
|
||||
Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
|
||||
Field f = clazz.getDeclaredField("delegate");
|
||||
f.setAccessible(true);
|
||||
return (SocketImpl) f.get(si);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the SocketImpl has an open socket.
|
||||
*/
|
||||
private static boolean isSocketOpen(SocketImpl si) throws Exception {
|
||||
// check if SocketImpl.fd is set
|
||||
Field f = SocketImpl.class.getDeclaredField("fd");
|
||||
f.setAccessible(true);
|
||||
FileDescriptor fd = (FileDescriptor) f.get(si);
|
||||
if (fd == null) {
|
||||
return false; // not created
|
||||
}
|
||||
|
||||
// call getOption to get the value of the SO_REUSEADDR socket option
|
||||
Method m = SocketImpl.class.getDeclaredMethod("getOption", SocketOption.class);
|
||||
m.setAccessible(true);
|
||||
try {
|
||||
m.invoke(si, StandardSocketOptions.SO_REUSEADDR);
|
||||
return true; // socket is open
|
||||
} catch (InvocationTargetException e) {
|
||||
if (e.getCause() instanceof IOException) {
|
||||
return false; // assume socket is closed
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
86
test/jdk/java/net/Socket/asyncClose/Race.java
Normal file
86
test/jdk/java/net/Socket/asyncClose/Race.java
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright (c) 2013, 2019, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8006395 8012244
|
||||
* @summary Tests racing code that reads and closes a Socket
|
||||
*/
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.Phaser;
|
||||
|
||||
// Racey test, will not always fail, but if it does then we have a problem.
|
||||
|
||||
public class Race {
|
||||
final static int THREADS = 100;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try (ServerSocket ss = new ServerSocket()) {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
ss.bind(new InetSocketAddress(loopback, 0));
|
||||
final int port = ss.getLocalPort();
|
||||
final Phaser phaser = new Phaser(THREADS + 1);
|
||||
for (int i=0; i<100; i++) {
|
||||
try {
|
||||
final Socket s = new Socket(loopback, port);
|
||||
s.setSoLinger(false, 0);
|
||||
try (Socket sa = ss.accept()) {
|
||||
sa.setSoLinger(false, 0);
|
||||
final InputStream is = s.getInputStream();
|
||||
Thread[] threads = new Thread[THREADS];
|
||||
for (int j=0; j<THREADS; j++) {
|
||||
threads[j] = new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
while (is.read() != -1)
|
||||
Thread.sleep(50);
|
||||
} catch (Exception x) {
|
||||
if (!(x instanceof SocketException
|
||||
&& x.getMessage().equalsIgnoreCase("socket closed")))
|
||||
x.printStackTrace();
|
||||
// ok, expect Socket closed
|
||||
}
|
||||
}};
|
||||
}
|
||||
for (int j=0; j<100; j++)
|
||||
threads[j].start();
|
||||
phaser.arriveAndAwaitAdvance();
|
||||
s.close();
|
||||
for (int j=0; j<100; j++)
|
||||
threads[j].join();
|
||||
}
|
||||
} catch (ConnectException e) {
|
||||
System.err.println("Exception " + e + " Port: " + port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
98
test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java
Normal file
98
test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2019, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests that a thread blocked in ServerSocket.accept
|
||||
* throws a SocketException if the socket is asynchronously closed.
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class ServerSocket_accept extends AsyncCloseTest implements Runnable {
|
||||
private final ServerSocket ss;
|
||||
private final int timeout;
|
||||
private final CountDownLatch latch;
|
||||
private final AtomicBoolean readyToClose = new AtomicBoolean(false);
|
||||
|
||||
public ServerSocket_accept() throws IOException {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public ServerSocket_accept(int timeout) throws IOException {
|
||||
this.timeout = timeout;
|
||||
latch = new CountDownLatch(1);
|
||||
ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
|
||||
}
|
||||
|
||||
public String description() {
|
||||
String s = "ServerSocket.accept()";
|
||||
if (timeout > 0) {
|
||||
s += " (with timeout)";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
latch.countDown();
|
||||
Socket s;
|
||||
// if readyToClose is still false it means some other
|
||||
// process on the system attempted to connect: just
|
||||
// ignore it, and go back to accept again.
|
||||
do {
|
||||
s = ss.accept();
|
||||
} while (!readyToClose.get());
|
||||
failed("ServerSocket.accept() returned unexpectedly!!" + " - " + s);
|
||||
} catch (SocketException se) {
|
||||
closed();
|
||||
} catch (Exception e) {
|
||||
failed(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncCloseTest go(){
|
||||
try {
|
||||
Thread thr = new Thread(this);
|
||||
thr.start();
|
||||
latch.await();
|
||||
Thread.sleep(5000); //sleep, so ServerSocket.accept() can block
|
||||
readyToClose.set(true);
|
||||
ss.close();
|
||||
thr.join();
|
||||
|
||||
if (isClosed()) {
|
||||
return passed();
|
||||
} else {
|
||||
return failed("ServerSocket.accept() wasn't preempted");
|
||||
}
|
||||
} catch (Exception x) {
|
||||
failed(x.getMessage());
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2024, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests that a thread blocked in Socket.getInputStream().read()
|
||||
* throws a SocketException if the socket is asynchronously closed.
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class Socket_getInputStream_read extends AsyncCloseTest implements Runnable {
|
||||
private final Socket s;
|
||||
private final int timeout;
|
||||
private final CountDownLatch latch;
|
||||
|
||||
public Socket_getInputStream_read() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public Socket_getInputStream_read(int timeout) {
|
||||
this.timeout = timeout;
|
||||
latch = new CountDownLatch(1);
|
||||
s = new Socket();
|
||||
}
|
||||
|
||||
public String description() {
|
||||
String s = "Socket.getInputStream().read()";
|
||||
if (timeout > 0) {
|
||||
s += " (with timeout)";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
InputStream in = s.getInputStream();
|
||||
if (timeout > 0) {
|
||||
s.setSoTimeout(timeout);
|
||||
}
|
||||
latch.countDown();
|
||||
int n = in.read();
|
||||
failed("Socket.getInputStream().read() returned unexpectedly, n=" + n);
|
||||
} catch (SocketException se) {
|
||||
if (latch.getCount() != 1) {
|
||||
closed();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failed(e.getMessage());
|
||||
} finally {
|
||||
if (latch.getCount() == 1) {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncCloseTest go() {
|
||||
try {
|
||||
InetAddress lh = InetAddress.getLocalHost();
|
||||
try (ServerSocket ss = new ServerSocket(0, 0, lh)) {
|
||||
s.connect(new InetSocketAddress(lh, ss.getLocalPort()));
|
||||
try (Socket s2 = ss.accept()) {
|
||||
Thread thr = new Thread(this);
|
||||
thr.start();
|
||||
latch.await();
|
||||
Thread.sleep(5000); //sleep, so Socket.getInputStream().read() can block
|
||||
s.close();
|
||||
thr.join();
|
||||
}
|
||||
|
||||
if (isClosed()) {
|
||||
return passed();
|
||||
} else {
|
||||
return failed("Socket.getInputStream().read() wasn't preempted");
|
||||
}
|
||||
}
|
||||
} catch (Exception x) {
|
||||
failed(x.getMessage());
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2024, 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Tests that a thread blocked in Socket.getOutputStream().write()
|
||||
* throws a SocketException if the socket is asynchronously closed.
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class Socket_getOutputStream_write extends AsyncCloseTest implements Runnable {
|
||||
private final Socket s;
|
||||
private final CountDownLatch latch;
|
||||
|
||||
public Socket_getOutputStream_write() {
|
||||
latch = new CountDownLatch(1);
|
||||
s = new Socket();
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return "Socket.getOutputStream().write()";
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream out = s.getOutputStream();
|
||||
byte b[] = new byte[8192];
|
||||
latch.countDown();
|
||||
for (;;) {
|
||||
out.write(b);
|
||||
}
|
||||
} catch (SocketException se) {
|
||||
if (latch.getCount() != 1) {
|
||||
closed();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
failed(e.getMessage());
|
||||
} finally {
|
||||
if (latch.getCount() == 1) {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public AsyncCloseTest go() {
|
||||
try {
|
||||
InetAddress lh = InetAddress.getLocalHost();
|
||||
try (ServerSocket ss = new ServerSocket(0, 0, lh)) {
|
||||
s.connect(new InetSocketAddress(lh, ss.getLocalPort()));
|
||||
try (Socket s2 = ss.accept()) {
|
||||
Thread thr = new Thread(this);
|
||||
thr.start();
|
||||
latch.await();
|
||||
Thread.sleep(1000);
|
||||
s.close();
|
||||
thr.join();
|
||||
}
|
||||
|
||||
if (isClosed()) {
|
||||
return passed();
|
||||
} else {
|
||||
return failed("Socket.getOutputStream().write() wasn't preempted");
|
||||
}
|
||||
}
|
||||
} catch (Exception x) {
|
||||
failed(x.getMessage());
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue