llvm / llvm/llvm-project

[Flang] Runtime crash or wrong result for huge local array

Open
#192,932 2 comments 0 reactions 0 assignees View on GitHub
flang:ir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Flang crashes or produces wrong results when a subroutine contains a very large local array. The issue is caused by allocating huge arrays on the stack, leading to stack overflow.

Expected Behavior: Large local arrays should be handled safely, either through heap allocation or with a clear compile-time error when stack allocation would fail.

Actual Behavior:

At -O0:
Compilation produces warning:
`warning: :0:0: stack frame size (25769803880) exceeds limit (4294967295) in function 'toobig_' [-Wbackend-plugin]`
Runtime with limited stack: segmentation fault
Runtime with unlimited stack: Wrong result

At -O1:
Compilation produces warning:
```
warning: :0:0: stack frame size (25769803864) exceeds limit (4294967295) in function '_QQmain' [-Wbackend-plugin]
warning: :0:0: stack frame size (25769803864) exceeds limit (4294967295) in function 'toobig_' [-Wbackend-plugin]
warning: :0:0: stack frame size (25769803864) exceeds limit (4294967295) in function 'main' [-Wbackend-plugin]
```
Runtime with limited stack: segmentation fault
Runtime with unlimited stack: Correct result

Reproducer:

```fortran
Program test
Call toobig
End Program
Subroutine toobig
Integer(8),Parameter :: big = 3 * 1024_8**3, dim1 = big, dim2 = 4
Integer(2) :: x(dim1,dim2), j
Do j=1,4
x(dim1,j) = j
End Do
Print *, x(dim1,:)
End Subroutine
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.