Issue 243088
Summary [flang] Inline heap allocations (fir.allocmem) do not check the allocation size for overflow or failure
Labels flang
Assignees
Reporter eugeneepshteyn
    The compiler-generated heap allocations that do not go through flang-rt (`fir.allocmem`) compute the byte size without any overflow check. A request whose size overflows, or is larger than `INT64_MAX`, gets a tiny buffer (1 byte, rounded up to 64 for arrays) and the program continues as if the allocation succeeded. This is the inline counterpart of 240196, which covers the runtime path (`ALLOCATE` with `STAT=`) and is being addressed in flang-rt by 240951; that fix does not reach these allocations.

`ALLOCATE` without `STAT=`, of a non-pointer allocatable of intrinsic type:

```fortran
program alloc_no_stat
  implicit none
  real(8), allocatable :: a(:), b(:), c(:,:), d(:)
  character(kind=4, len=:), allocatable :: s
  allocate(a(2_8**62))              ! 2**65 bytes: wraps to 0
  print *, 'a:', allocated(a), size(a, kind=8)
  allocate(b(2_8**60)) ! 2**63 bytes: no wrap
  print *, 'b:', allocated(b), size(b, kind=8)
  allocate(c(2_8**32, 2_8**32))     ! 2**67 bytes: wraps to 0
 print *, 'c:', allocated(c)
  allocate(character(kind=4, len=2_8**62) :: s) ! 2**64 bytes: wraps to 0
  print *, 's:', allocated(s), len(s, kind=8)
 allocate(d(2_8**45))              ! 2**48 bytes: no overflow, malloc fails
 print *, 'd:', allocated(d)
end program
```

```console
$ flang alloc_no_stat.f90 && ./a.out
 a: T 4611686018427387904
 b: T 1152921504606846976
 c: T
 s: T 0
 d: F
```

Every `ALLOCATE` here should initiate error termination (F2023 9.7.1.2 p11: an error condition in an `ALLOCATE` statement without `STAT=` initiates error termination). Instead `a`, `b` and `c` are reported allocated on a 64-byte buffer and `s` on a 1-byte one, and `d` is left unallocated while execution continues. Same results at `-O0` and `-O2`.

The same happens without any `ALLOCATE` statement, for example on reallocation in an intrinsic assignment:

```fortran
program temp_overflow
  implicit none
  real(8), allocatable :: a(:)
  integer(8) :: i, n
  n = 2_8**62
  a = [(real(i, 8), i = 1_8, n)]    ! 2**65-byte result: wraps to 0
  print *, size(a, kind=8)
end program
```

This segfaults while filling the 64-byte buffer instead of reporting an allocation failure.

Cause, as far as I can tell:

- `ALLOCATE` without `STAT=` (no SOURCE=/MOLD=, not a pointer, coarray, derived or polymorphic type) is lowered inline to `fir.allocmem` (`genInlinedAllocation` in `flang/lib/Lower/Allocatable.cpp`). Reallocation on assignment (`allocateAndInitNewStorage` in `flang/lib/Optimizer/Builder/MutableBox.cpp`) and HLFIR temporaries (`createHeapTemporary`) also produce `fir.allocmem`.
- `AllocMemOpConversion` in `flang/lib/Optimizer/CodeGen/CodeGen.cpp` multiplies the element size, the character length and every extent with plain `llvm.mul`, then replaces a size that is not signed-positive with 1 (`icmp sgt size, 0`). So a product that wraps to 0, and an exact size of `2**63` bytes or more, both become `malloc(1)` (`aligned_alloc(64, 64)` for arrays). The descriptor's `elem_len` for the CHARACTER case is computed the same way, hence `len(s) = 0`.
- The inline `ALLOCATE` never checks the pointer returned by `malloc`, so a genuine allocation failure (`d`) leaves the variable unallocated with no error termination.

The `fir.alloca` codegen (automatic arrays, stack temporaries) multiplies the extents the same way, unchecked.

Observed with flang built from main at 2f6c791287848c44fbfd85e724fd4f8728aad7d4 on x86_64 Linux.

(This issue was filed with the help of AI)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to