[Flang] execution error when a variable with an undefined value is specified as an argument to sizeof intrinsic function
Version of flang-new : 18.0.0(103811a27abcbcc6792c88dcdd30034e5cc8adc3)
When a variable with an undefined value is specified as an argument to sizeof intrinsic function, the program terminates abnormally at runtime.
ifort also terminated abnormally at runtime, but gfortran terminated normally with the function returning 0.
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
sngts008_.f90:
program main
call s()
print *,'pass'
contains
subroutine s(a)
integer,dimension(:),optional::a
if(sizeof(a)/=0) print *,'err'
end subroutine s
end program main
$ flang-new sngts008_.f90; ./a.out
Segmentation fault (core dumped)
$
$ gfortran sngts008_.f90; ./a.out
pass
$
$ ifort sngts008_.f90; ./a.out
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
libc.so.6 00007FDDD5C42520 Unknown Unknown Unknown
a.out 00000000004041D4 Unknown Unknown Unknown
a.out 000000000040418D Unknown Unknown Unknown
libc.so.6 00007FDDD5C29D90 Unknown Unknown Unknown
libc.so.6 00007FDDD5C29E40 __libc_start_main Unknown Unknown
a.out 00000000004040A5 Unknown Unknown Unknown
$
@llvm/issue-subscribers-flang-frontend
Author: None (ohno-fj)
When a variable with an undefined value is specified as an argument to sizeof intrinsic function, the program terminates abnormally at runtime.
ifort also terminated abnormally at runtime, but gfortran terminated normally with the function returning 0.
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
sngts008_.f90:
program main
call s()
print *,'pass'
contains
subroutine s(a)
integer,dimension(:),optional::a
if(sizeof(a)/=0) print *,'err'
end subroutine s
end program main
$ flang-new sngts008_.f90; ./a.out
Segmentation fault (core dumped)
$
$ gfortran sngts008_.f90; ./a.out
pass
$
$ ifort sngts008_.f90; ./a.out
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
libc.so.6 00007FDDD5C42520 Unknown Unknown Unknown
a.out 00000000004041D4 Unknown Unknown Unknown
a.out 000000000040418D Unknown Unknown Unknown
libc.so.6 00007FDDD5C29D90 Unknown Unknown Unknown
libc.so.6 00007FDDD5C29E40 __libc_start_main Unknown Unknown
a.out 00000000004040A5 Unknown Unknown Unknown
$
@llvm/issue-subscribers-bug
Author: None (ohno-fj)
When a variable with an undefined value is specified as an argument to sizeof intrinsic function, the program terminates abnormally at runtime.
ifort also terminated abnormally at runtime, but gfortran terminated normally with the function returning 0.
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
sngts008_.f90:
program main
call s()
print *,'pass'
contains
subroutine s(a)
integer,dimension(:),optional::a
if(sizeof(a)/=0) print *,'err'
end subroutine s
end program main
$ flang-new sngts008_.f90; ./a.out
Segmentation fault (core dumped)
$
$ gfortran sngts008_.f90; ./a.out
pass
$
$ ifort sngts008_.f90; ./a.out
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
libc.so.6 00007FDDD5C42520 Unknown Unknown Unknown
a.out 00000000004041D4 Unknown Unknown Unknown
a.out 000000000040418D Unknown Unknown Unknown
libc.so.6 00007FDDD5C29D90 Unknown Unknown Unknown
libc.so.6 00007FDDD5C29E40 __libc_start_main Unknown Unknown
a.out 00000000004040A5 Unknown Unknown Unknown
$
Investigating this.
What seems to be happening is that gfortran creates a temp integer variable and passes that to sizeof() when compiling this code (sizeof(a) returns 4 in gfortran). Flang is just passing a null pointer.
The current Flang implementation is based on the Fortran 2018 standard, so calling the subroutine with no argument, and therefore, sizeof() with no argument is expected to be an error. This doesn't even compile with gfortran when specifying the 2018 standard:
$ gfortran sngts008_.f90 -std=f2018 -o sngts008_.f90-gnu
/usr/bin/ld: /tmp/cc9FTZ5h.o: in function `s.0':
sizeof.f90:(.text+0x104): undefined reference to `sizeof_'
collect2: error: ld returned 1 exit status
I think the real question here is whether we want to mimic the behavior of gfortran or not. Any opinions about this?
It seems this happens because sizeof() is a GNU extension, so it's only supported with -std=gnu.
https://gcc.gnu.org/onlinedocs/gfortran/Fortran-Dialect-Options.html
Since flang already supports sizeof(), IMO it makes sense to fix this issue.
Never reference the value of an optional argument that is not present.
Never reference the value of an optional argument that is not present.
Thank you for your comment. I understand the above.