Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
When certain kinds of errors are detected, such as irrecoverable logic errors, rather than risk data corruption by continuing to execute in an indeterminate state, the appropriate strategy may be for the system to quickly shut down, allowing the operator to start it afresh in a determinate state.
\[[ISO/IEC TR 24772:2010|AA. Bibliography#ISO/IEC TR 24772-2010]\] Section 6.4746, "REUTermination TerminationStrategy strategy[REU]," says:

When a fault is detected, there are many ways in which a system can react. The quickest and most noticeable way is to fail hard, also known as fail fast or fail stop. The reaction to a detected fault is to immediately halt the system. Alternatively, the reaction to a detected fault could be to fail soft. The system would keep working with the faults present, but the performance of the system would be degraded. Systems used in a high availability environment such as telephone switching centers, e-?commerce, etc. or other "always available" applications would likely use a fail soft approach. What is actually done in a fail soft approach can vary depending on whether the system is used for safety critical or security critical purposes. For fail?safe systems, such as flight controllers, traffic signals, or medical monitoring systems, there would be no effort to meet normal operational requirements, but rather to limit the damage or danger caused by the fault. A system that fails securely, such as cryptologic systems, would maintain maximum security when a fault is detected, possibly through a denial of service.

...

The reaction to a fault in a system can depend on the criticality of the part in which the fault originates. When a program consists of several tasks, the tasks each task may be critical, or not. If a task is critical, it may or may not be restartable by the rest of the program. Ideally, a task which that detects a fault within itself should be able to halt leaving its resources available for use by the rest of the program, halt clearing away its resources, or halt the entire program. The latency of any such communication, task termination and whether other tasks can ignore such a communication, termination signals should be clearly specified. Having inconsistent reactions to a fault , such as the fault reaction to a crypto fault, can potentially be a vulnerability.

Java provides two options for program termination: Runtime.exit() (this is equivalent to System.exit()) and Runtime.halt().

Runtime.exit()

Wiki Markup
{{Runtime.exit()}} is the typical way of exiting a program. According to the Java API \[[API 06|AA. Bibliography#API 06]\]:

Terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

The virtual machine's shutdown sequence consists of two phases. In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish. In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is done performed the virtual machine halts.

If this method is invoked after the virtual machine has begun its shutdown sequence then if shutdown hooks are being run this method will block indefinitely. If shutdown hooks have already been run and on-exit finalization has been enabled then this method halts the virtual machine with the given status code if the status is nonzero; otherwise, it blocks indefinitely.

The System.exit method is the conventional and convenient means of invoking this method.

The Runtime.addShutdownHook() method can be used to customize Runtime.exit() to perform additional actions at program termination.
This method takes a single Thread, which must be initalized initialized but unstarted. Then, when the JVM begins to shut down, the thread will be run. Since the JVM usually has a fixed time to shut down, these threads should not be long-running and should not attempt user interaction.

Runtime.halt()

Wiki Markup
{{Runtime.halt()}} works similarly but does NOT run shutdown hooks or
finalizers:
 finalizers. According to the Java API \[[API 06|AA. Bibliography#API 06]\]

Forcibly terminates the currently running Java virtual machine. This method never returns normally.
This method should be used with extreme caution. Unlike the exit method, this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated then this method does not wait for any running shutdown hooks or finalizers to finish their work.

...

This example creates a new file, outputs some text to it, and abruptly exits using Runtime.exit(). Consequently, the file is closed without the text actually being written to it.

Code Block
bgColor#ffcccc
public class CreateFile {
  public static void main(String[] args) throws FileNotFoundException {
    final PrintStream out = new PrintStream(new BufferedOutputStream(
                                        new FileOutputStream("foo.txt")));
    out.println("hello");
    Runtime.getRuntime().exit(1);
  }
}

...

This noncompliant code example calls Runtime.halt() instead of Runtime.exit(). The Runtime.halt() method stops the JVM without invoking any shutdown hooks; consequently the file is not properly written to or nor closed.

Code Block
bgColor#ffcccc
public class CreateFile {
  public static void main(String[] args) throws FileNotFoundException {
    final PrintStream out = new PrintStream(new BufferedOutputStream(
                                        new FileOutputStream("foo.txt")));
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
          out.close();
        }
      }));
    out.println("hello");
    Runtime.getRuntime().halt(1);
  }
}

...

C Secure Coding Standard

ERR04-C. Choose an appropriate termination strategy

C++ Secure Coding Standard

ERR04-CPP. Choose an appropriate termination strategy

ISO/IEC TR 24772:2010

"Termination Strategy REU"

MITRE CWE

CWE ID 705, "Incorrect Control Flow Scoping"

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="42c0f36b544c0df4-f823b597-494c40dc-af8bb65a-6bd94c7b3969cdb871ebbe74"><ac:plain-text-body><![CDATA[

[[MITRE 07API 06

AA. Bibliography#MITRE 07Bibliography#API 06]]

[CWE ID 705Class Runtime

http://cwedownload.mitre.org/data/definitions/705.html], "Incorrect Control Flow Scoping" oracle.com/javase/6/docs/api/java/lang/Runtime.html]

]]></ac:plain-text-body></ac:structured-macro>

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="19306bd6c01e527a-6fa36047-4dfb4118-a5528515-3cd4af37b39df42523815bb6"><ac:plain-text-body><![CDATA[

[[ISO/IEC PDTR TR 24772:2010

AA. Bibliography#ISO/IEC PDTR TR 24772-2010]]

Section 6.46, "Termination Strategy [REU Termination strategy]"

]]></ac:plain-text-body></ac:structured-macro>

...