12

How can I reproduce EXCEPTION_STACK_OVERFLOW error in Java.

PS: I am not talking of StackOverflowError Error in Java which gracefully shuts the JVM. I am talking of EXCEPTION_STACK_OVERFLOW in error.log which cause JVM to crash.

Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121
  • 3
    please double check your *PS*, I guess there's a tiny *not* missing in between "am" and "talking" ... – Andreas Dolk Jul 08 '11 at 11:15
  • 2
    Most `EXCEPTION_STACK_OVERFLOW` errors I found so far happen in native code outside the JVM... If there was one *inside*, it would be a bug and to be fixed. – Andreas Dolk Jul 08 '11 at 11:21
  • Not exact answer, but somehwat is there at http://stackoverflow.com/questions/65200/how-do-you-crash-a-jvm – Sandeep Jindal Jul 08 '11 at 12:36

3 Answers3

9

Most EXCEPTION_STACK_OVERFLOW errors I found so far happen in native code outside the JVM. A crash inside the JVM is worth a bug report and will be fixed. Or are you in need of an (unknown) exploit?

So the easist and most reliable way would be to write a native library with some code that causes the JVM to crash and call that with JNI.

(general answer, I actually don't know how to do it exactly. Can't be done with java code only ;) )

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • 1
    +1. I can do +1 only but wanted to do +100 for confirming my understanding. Most of the others has mentioned about StackOverFlowError! – Sandeep Jindal Jul 08 '11 at 11:35
4

Blow stack:

public static void main(String[] args) {
    main(null);
}

Blow heap:

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    while(true) list.add(new String("boom"));
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 2
    None of the above would cause JVM crash. First one will cause StackOverflowError and second would cause OutOfMemoryError. – Sandeep Jindal Jul 08 '11 at 11:37
4
public static void stackoverflow()
{
     stackoverflow();
}

Call it, and enjoy :D

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287