Bug 1373 - recursion stress test causes segmentation fault
Summary: recursion stress test causes segmentation fault
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: libgcj (show other bugs)
Version: 2.96
: P3 normal
Target Milestone: ---
Assignee: Andrew Haley
URL:
Keywords:
: 13605 24980 27268 (view as bug list)
Depends on:
Blocks: 13603
  Show dependency treegraph
 
Reported: 2000-12-20 12:19 UTC by osk
Modified: 2016-09-30 22:52 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-02-20 18:42:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description osk 2000-12-20 12:19:22 UTC
According to the Java VM specs, ยง3.5.2, the vm should
throw StackOverflowError or OutOfMemoryError when the
stack is full or there's no more memory for the dynamic
stack. GCJ doesn't do this.

Consider the test program below. Here's the result of
running it in some different x86 environments:

gcj (gcj -o recurse --main=recurse recurse.java)
  Segmentation fault after >40000, <50000 recursions
Blackdown JDK 1.2.2
  Segmentation fault after >40000, <50000 recursions
gcj -O2
  Never exists - infinite recursion due to tail recursion optimization
IBM JDK 1.1.8
  Never exists - infinite recursion due to tail recursion optimization
IBM JDK 1.3.0
  Never exists - infinite recursion due to tail recursion optimization
IBM JDK 1.1.8 -nojit
  StackOverflowException with very long stack trace (>9000 lines)
gij
  Segmentation fault after >2000, <3000 recursions

Release:
unknown

Environment:
gcc/gcj 2.96 20000904

How-To-Repeat:
import java.util.Vector;

public class recurse {
	public static void main(String[] args) {
		recurse(0);
	}
	public static void recurse(int level) {
		if (level < 10000) {
			if (level % 1000 == 0)
				System.out.println(level);
		} else if (level < 100000) {
			if (level % 10000 == 0)
				System.out.println(level);
		} else if (level % 100000 == 0) {
			System.out.println(level);
		}
		recurse(level+1);
	}
}
Comment 1 Andrew Pinski 2003-05-24 23:41:26 UTC
still happens in the mainline (20030524).

With stacksize       8192 kbytes
tin:~/src/gnu/gcctest>./recurse
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
20000
30000
40000
50000
60000
70000
80000
90000
100000
200000
Segmentation fault (core dumped)
tin:~/src/gnu/gcctest>gij recurse
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
Segmentation fault (core dumped)
Comment 2 Andrew Pinski 2003-05-27 23:42:28 UTC
One way of fixing this having a signal catcher for SIGSEGV but from the glibc's signal 
man page:
According to POSIX, the behaviour of a process is undefined after it ignores a SIGFPE, 
SIGILL, or SIGSEGV signal that was not generated by the kill(2) or the raise(3) functions.
So I do not know if it is okay to do it at all?
Comment 3 Andrew Pinski 2004-01-07 18:42:40 UTC
*** Bug 13605 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2004-01-07 18:48:38 UTC
A library issue and not a front-end one.
Comment 5 Bruno Haible 2004-01-08 20:39:43 UTC
> One way of fixing this having a signal catcher for SIGSEGV but from the
glibc's signal 
> man page:
> According to POSIX, the behaviour of a process is undefined after it ignores a
SIGFPE, 
> SIGILL, or SIGSEGV signal that was not generated by the kill(2) or the
raise(3) functions.

This is not very relevant, because in practice, using the proper cautions,
catching SIGSEGV coming from a stack overflow works fine. GNU libsigsegv
provides the functionality, and GNU clisp and GNU smalltalk use it. It works on
Linux, OSF/1, FreeBSD, OpenBSD, NetBSD/Alpha, MacOS X, Solaris, HP-UX/HPPA,
IRIX, AIX 4, BeOS, Cygwin, Windows. The only tricky point in using it would be
the necessity to extend the stack by a few pages in order to be able to throw
the StackOverflowException. Or to throw this exception from within the signal
stack. Either of these would be challenging.
Comment 6 Andrew Pinski 2004-01-08 20:44:27 UTC
Note the word "undefined", on most platforms it will work just fine but it does not have to work on 
some platforms or even on ones where  it looks like it does.
Comment 7 Andrew Pinski 2004-04-19 15:25:18 UTC
Note at -O1 on the tree-ssa it never exits because of tail recursion optimization also.
Comment 8 Andrew Haley 2005-10-13 09:07:59 UTC
See http://gcc.gnu.org/ml/java-patches/2004-q4/msg00230.html.
Comment 9 Andrew Haley 2005-10-13 09:10:19 UTC
See also http://gcc.gnu.org/ml/java-patches/2004-q4/msg00241.html
Comment 10 Andrew Haley 2005-10-13 09:11:54 UTC
And http://gcc.gnu.org/ml/java-patches/2004-q4/msg00270.html.

Hans' threading seems to be broken.
Comment 11 Andrew Pinski 2005-11-21 21:46:50 UTC
*** Bug 24980 has been marked as a duplicate of this bug. ***
Comment 12 Andrew Pinski 2006-04-23 16:16:17 UTC
*** Bug 27268 has been marked as a duplicate of this bug. ***
Comment 13 Andrew Pinski 2006-04-23 16:18:21 UTC
A better testcase comes from PR 27268 which seg faults at all optimizations level currently (though this case can also be optimized out to be an infinite loop really):
public class test
{
    public test()
    {
        test t = new test();
    }

    public static void main(String argv[])
    {
        new test();
    }
}
Comment 14 Andrew Pinski 2016-09-30 22:52:19 UTC
Closing as won't fix as libgcj (and the java front-end) has been removed from the trunk.