Skip to content

Commit 7217cb7

Browse files
mkartashevMichael-Mc-Mahon
authored andcommitted
8274883: (se) Selector.open throws IAE when the default file system provider is changed to a custom provider
Reviewed-by: alanb, michaelm
1 parent 7ea4b19 commit 7217cb7

File tree

4 files changed

+256
-5
lines changed

4 files changed

+256
-5
lines changed

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,11 +700,11 @@ public SocketChannel bind(SocketAddress local) throws IOException {
700700
private SocketAddress unixBind(SocketAddress local) throws IOException {
701701
UnixDomainSockets.checkPermission();
702702
if (local == null) {
703-
return UnixDomainSockets.UNNAMED;
703+
return UnixDomainSockets.unnamed();
704704
} else {
705705
Path path = UnixDomainSockets.checkAddress(local).getPath();
706706
if (path.toString().isEmpty()) {
707-
return UnixDomainSockets.UNNAMED;
707+
return UnixDomainSockets.unnamed();
708708
} else {
709709
// bind to non-empty path
710710
UnixDomainSockets.bind(fd, path);

src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
class UnixDomainSockets {
4545
private UnixDomainSockets() { }
4646

47-
static final UnixDomainSocketAddress UNNAMED = UnixDomainSocketAddress.of("");
47+
private static class UnnamedHolder {
48+
static final UnixDomainSocketAddress UNNAMED = UnixDomainSocketAddress.of("");
49+
}
4850

4951
private static final boolean supported;
5052

@@ -71,7 +73,7 @@ static UnixDomainSocketAddress getRevealedLocalAddress(SocketAddress sa) {
7173
// Security check passed
7274
} catch (SecurityException e) {
7375
// Return unnamed address only if security check fails
74-
addr = UNNAMED;
76+
addr = unnamed();
7577
}
7678
return addr;
7779
}
@@ -133,7 +135,11 @@ static UnixDomainSocketAddress generateTempName() throws IOException {
133135
throw new BindException("Could not locate temporary directory for sockets");
134136
int rnd = random.nextInt(Integer.MAX_VALUE);
135137
try {
136-
Path path = Path.of(dir, "socket_" + rnd);
138+
final Path path = Path.of(dir, "socket_" + rnd);
139+
if (path.getFileSystem().provider() != sun.nio.fs.DefaultFileSystemProvider.instance()) {
140+
throw new UnsupportedOperationException(
141+
"Unix Domain Sockets not supported on non-default file system");
142+
}
137143
return UnixDomainSocketAddress.of(path);
138144
} catch (InvalidPathException e) {
139145
throw new BindException("Invalid temporary directory");
@@ -160,6 +166,10 @@ static int accept(FileDescriptor fd, FileDescriptor newfd, String[] paths)
160166
return n;
161167
}
162168

169+
static UnixDomainSocketAddress unnamed() {
170+
return UnnamedHolder.UNNAMED;
171+
}
172+
163173
private static native boolean init();
164174

165175
private static native int socket0() throws IOException;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2021, JetBrains s.r.o.. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*/
24+
25+
/**
26+
* @test
27+
* @summary Verifies that an attempt to call Selector.open() on a non-default
28+
* file system succeeds.
29+
* @build CustomFileSystem CustomFileSystemProvider
30+
* @run main/othervm -Djava.nio.file.spi.DefaultFileSystemProvider=CustomFileSystemProvider CustomFileSystem
31+
*/
32+
33+
public class CustomFileSystem {
34+
public static void main(String args[]) throws java.io.IOException {
35+
java.nio.channels.Selector.open();
36+
}
37+
}
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2021, JetBrains s.r.o.. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation.
9+
*
10+
* This code is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* version 2 for more details (a copy is included in the LICENSE file that
14+
* accompanied this code).
15+
*
16+
* You should have received a copy of the GNU General Public License version
17+
* 2 along with this work; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*
20+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21+
* or visit www.oracle.com if you need additional information or have any
22+
* questions.
23+
*/
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.net.URI;
28+
import java.nio.channels.FileChannel;
29+
import java.nio.channels.SeekableByteChannel;
30+
import java.nio.file.AccessMode;
31+
import java.nio.file.CopyOption;
32+
import java.nio.file.DirectoryStream;
33+
import java.nio.file.FileStore;
34+
import java.nio.file.FileSystem;
35+
import java.nio.file.LinkOption;
36+
import java.nio.file.OpenOption;
37+
import java.nio.file.Path;
38+
import java.nio.file.ReadOnlyFileSystemException;
39+
import java.nio.file.attribute.BasicFileAttributes;
40+
import java.nio.file.attribute.FileAttribute;
41+
import java.nio.file.attribute.FileAttributeView;
42+
import java.nio.file.attribute.UserPrincipalLookupService;
43+
import java.nio.file.spi.FileSystemProvider;
44+
import java.util.Collections;
45+
import java.util.Iterator;
46+
import java.util.Map;
47+
import java.util.Set;
48+
49+
public class CustomFileSystemProvider extends FileSystemProvider {
50+
51+
private final FileSystemProvider defaultProvider;
52+
53+
public CustomFileSystemProvider(FileSystemProvider defaultProvider) {
54+
this.defaultProvider = defaultProvider;
55+
}
56+
57+
FileSystemProvider defaultProvider() {
58+
return defaultProvider;
59+
}
60+
61+
@Override
62+
public String getScheme() {
63+
return "file";
64+
}
65+
66+
@Override
67+
public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException {
68+
return defaultProvider.newFileSystem(uri, env);
69+
}
70+
71+
@Override
72+
public FileSystem getFileSystem(URI uri) {
73+
return defaultProvider.getFileSystem(uri);
74+
}
75+
76+
@Override
77+
public Path getPath(URI uri) {
78+
return defaultProvider.getPath(uri);
79+
}
80+
81+
@Override
82+
public void setAttribute(Path file, String attribute, Object value,
83+
LinkOption... options)
84+
throws IOException
85+
{
86+
throw new RuntimeException("not implemented");
87+
}
88+
89+
@Override
90+
public Map<String,Object> readAttributes(Path file, String attributes,
91+
LinkOption... options)
92+
throws IOException
93+
{
94+
throw new RuntimeException("not implemented");
95+
}
96+
97+
@Override
98+
public <A extends BasicFileAttributes> A readAttributes(Path file,
99+
Class<A> type,
100+
LinkOption... options)
101+
throws IOException
102+
{
103+
throw new RuntimeException("not implemented");
104+
}
105+
106+
@Override
107+
public <V extends FileAttributeView> V getFileAttributeView(Path file,
108+
Class<V> type,
109+
LinkOption... options)
110+
{
111+
throw new RuntimeException("not implemented");
112+
}
113+
114+
@Override
115+
public boolean isHidden(Path file) throws IOException {
116+
throw new ReadOnlyFileSystemException();
117+
}
118+
119+
@Override
120+
public boolean isSameFile(Path file, Path other) throws IOException {
121+
throw new RuntimeException("not implemented");
122+
}
123+
124+
@Override
125+
public void checkAccess(Path file, AccessMode... modes)
126+
throws IOException
127+
{
128+
throw new RuntimeException("not implemented");
129+
}
130+
131+
@Override
132+
public void copy(Path source, Path target, CopyOption... options)
133+
throws IOException
134+
{
135+
throw new RuntimeException("not implemented");
136+
}
137+
138+
@Override
139+
public void move(Path source, Path target, CopyOption... options)
140+
throws IOException
141+
{
142+
throw new RuntimeException("not implemented");
143+
}
144+
145+
@Override
146+
public void delete(Path file) throws IOException {
147+
throw new RuntimeException("not implemented");
148+
}
149+
150+
@Override
151+
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
152+
throws IOException
153+
{
154+
throw new RuntimeException("not implemented");
155+
}
156+
157+
@Override
158+
public void createLink(Path link, Path existing) throws IOException {
159+
throw new RuntimeException("not implemented");
160+
}
161+
162+
@Override
163+
public Path readSymbolicLink(Path link) throws IOException {
164+
throw new RuntimeException("not implemented");
165+
}
166+
167+
@Override
168+
public void createDirectory(Path dir, FileAttribute<?>... attrs)
169+
throws IOException
170+
{
171+
throw new RuntimeException("not implemented");
172+
}
173+
174+
@Override
175+
public DirectoryStream<Path> newDirectoryStream(Path dir,
176+
DirectoryStream.Filter<? super Path> filter)
177+
throws IOException
178+
{
179+
throw new RuntimeException("not implemented");
180+
}
181+
182+
@Override
183+
public SeekableByteChannel newByteChannel(Path file,
184+
Set<? extends OpenOption> options,
185+
FileAttribute<?>... attrs)
186+
throws IOException
187+
{
188+
throw new RuntimeException("not implemented");
189+
}
190+
191+
@Override
192+
public FileChannel newFileChannel(Path file,
193+
Set<? extends OpenOption> options,
194+
FileAttribute<?>... attrs)
195+
throws IOException
196+
{
197+
throw new RuntimeException("not implemented");
198+
}
199+
200+
@Override
201+
public FileStore getFileStore(Path file) throws IOException {
202+
throw new RuntimeException("not implemented");
203+
}
204+
}

0 commit comments

Comments
 (0)