Description
Describe the issue
I am using GraalVM 21.1.0 Java 11 CE to compile static binaries for linux using musl 1.2.2-1
(from Debian unstable). The stack depth is much more limited than a similar binary compiled without --static
. I tried to increase the limit with "-H:CCompilerOption=-Wl,-z,stack-size=10485760"
but to no avail.
Steps to reproduce the issue
Compile the following JavaRecur.java
program and run it:
public class JavaRecur {
public static void foo(int x) {
System.out.println(x);
foo(x+1);
}
public static void main(String [] args) {
foo(0);
}
}
javac JavaRecur.java
"$GRAALVM_HOME/bin/native-image" --no-server --no-fallback "--static" "--libc=musl" "-H:CCompilerOption=-Wl,-z,stack-size=10485760" JavaRecur
./javarecur
You will likely see that the stack depth is limited to around 2500
while with the non-static binary we can go up to around 260000
.
Note that a basic C program like this:
#include <stdio.h>
void recur(int x)
{
if (x % 10000 == 0)
printf("%d\n", x);
recur(x + 1);
}
int main()
{
recur(0);
return 0;
}
behaves pretty much the same with gcc
and musl-gcc
(without passing linker options to either):
$ gcc -o main main.c
$ ./main
0
10000
...
250000
260000
[1] 31779 segmentation fault ./main
Similar output with musl-gcc -o main --static main.c
.
Describe GraalVM and your environment:
- GraalVM version 21.1.0 JDK 11
- Linux
- Amd64
More details
See https://gist.github.com/borkdude/3a36ccc5dd186ac9f1a761b9b7b7cd36 for a stack trace that contains more info.
See https://gist.github.com/borkdude/8395ac49d7b90ebe4b9a911565304b3d for options passed to the linker printed with "-H:CCompilerOption=--verbose"
and "-H:+TraceNativeToolUsage"
.
Workaround
Compile with -H:CCompilerOption=-Wl,-z,stack-size=10485760
(or higher stack size if needed)
and then start your main program in a thread instead.
This is surely a workaround, the main issue should still be addressed.
/cc @gradinac