Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flang] Optional character argument used in string concatenation causes segfault #112593

Open
agozillon opened this issue Oct 16, 2024 · 1 comment
Assignees
Labels
flang Flang issues not falling into any other category

Comments

@agozillon
Copy link
Contributor

Now to preface this, I am not sure if this is legal, illegal or UB in terms of the Fortran specification (so please do chime in any available Fortran guru's it would be greatly appreciated), however, it is something that we've encountered in the UMT benchmark that appears to be supported by gfortran and presumably other fortran compilers that currently support UMT.

In the below example, we effectively use a nullary optional argument as part of a string concatenation, in flang we segfault, in gfortran it will allow it and basically insert a blank character (the additional call to f90veify may be unnecessary to evoke the behavior, it's primarily the idiom that UMT uses).

module reproducers
  contains
  subroutine f90verify(message)
    implicit none
    character(*), intent(in) :: message

    print *, "does nothing"
  end subroutine

  subroutine optional_test(h_ptr, label)
    integer, intent(in), target, dimension(1) :: h_ptr
    character(len=*), intent(in), optional :: label

    call f90verify("Failed to allocate " // label // " on device.")
  end subroutine
end module reproducers

program main
    use reproducers
    integer :: test(10)
    call optional_test(test)
end program

So I suppose, if this is UB, but other compilers do something more "reasonable" than segfault, then the question would be if we wish to follow suit and fix it to conform with the gfortran and friends.

If it is legal, then it's likely something we wish to fix.

If it is illegal, then it's likely not flang's problem, and it's up to UMT to alter their source code to be legal.

But if it is something that requires fixing in flang, then it's likely there is someone more suitable to fixing this issue than I am as I am not particularly familiar with how optional or characters are handled in Flang at the moment.

@github-actions github-actions bot added the flang Flang issues not falling into any other category label Oct 16, 2024
@klausler
Copy link
Contributor

This usage is not conforming -- there's no carve-out for character concatenation (//) as an exception to references to absent optional dummy arguments in F'2023 15.5.2.13. But this usage works with literally every other Fortran compiler to which I have access (7), so it's portable, and this compiler must support it, at least with assumed-length character arguments as in this test case.

My suspicion is that the compiler should be passing 0 for the length of the absent assumed-length character argument but does not. Printing LEN(label) before the crash reveals an uninitialized-looking big negative integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category
Projects
None yet
Development

No branches or pull requests

3 participants