Skip to content

Commit 1027f56

Browse files
committed
Add Java 24 compatibility
The SecurityManager is a hollow shell that just throws exceptions in Java 24. We should avoid calling it. This will make tests that call System.exit crash the worker. Bazel is doing the same thing for now, see bazelbuild/bazel#24354 It is often possible to edit code to avoid calling System.exit, and guarding against it requires more effort in Java 24+, likely involving attaching an agent. Until someone actually has that need, it doesn't seem worth doing.
1 parent 11d3b32 commit 1027f56

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

scala/private/rule_impls.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,12 @@ def java_bin_windows(ctx):
222222
def is_windows(ctx):
223223
return ctx.configuration.host_path_separator == ";"
224224

225-
# Return a jvm flag allowing security manager for jdk runtime >= 17
225+
# Return a jvm flag allowing security manager for jdk runtime >= 17 and < 24,
226+
# since 17 requires explicitly enabling the SM while 24 removes support for the SM entirely.
226227
# If no runtime is supplied then runtime is taken from ctx.attr._java_host_runtime
227228
# This must be a runtime used in generated java_binary script (usually workers using SecurityManager)
228229
def allow_security_manager(ctx, runtime = None):
229230
java_runtime = runtime if runtime else ctx.attr._java_host_runtime[java_common.JavaRuntimeInfo]
230231

231232
# Bazel 5.x doesn't have java_runtime.version defined
232-
return ["-Djava.security.manager=allow"] if hasattr(java_runtime, "version") and java_runtime.version >= 17 else []
233+
return ["-Djava.security.manager=allow"] if hasattr(java_runtime, "version") and java_runtime.version >= 17 and java_runtime.version < 24 else []

src/java/io/bazel/rulesscala/worker/Worker.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ public static void workerMain(String workerArgs[], Interface workerInterface) th
5454

5555
/** The main loop for persistent worker processes */
5656
private static void persistentWorkerMain(Interface workerInterface) {
57-
System.setSecurityManager(
58-
new SecurityManager() {
59-
@Override
60-
public void checkPermission(Permission permission) {
61-
Matcher matcher = exitPattern.matcher(permission.getName());
62-
if (matcher.find()) throw new ExitTrapped(Integer.parseInt(matcher.group(1)));
63-
}
64-
});
57+
if (Runtime.version().feature() < 24) {
58+
System.setSecurityManager(
59+
new SecurityManager() {
60+
@Override
61+
public void checkPermission(Permission permission) {
62+
Matcher matcher = exitPattern.matcher(permission.getName());
63+
if (matcher.find()) throw new ExitTrapped(Integer.parseInt(matcher.group(1)));
64+
}
65+
});
66+
}
6567

6668
InputStream stdin = System.in;
6769
PrintStream stdout = System.out;

src/java/io/bazel/rulesscala/worker/WorkerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ public void testBufferWriteReadAndReset() throws Exception {
216216
public static void teardown() {
217217
// Persistent workers install a security manager. We need to
218218
// reset it here so that our own process can exit!
219-
System.setSecurityManager(null);
219+
if (Runtime.version().feature() < 24) {
220+
System.setSecurityManager(null);
221+
}
220222
}
221223

222224
// Copied/modified from Bazel's MoreAsserts

0 commit comments

Comments
 (0)