Issue 243040
Summary [flang-rt] OPEN after the runtime has closed all units segfaults when FORT_CONVERT_UNIT lists units
Labels flang:runtime, crash-on-valid
Assignees
Reporter eugeneepshteyn
    When `FORT_CONVERT_UNIT` contains any per-unit exception, an OPEN that runs after the runtime has closed all units at program termination crashes with a segmentation fault. One way to get there is an `atexit` handler registered before the first Fortran I/O:

```fortran
module late_io
  use iso_c_binding
contains
  subroutine late() bind(c)
    open (11, file='late.dat', form='unformatted', status='replace')
    write (11) 42
 close (11)
    print *, 'late OPEN done'
  end subroutine
end module

program p
  use late_io
  interface
    integer(c_int) function atexit(f) bind(c, name='atexit')
      import :: c_int, c_funptr
 type(c_funptr), value :: f
    end function
  end interface
  integer :: rc
  rc = atexit(c_funloc(late)) ! registered before the first Fortran I/O
 print *, 'main done'
end program
```

```console
$ flang late_open.f90 -o late_open
$ ./late_open
 main done
 late OPEN done
$ FORT_CONVERT_UNIT=swap:99 ./late_open
 main done
Segmentation fault
```

- `FORT_CONVERT_UNIT=swap:11` crashes the same way. A global mode alone (`FORT_CONVERT_UNIT=swap`, or `FORT_CONVERT=swap`) does not crash.
- It happens with both the static and the shared runtime, and after END, STOP and `CALL EXIT` alike. A callback registered with `RegisterImagesNormalEndCallback` hits it too, because `NormalExit` calls that callback after closing the units.
- gfortran 13 runs the same program with `GFORTRAN_CONVERT_UNIT=swap:11` and writes `late.dat` byte-swapped.

Seen on x86_64 Linux with flang at llvm-project 2f6c79128784. The code involved is unchanged on main at e0b437836a21.

**Cause.** `ExternalFileUnit::CloseAll()` frees `executionEnvironment.convertUnits` and sets it to null, but leaves `numConvertUnits` unchanged ([external-unit.cpp L324-L327](https://github.com/llvm/llvm-project/blob/e0b437836a212942c47d86356316793a171b9a0b/flang-rt/lib/runtime/external-unit.cpp#L324-L327)). The next OPEN calls `ExecutionEnvironment::UnitRtConvert()`, which loops over `numConvertUnits` entries of the null array ([environment.cpp L248-L260](https://github.com/llvm/llvm-project/blob/e0b437836a212942c47d86356316793a171b9a0b/flang-rt/lib/runtime/environment.cpp#L248-L260)). This came in with 223831, which added `FORT_CONVERT_UNIT`.

**Possible fix.** Don't free the array in `CloseAll()`. It is a few bytes that process exit reclaims, and keeping it leaves the exceptions in force for a late OPEN, just as the `FORT_CONVERT` setting survives `CloseAll()`. The comment at environment.cpp L237 that refers to this free would go too. Resetting `numConvertUnits` to 0 beside the free would also stop the crash, but a late OPEN would then silently lose its per-unit exceptions.

(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