Skip to content

HBASE-27594 [JDK17] SecurityManager is deprecated since JDK17 #6932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.hadoop.hbase.logging.Log4jUtils;
import org.apache.hadoop.hbase.util.AbstractHBaseTool;
import org.apache.hadoop.hbase.util.CommonFSUtils;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.apache.hadoop.util.ToolRunner;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
Expand Down Expand Up @@ -178,7 +179,7 @@ public static void main(String[] args) throws Exception {
URI defaultFs = hbasedir.getFileSystem(conf).getUri();
CommonFSUtils.setFsDefault(conf, new Path(defaultFs));
int ret = ToolRunner.run(conf, new BackupDriver(), args);
System.exit(ret);
ExitHandler.getInstance().exit(ret);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.hadoop.hbase.logging.Log4jUtils;
import org.apache.hadoop.hbase.util.AbstractHBaseTool;
import org.apache.hadoop.hbase.util.CommonFSUtils;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.apache.hadoop.util.ToolRunner;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
Expand Down Expand Up @@ -222,7 +223,7 @@ public static void main(String[] args) throws Exception {
URI defaultFs = hbasedir.getFileSystem(conf).getUri();
CommonFSUtils.setFsDefault(conf, new Path(defaultFs));
int ret = ToolRunner.run(conf, new RestoreDriver(), args);
System.exit(ret);
ExitHandler.getInstance().exit(ret);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.snapshot.SnapshotRegionLocator;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.apache.hadoop.hbase.util.MapReduceExtendedCell;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
Expand Down Expand Up @@ -159,7 +160,7 @@ private void usage(final String errorMsg) {
*/
public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new MapReduceHFileSplitterJob(HBaseConfiguration.create()), args);
System.exit(ret);
ExitHandler.getInstance().exit(ret);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions hbase-checkstyle/src/main/resources/hbase/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@

<!-- Make the @SuppressWarnings annotations available to Checkstyle -->
<module name="SuppressWarningsHolder"/>

<!-- Custom check to detect System.exit calls -->
Copy link
Contributor Author

@NihalJain NihalJain Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: added this checkstyle rule to raise checkstyle an issue if anyone tries to directly use System.exit

<module name="RegexpSinglelineJava">
<property name="format" value="System\.exit\(.*\);"/>
<property name="message" value="Direct usage of System.exit(status) is not allowed, use ExitHandler.getInstance().exit(status) instead."/>
<property name="ignoreComments" value="true"/>
</module>
</module>

<!-- Size Violation Checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.ClassSize;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.DefaultCodec;
import org.apache.hadoop.io.compress.GzipCodec;
Expand Down Expand Up @@ -143,7 +144,7 @@ private static void usage(final int errorCode) {
System.out.println(" --count Count of Cells");
System.out.println(" --size Size of Cell values");
System.out.println("Example: IPCUtil --count=1024 --size=1024");
System.exit(errorCode);
ExitHandler.getInstance().exit(errorCode);
}

private static void timerTests(final CellBlockBuilder builder, final int count, final int size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ protected void doStaticMain(String args[]) {
LOG.error("Error running command-line tool", ex);
ret = EXIT_FAILURE;
}
System.exit(ret);
ExitHandler.getInstance().exit(ret);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.util;

import org.apache.yetus.audience.InterfaceAudience;

/**
* A utility class to wrap system exit calls. We use this to allow for testing and to provide a
* consistent way to handle system exit across the codebase. NOTE: We should avoid calling
* System.exit directly in the codebase. Instead, we should use this class to handle system exit
* calls.
*/
@InterfaceAudience.Private
public class ExitHandler {
private static ExitHandler instance = new ExitHandler();

public static ExitHandler getInstance() {
Copy link
Contributor Author

@NihalJain NihalJain Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I wrap into Double-Checked Locking. seems overkill for this use case. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some kind of semaphore to ensure that only one thread in a JVM holds a test instance ?
That would also require releasing each test ExitHandler instance explicitily.

return instance;
}

public static void setInstance(ExitHandler handler) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should i let this be called from only tests?

instance = handler;
}

/**
* This method is called when the system needs to exit. By default, it calls System.exit.
* Subclasses can override this method to provide custom exit behavior.
* @param status the exit status
*/
@SuppressWarnings("checkstyle:RegexpSinglelineJava")
public void exit(int status) {
System.exit(status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Hash getInstance() {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: JenkinsHash filename");
System.exit(-1);
ExitHandler.getInstance().exit(-1);
}
FileInputStream in = new FileInputStream(args[0]);
byte[] bytes = new byte[512];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public static void main(String[] args) {

if (longSet.contains(randLong)) {
System.err.println("Conflict! count=" + cnt);
System.exit(1);
ExitHandler.getInstance().exit(1);
}

if (cnt % precision == 0) {
if (!longSet.add(randLong)) {
System.err.println("Conflict! count=" + cnt);
System.exit(1);
ExitHandler.getInstance().exit(1);
}

if (cnt % reportPeriod == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,46 @@
*/
package org.apache.hadoop.hbase;

import org.apache.hadoop.hbase.util.ExitHandler;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/*
* Test rules to prevent System.exit or Runtime.halt from exiting
* the JVM - instead an exception is thrown.
* */
/**
* Test rules to prevent Exit calls in tests. This is useful to prevent tests from exiting the JVM
* and to ensure that tests can be run in a controlled environment. This rule replaces the default
* ExitHandler with a mock handler that throws an exception when exit is called.
*/
public class SystemExitRule implements TestRule {
final static SecurityManager securityManager = new TestSecurityManager();
private static final ExitHandler mockHandler = new ExitHandler() {
@Override
public void exit(int status) {
throw new SystemExitInTestException();
}
};

@Override
public Statement apply(final Statement s, Description d) {
return new Statement() {
@Override
public void evaluate() throws Throwable {

ExitHandler originalHandler = ExitHandler.getInstance();
try {
forbidSystemExitCall();
s.evaluate();
} finally {
System.setSecurityManager(null);
ExitHandler.setInstance(originalHandler);
}
}

};
};
}

// Exiting the JVM is not allowed in tests and this exception is thrown instead
// when it is done
public static class SystemExitInTestException extends SecurityException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still throw SecurityException?

}

private static void forbidSystemExitCall() {
System.setSecurityManager(securityManager);
ExitHandler.setInstance(mockHandler);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
Expand All @@ -32,7 +33,7 @@ public class TestSystemExitInTest {

@Test(expected = SystemExitRule.SystemExitInTestException.class)
public void testSystemExit() {
System.exit(1);
ExitHandler.getInstance().exit(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.hadoop.hbase.io.compress.CompressionTestBase;
import org.apache.hadoop.hbase.io.compress.DictionaryCache;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.hbase.util.ExitHandler;
import org.apache.hadoop.hbase.util.RandomDistribution;
import org.junit.BeforeClass;
import org.junit.ClassRule;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void main(String[] args) throws IOException {
// zstd --train -B1024 -o <dictionary_file> <input_file>
if (args.length < 1) {
System.err.println("Usage: TestZstdCodec <outFile>");
System.exit(-1);
ExitHandler.getInstance().exit(-1);
}
final RandomDistribution.DiscreteRNG rng =
new RandomDistribution.Zipf(new Random(), 0, Byte.MAX_VALUE, 2);
Expand Down
Loading