Open4

Deep dive into JVM

tttoltttol

JVMをクラッシュさせてみよう

JVMを意図的にクラッシュさせるとしたら、どうしますか?

tttoltttol

無限ループ

public class Crash {
    public static void main(String[] args) {
        int i = 0;
        while (true) {
            System.out.println(i++);
        }
    }
}
$ java Crash
1
2
3
4
5
# 中略
14465754
14465755
14465756
tttoltttol

再帰呼び出し

public class Crash {
    public static void main(String[] args) {
        someFunc();
    }

    private static void someFunc() {
        someFunc();
    }
}
$ java Crash
Exception in thread "main" java.lang.StackOverflowError
        at Crash.someFunc(Crash.java:7)
tttoltttol

JNI

javac CrashDemo.java
javac -h . CrashDemo.java
# libcrash.soではなくlibcrash.dylibでoutする
gcc -shared -fpic -o libcrash.dylib -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin CrashDemo.c
java -Djava.library.path=. CrashDemo

$ gcc -shared -o libcrash.dylib -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin CrashDemo.c
$ java -Djava.library.path=. CrashDemo                                                                
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000000104f67f9c, pid=85066, tid=5379
#
# JRE version: OpenJDK Runtime Environment Corretto-21.0.1.12.1 (21.0.1+12) (build 21.0.1+12-LTS)
# Java VM: OpenJDK 64-Bit Server VM Corretto-21.0.1.12.1 (21.0.1+12-LTS, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-aarch64)
# Problematic frame:
# C  [libcrash.dylib+0x3f9c]  Java_CrashDemo_causeSegmentationFault+0x18
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/t-takahashi/Documents/workspace/jvm-deep-dive/hs_err_pid85066.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/corretto/corretto-21/issues/
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
zsh: abort      java -Djava.library.path=. CrashDemo