Skip to content

Commit 7b2a12a

Browse files
authored
Implementing the correct exit functions (Runtime) (#118657) (#118680)
1 parent 461ea39 commit 7b2a12a

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
import java.net.URLStreamHandlerFactory;
1414

1515
public interface EntitlementChecker {
16-
void check$java_lang_System$exit(Class<?> callerClass, int status);
16+
17+
// Exit the JVM process
18+
void check$$exit(Class<?> callerClass, Runtime runtime, int status);
19+
20+
void check$$halt(Class<?> callerClass, Runtime runtime, int status);
1721

1822
// URLClassLoader ctor
1923
void check$java_net_URLClassLoader$(Class<?> callerClass, URL[] urls);

libs/entitlement/qa/common/src/main/java/org/elasticsearch/entitlement/qa/common/RestEntitlementsCheckAction.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ static CheckAction serverAndPlugin(Runnable action) {
4747
}
4848

4949
private static final Map<String, CheckAction> checkActions = Map.ofEntries(
50-
entry("system_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::systemExit)),
50+
entry("runtime_exit", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeExit)),
51+
entry("runtime_halt", CheckAction.serverOnly(RestEntitlementsCheckAction::runtimeHalt)),
5152
entry("create_classloader", CheckAction.serverAndPlugin(RestEntitlementsCheckAction::createClassLoader))
5253
);
5354

54-
@SuppressForbidden(reason = "Specifically testing System.exit")
55-
private static void systemExit() {
56-
logger.info("Calling System.exit(123);");
57-
System.exit(123);
55+
@SuppressForbidden(reason = "Specifically testing Runtime.exit")
56+
private static void runtimeExit() {
57+
logger.info("Calling Runtime.exit;");
58+
Runtime.getRuntime().exit(123);
59+
}
60+
61+
@SuppressForbidden(reason = "Specifically testing Runtime.halt")
62+
private static void runtimeHalt() {
63+
logger.info("Calling Runtime.halt;");
64+
Runtime.getRuntime().halt(123);
5865
}
5966

6067
private static void createClassLoader() {

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/api/ElasticsearchEntitlementChecker.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public ElasticsearchEntitlementChecker(PolicyManager policyManager) {
2828
}
2929

3030
@Override
31-
public void check$java_lang_System$exit(Class<?> callerClass, int status) {
31+
public void check$$exit(Class<?> callerClass, Runtime runtime, int status) {
32+
policyManager.checkExitVM(callerClass);
33+
}
34+
35+
@Override
36+
public void check$$halt(Class<?> callerClass, Runtime runtime, int status) {
3237
policyManager.checkExitVM(callerClass);
3338
}
3439

libs/entitlement/src/main23/java/org/elasticsearch/entitlement/runtime/api/Java23ElasticsearchEntitlementChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public Java23ElasticsearchEntitlementChecker(PolicyManager policyManager) {
1919
}
2020

2121
@Override
22-
public void check$java_lang_System$exit(Class<?> callerClass, int status) {
22+
public void check$$exit(Class<?> callerClass, Runtime runtime, int status) {
2323
// TODO: this is just an example, we shouldn't really override a method implemented in the superclass
24-
super.check$java_lang_System$exit(callerClass, status);
24+
super.check$$exit(callerClass, runtime, status);
2525
}
2626
}

0 commit comments

Comments
 (0)