Skip to content

Commit 23901f3

Browse files
committed
8333680: com/sun/tools/attach/BasicTests.java fails with "SocketException: Permission denied: connect"
Backport-of: 17bd483ff01e463cef45824f0c1296a8f3e782c8
1 parent 6d38efa commit 23901f3

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

test/jdk/com/sun/tools/attach/Agent.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 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
@@ -28,6 +28,7 @@
2828
* the given port.
2929
*/
3030
import java.net.Socket;
31+
import java.net.InetAddress;
3132
import java.net.InetSocketAddress;
3233
import java.io.IOException;
3334

@@ -38,7 +39,7 @@ public static void agentmain(String args) throws IOException {
3839
int port = Integer.parseInt(args);
3940
System.out.println("Agent connecting back to Tool....");
4041
Socket s = new Socket();
41-
s.connect( new InetSocketAddress(port) );
42+
s.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
4243
System.out.println("Agent connected to Tool.");
4344
s.close();
4445
}

test/jdk/com/sun/tools/attach/BasicTests.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.io.File;
2525
import java.io.IOException;
26+
import java.net.InetAddress;
27+
import java.net.InetSocketAddress;
2628
import java.net.ServerSocket;
2729
import java.net.Socket;
2830
import java.util.List;
@@ -213,7 +215,8 @@ public static void main(String args[]) throws Exception {
213215

214216
System.out.println(" - Test: End-to-end connection with agent");
215217

216-
ServerSocket ss = new ServerSocket(0);
218+
ServerSocket ss = new ServerSocket();
219+
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
217220
int port = ss.getLocalPort();
218221

219222
System.out.println(" - Loading Agent.jar into target VM ...");
@@ -231,7 +234,8 @@ public static void main(String args[]) throws Exception {
231234

232235
System.out.println(" - Test: End-to-end connection with RedefineAgent");
233236

234-
ServerSocket ss2 = new ServerSocket(0);
237+
ServerSocket ss2 = new ServerSocket();
238+
ss2.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
235239
int port2 = ss2.getLocalPort();
236240

237241
System.out.println(" - Loading RedefineAgent.jar into target VM ...");

test/jdk/com/sun/tools/attach/RedefineAgent.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 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
@@ -32,6 +32,7 @@
3232
* 6446941 java.lang.instrument: multiple agent attach fails (first agent chooses capabilities)
3333
*/
3434
import java.net.Socket;
35+
import java.net.InetAddress;
3536
import java.net.InetSocketAddress;
3637
import java.io.IOException;
3738
import java.util.Arrays;
@@ -104,7 +105,7 @@ public static void agentmain(String args, Instrumentation inst) throws Exception
104105
int port = Integer.parseInt(args);
105106
System.out.println("RedefineAgent connecting back to Tool....");
106107
Socket s = new Socket();
107-
s.connect( new InetSocketAddress(port) );
108+
s.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port));
108109
System.out.println("RedefineAgent connected to Tool.");
109110

110111
testRedefine(inst);

0 commit comments

Comments
 (0)