llvm / llvm/llvm-project

[Flang] Incorrect `ASSOCIATED()` result and crash when a procedure pointer is associated with an internal procedure from within that procedure itself

Open
#188,884 4 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```
flang version 23.0.0git (https://github.com/llvm/llvm-project.git b2ba79578b35f11759a856cf9dc7e66a46258206)
```

When a procedure pointer is pointer-associated with an internal procedure from within that internal procedure itself
(e.g., `p => sub` inside `sub`), `ASSOCIATED(p, sub)` evaluates incorrectly in flang, and calling through
the procedure pointer (`call p`) crashes.

sample.f90
```fortran
procedure(sub) ,pointer :: p
call sub
if (.not.associated(p,sub)) print *,2020
call p
print *,'pass'
contains
subroutine sub()
p=>sub
end subroutine sub
end program
```

```
$ flang sample.f90
/opt/rh/gcc-toolset-13/root/usr/lib/gcc/aarch64-redhat-linux/13/../../../../bin/ld: warning: /tmp/sample-bbf0c7.o: requires executable stack (because the .note.GNU-stack section is executable)
$ ./a.out
2020
Illegal instruction (core dumped)
```
There is no issue when a procedure pointer is associated with an internal procedure at its calling scope.

sample2.f90
```fortran
procedure(sub) ,pointer :: p
p=> sub
if (.not.associated(p,sub)) print *,2020
call p
print *,'pass'
contains
subroutine sub()
end subroutine sub
end program
```
```
$ flang sample2.f90
$ ./a.out
pass
```

### Other compilers

```
$ gfortran sample.f90
sample.f90:8:4:

8 | p=>sub
| 1
Warning: Non-RECURSIVE procedure ‘sub’ at (1) is possibly calling itself recursively. Declare it RECURSIVE or use ‘-frecursive’
/usr/bin/ld: warning: /tmp/cc0OytDQ.o: requires executable stack (because the .note.GNU-stack section is executable)
$ ./a.out
pass
```
```
$ ifx sample.f90
$ ./a.out
pass
```

### Specification note (Fortran)

Fortran 2023 clause 15.2.2.4 "Procedure pointers"
states that procedure pointers may be pointer-associated with internal procedures.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the failure with flang using sample.f90 and compare it with sample2.f90 and the reported gfortran/ifx behavior. Trace flang's handling of an internal procedure pointer assigned from within sub, then verify that ASSOCIATED(p, sub) is true and call p completes with the expected pass output without a crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.