Skip to content

Commit eb835e0

Browse files
committed
8366040: Change URL.lookupViaProviders to use ScopedValue to detect recursive lookup
Reviewed-by: alanb, dfuchs
1 parent eb729f0 commit eb835e0

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

src/java.base/share/classes/java/net/URL.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
import jdk.internal.access.JavaNetURLAccess;
4343
import jdk.internal.access.SharedSecrets;
44-
import jdk.internal.misc.ThreadTracker;
4544
import jdk.internal.misc.VM;
4645
import jdk.internal.vm.annotation.AOTRuntimeSetup;
4746
import jdk.internal.vm.annotation.AOTSafeClassInitializer;
@@ -1394,24 +1393,13 @@ private static URLStreamHandler lookupViaProperty(String protocol) {
13941393
return handler;
13951394
}
13961395

1397-
private static class ThreadTrackHolder {
1398-
static final ThreadTracker TRACKER = new ThreadTracker();
1399-
}
1400-
1401-
private static Object tryBeginLookup() {
1402-
return ThreadTrackHolder.TRACKER.tryBegin();
1403-
}
1404-
1405-
private static void endLookup(Object key) {
1406-
ThreadTrackHolder.TRACKER.end(key);
1407-
}
1396+
private static final ScopedValue<Boolean> IN_LOOKUP = ScopedValue.newInstance();
14081397

14091398
private static URLStreamHandler lookupViaProviders(final String protocol) {
1410-
Object key = tryBeginLookup();
1411-
if (key == null) {
1399+
if (IN_LOOKUP.isBound()) {
14121400
throw new Error("Circular loading of URL stream handler providers detected");
14131401
}
1414-
try {
1402+
return ScopedValue.where(IN_LOOKUP, true).call(() -> {
14151403
final ClassLoader cl = ClassLoader.getSystemClassLoader();
14161404
final ServiceLoader<URLStreamHandlerProvider> sl =
14171405
ServiceLoader.load(URLStreamHandlerProvider.class, cl);
@@ -1423,9 +1411,7 @@ private static URLStreamHandler lookupViaProviders(final String protocol) {
14231411
return h;
14241412
}
14251413
return null;
1426-
} finally {
1427-
endLookup(key);
1428-
}
1414+
});
14291415
}
14301416

14311417
/**

test/jdk/java/net/spi/URLStreamHandlerProvider/Basic.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, 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
@@ -77,6 +77,7 @@ public static void main(String[] args) throws Throwable {
7777
viaProvider("bert", KNOWN);
7878
viaBadProvider("tom", SCE);
7979
viaBadProvider("jerry", SCE);
80+
viaCircularProvider("circular", CIRCULAR);
8081
}
8182

8283
private static String withoutWarning(String in) {
@@ -99,6 +100,12 @@ private static String withoutWarning(String in) {
99100
throw new RuntimeException("exitValue: "+ r.exitValue + ", output:[" +r.output +"]");
100101
}
101102
};
103+
static final Consumer<Result> CIRCULAR = r -> {
104+
if (r.exitValue == 0 ||
105+
!r.output.contains("Circular loading of URL stream handler providers detected")) {
106+
throw new RuntimeException("exitValue: " + r.exitValue + ", output:[" + r.output + "]");
107+
}
108+
};
102109

103110
static void unknownProtocol(String protocol, Consumer<Result> resultChecker) {
104111
System.out.println("\nTesting " + protocol);
@@ -125,6 +132,15 @@ static void viaBadProvider(String protocol, Consumer<Result> resultChecker,
125132
sysProps);
126133
}
127134

135+
static void viaCircularProvider(String protocol, Consumer<Result> resultChecker,
136+
String... sysProps)
137+
throws Exception
138+
{
139+
viaProviderWithTemplate(protocol, resultChecker,
140+
TEST_SRC.resolve("circular.provider.template"),
141+
sysProps);
142+
}
143+
128144
static void viaProviderWithTemplate(String protocol,
129145
Consumer<Result> resultChecker,
130146
Path template, String... sysProps)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package $package;
25+
26+
import java.io.IOException;
27+
import java.net.URI;
28+
import java.net.URL;
29+
import java.net.URLConnection;
30+
import java.net.URLStreamHandler;
31+
import java.net.spi.URLStreamHandlerProvider;
32+
33+
public final class Provider extends URLStreamHandlerProvider {
34+
35+
private static final String PROTOCOL = "$protocol";
36+
37+
@Override
38+
public URLStreamHandler createURLStreamHandler(String protocol) {
39+
try {
40+
// Trigger circular lookup
41+
URI.create("bogus://path/to/nothing").toURL();
42+
} catch (Exception exception) {
43+
throw new RuntimeException(exception);
44+
}
45+
throw new AssertionError("Should not have reached here!");
46+
}
47+
48+
}

0 commit comments

Comments
 (0)