public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jellby at yahoo dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/100183] New: Segmentation fault at runtime when passing an internal procedure as argument
Date: Wed, 21 Apr 2021 14:33:27 +0000	[thread overview]
Message-ID: <bug-100183-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100183

            Bug ID: 100183
           Summary: Segmentation fault at runtime when passing an internal
                    procedure as argument
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jellby at yahoo dot com
  Target Milestone: ---

I've only been able to reproduce it with:

$ uname -a
Darwin minimac.moose.housegordon.com 20.3.0 Darwin Kernel Version 20.3.0: Thu
Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101 arm64

$ gfortran -version
GNU Fortran (Homebrew GCC 10.2.0_4) 10.2.1 20201220


Compiling and running the following code works fine, but with -O1 it gives a
segmentation fault. Alternatively, undefining INTERNAL_PROC_ARG works with -O1.


$ cat test.F90
#define INTERNAL_PROC_ARG                                                      
                                                                               
          [45/90681]

module sorting
    implicit none
    private
    public :: argsort
    real, pointer :: mod_rV(:)

    interface
        logical pure function compare_int_t(a, b)
            integer, intent(in) :: a, b
        end function
    end interface

contains

    logical pure function my_compare_rV(x, y)
        integer, intent(in) :: x, y
        my_compare_rV = mod_rV(x) <= mod_rV(y)
    end function

    function argsort(V) result(idx)
        real, target, intent(inout) :: V(:)
        integer :: idx(lbound(V, 1):ubound(V, 1)), i

        idx = [(i, i = lbound(V, 1), ubound(V, 1))]

#       ifdef INTERNAL_PROC_ARG
        call sort(idx, my_compare)
#       else
        mod_rV => V
        call sort(idx, my_compare_rV)
#       endif

    contains
        logical pure function my_compare(x, y)
            integer, intent(in) :: x, y
            my_compare = V(x) <= V(y)
        end function
    end function argsort

    subroutine sort(A, compare)
        integer, intent(inout) :: A(:)
        procedure(compare_int_t) :: compare
        integer :: i, j, t
        do i = lbound(A, 1), ubound(A, 1)
          do j = i + 1, ubound(A, 1)
            if (.not. compare(A(i), A(j))) then
                t = A(i)
                A(i) = A(j)
                A(j) = t
            end if
          end do
        end do
    end subroutine sort
end module sorting

program test
    use sorting, only: argsort

    implicit none
    integer :: i
    integer, parameter :: seed(50) = [(i, i = 1, size(seed))]
    real :: lambdas(5)
    integer :: idx(size(lambdas))

    call random_seed(put=seed)
    call random_number(lambdas)

    write(6,*) 'Before sorting:'
    write(6,*) lambdas(:)
    idx(:) = argsort(lambdas)
    write(6,*) 'Argsort:'
    write(6,*) idx(:)
    write(6,*) 'Sorted:'
    write(6,*) lambdas(idx(:))
end program test


$ gfortran -O1 test.F90 -o test ; ./test       
 Before sorting:
  0.471070886      0.117344737      0.357547939      0.318134785     
0.696753800    
zsh: segmentation fault  ./test


$ gfortran -O0 test.F90 -o test ; ./test
 Before sorting:
  0.471070886      0.117344737      0.357547939      0.318134785     
0.696753800    
 Argsort:
           2           4           3           1           5
 Sorted:
  0.117344737      0.318134785      0.357547939      0.471070886     
0.696753800

             reply	other threads:[~2021-04-21 14:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21 14:33 jellby at yahoo dot com [this message]
2021-04-21 21:12 ` [Bug fortran/100183] " anlauf at gcc dot gnu.org
2021-04-22  7:25 ` rguenth at gcc dot gnu.org
2021-04-22  7:26 ` jellby at yahoo dot com
2021-04-22  7:32 ` iains at gcc dot gnu.org
2021-04-29 21:02 ` anlauf at gcc dot gnu.org
2021-04-29 21:30 ` iains at gcc dot gnu.org
2022-01-01  8:57 ` pinskia at gcc dot gnu.org
2022-01-01  9:01 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-100183-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).