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

            Bug ID: 123545
           Summary: [PDT] seg fault on assigning to parent PDT
           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: ---

Running the program below after compiling with the Intel ifx compiler Version
2025.2.1 Build 20250806 gives

Heading down the rabbit hole...
 expecting 1.000000, read    1.000000

Running the program below after compiling with the LLVM flang compiler man
brain built 12 November 2024 (version 22.0.0git
([email protected]:llvm/llvm-project 448146d6479cdfd6b7c80cb33dbaed882dead2f1)
gives

 expecting 1.000000, read  1.

both of which are acceptable results.  Both runs were on macOS.


Compiling with a 20251231 build of GCC 16 give a segmentation fault.  Several
comments in the program provide additional context.

$cat reproducer.F90

module julienne_fiats_m
  implicit none

  type string_t
    character(len=:), allocatable :: string_
  end type

  type file_t
    type(string_t), allocatable :: lines_(:)
  end type

  type hyperparameters_t(k) ! making this and the type below non-PDTs
eliminates the segmentation fault
    integer, kind :: k = kind(1.)
    real(k) :: learning_rate_ = real(1.5,k)
  end type

  type, extends(file_t) :: training_configuration_t(m) ! making this a non-PDT
gives a compile-time error
    integer, kind :: m = kind(1.)
    type(hyperparameters_t(m)) hyperparameters_
  end type

contains

  real function get_json_value(string)
    character(len=*) string
    read(string, fmt=*) get_json_value
    print *,"expecting 1.000000, read ", get_json_value
  end function

  function hyperparameters_to_json(self) result(lines)! invoked only by
training_config_from_component
    type(hyperparameters_t) self
    type(string_t), allocatable :: lines(:)
    integer, parameter :: max_width= 18
    character(len=max_width) learning_rate_string
    write(learning_rate_string,*) self%learning_rate_
    lines = [string_t(learning_rate_string)]
  end function

  type(training_configuration_t) function
training_config_from_component(hyperparameters)
    type(hyperparameters_t) hyperparameters
    training_config_from_component%hyperparameters_ = hyperparameters
    training_config_from_component%file_t =
file_t([hyperparameters_to_json(training_config_from_component%hyperparameters_)])
  end function

  function hyperparameters_from_json(lines) result(hyperparameters) ! invoked
only by training_config_from_file
    type(string_t) lines(:)
    type(hyperparameters_t) hyperparameters
    hyperparameters%learning_rate_ = get_json_value(lines(1)%string_)
  end function

  type(training_configuration_t) function training_config_from_file(line)
    character(len=*) line
    training_config_from_file%file_t = file_t([string_t(line)]) ! segmentation
fault
    training_config_from_file%hyperparameters_ =
hyperparameters_from_json(training_config_from_file%file_t%lines_)
  end function

end module

  use julienne_fiats_m
  implicit none
  type(training_configuration_t) training_configuration, from_json

  training_configuration =
training_config_from_component(hyperparameters_t(learning_rate_=1.))

  ! Removing the above assignment eliminates the segmentation fault even though
the segmentation fault
  ! occurs when executing the assignment below, which does not reference the
object defined above.
  ! Alternatively, changing the above line to an `associate` statement gives
the compile-time
  ! message: "Error: Invalid kind for REAL at (1)", where the "1" is between
`use` and `fiats_m` in
  ! the above use statement.

  print *,new_line(''), "Heading down the rabbit hole..." ! print to
demonstrate that execution continues past the above line
  from_json = training_config_from_file('1.00000000')
end

$ gfortran reproducer.F90
$ ./a.out

Heading down the rabbit hole...

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7ffbde5e551f in ???
        at ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0
#1  0x7ffbde6483fe in __GI___libc_free
        at ./malloc/malloc.c:3368
#2  0x402a69 in ???
#3  0x403ea2 in ???
#4  0x403f65 in ???
#5  0x7ffbde5ccd8f in __libc_start_call_main
        at ../sysdeps/nptl/libc_start_call_main.h:58
#6  0x7ffbde5cce3f in __libc_start_main_impl
        at ../csu/libc-start.c:392
#7  0x401134 in ???
#8  0xffffffffffffffff in ???
Segmentation fault (core dumped)

$ gfortran --version
GNU Fortran (GCC) 16.0.0 20251231 (experimental)

Reply via email to