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

            Bug ID: 123673
           Summary: [PDT] seg fault on composite PDT function result
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: damian at archaeologic dot codes
  Target Milestone: ---

$ cat reproducer.f90 
module input_output_pair_m
  implicit none

  ! Moving the contents of this module to the main program causes several 
  ! compile-time errors that do not occur with the ifx compiler

  type tensor_t(k)
    integer, kind :: k = kind(1.)
    real(k), allocatable :: values_(:)
  end type

  type input_output_pair_t(k)
    integer, kind :: k = kind(1.)
    type(tensor_t(k)) inputs_, expected_outputs_
  end type

contains

  ! Moving just the function below to become an internal subprogram in the main
program
  ! causes similar compile-time errors to those mentioned above

  type(input_output_pair_t) elemental function input_output_pair(inputs,
expected_outputs)
    type(tensor_t), intent(in) :: inputs, expected_outputs
    input_output_pair%inputs_ = inputs
    input_output_pair%expected_outputs_ = expected_outputs
  end function

end module

program trainable_network_test
  use input_output_pair_m
  implicit none

  type bin_t
    integer  first_, last_
  end type

  ! Removing the mini_batch_t's all instances of 'k' below causes
  ! the followingn compile-time error on the above 'use' statement:
  ! "Cannot convert TYPE(input_output_pair_t) to TYPE(Pdtinput_output_pair_t_4)
at (1)",
  ! where "1" is positiioned just after 'use'

  type mini_batch_t(k)
    integer, kind :: k = kind(1.)
    type(input_output_pair_t(k)), allocatable :: input_output_pairs_(:)
  end type

  type(input_output_pair_t), allocatable :: input_output_pairs(:)
  type(bin_t), allocatable :: bins(:)
  type(mini_batch_t) mini_batch_1
  integer, parameter :: num_pairs = 7 ! 7 is the mininum value that causes
segmentation fault
  integer, parameter :: n_bins = 2    ! 2 is the mininum value that causes
segmentation fault
  integer p, b

  input_output_pairs = input_output_pair([(tensor_t([0.,0.]), p = 1,
num_pairs)], [(tensor_t([0.,0.]), p = 1, num_pairs)])
  bins = [(bin(num_pairs, n_bins, b), b = 1, n_bins)]

  ! The final exectuable statement below causes a segmentation fault with
gfortran.
  ! Converting the assignment to an 'associate' statement also causes a seg
fault.

  mini_batch_1 = mini_batch(input_output_pairs(bins(1)%first_:bins(1)%last_))

contains

  type(bin_t) function bin(num_items, num_bins, bin_number)
    integer num_items, num_bins, bin_number
    associate(remainder => mod(num_items, num_bins), items_per_bin =>
num_items/num_bins)
      if (bin_number <= remainder) then
        bin%first_ = 1 + (bin_number-1)*(items_per_bin+1)
        bin%last_  = bin_number*(items_per_bin+1)
      else
        bin%first_ = 1 + (remainder-1)*(items_per_bin+1) + 1 +
(bin_number-remainder)*items_per_bin
        bin%last_ = remainder*(items_per_bin+1) +
(bin_number-remainder)*items_per_bin
      end if
    end associate
  end function

  type(mini_batch_t) function mini_batch(input_output_pairs)
    type(input_output_pair_t) input_output_pairs(:)
    mini_batch%input_output_pairs_ = input_output_pairs
  end function

end program

$ gfortran reproducer.f90 

$ ./a.out
free(): double free detected in tcache 2

Program received signal SIGABRT: Process abort signal.

Backtrace for this error:
#0  0x7f4e8898a51f in ???
        at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
#1  0x7f4e889de9fc in __pthread_kill_implementation
        at ./nptl/pthread_kill.c:44
#2  0x7f4e889de9fc in __pthread_kill_internal
        at ./nptl/pthread_kill.c:78
#3  0x7f4e889de9fc in __GI___pthread_kill
        at ./nptl/pthread_kill.c:89
#4  0x7f4e8898a475 in __GI_raise
        at ../sysdeps/posix/raise.c:26
#5  0x7f4e889707f2 in __GI_abort
        at ./stdlib/abort.c:79
#6  0x7f4e889d1676 in __libc_message
        at ../sysdeps/posix/libc_fatal.c:156
#7  0x7f4e889e8cfb in malloc_printerr
        at ./malloc/malloc.c:5664
#8  0x7f4e889eb0aa in _int_free
        at ./malloc/malloc.c:4473
#9  0x7f4e889ed452 in __GI___libc_free
        at ./malloc/malloc.c:3391
#10  0x404a4d in ???
#11  0x405016 in ???
#12  0x7f4e88971d8f in __libc_start_call_main
        at ../sysdeps/nptl/libc_start_call_main.h:58
#13  0x7f4e88971e3f in __libc_start_main_impl
        at ../csu/libc-start.c:392
#14  0x4010b4 in ???
#15  0xffffffffffffffff in ???
Aborted (core dumped)

$ gfortran --version
GNU Fortran (GCC) 16.0.1 20260116 (experimental)
  • [Bug fortran/123673] New: [P... damian at archaeologic dot codes via Gcc-bugs

Reply via email to