Bug 33040 - [ISO_C_BINDING] ICE in gfc_trans_structure_assign
[ISO_C_BINDING] ICE in gfc_trans_structure_assign
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.3.0
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
: ice-on-valid-code
Depends on:
Blocks: 32630
  Show dependency treegraph
 
Reported: 2007-08-09 21:13 UTC by Tobias Burnus
Modified: 2007-09-11 16:46 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-08-09 22:54:15


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2007-08-09 21:13:25 UTC
The following reduced example, based on http://www.lrz-muenchen.de/services/software/mathematik/gsl/fortran/, gives an segmentation fault:

fgsl.f90: In function 'fgsl_multifit_fdfsolver_jac':
fgsl.f90:24: internal compiler error: Segmentation fault

valgrind shows:
==21413== Invalid read of size 2
==21413==    at 0x49810A: gfc_trans_structure_assign (trans-expr.c:3179)
     tmp = build3 (COMPONENT_REF, TREE_TYPE (field), dest, field, NULL_TREE);
==21413==    by 0x497FF3: gfc_trans_subcomponent_assign (trans-expr.c:3139)
==21413==    by 0x498142: gfc_trans_structure_assign (trans-expr.c:3180)
==21413==    by 0x4982FE: gfc_conv_structure (trans-expr.c:3207)
==21413==    by 0x494EA0: gfc_trans_assignment_1 (trans-expr.c:4020)
==21413==    by 0x49501B: gfc_trans_assignment (trans-expr.c:4171)
==21413==    by 0x4791CA: gfc_trans_code (trans.c:577)

The crash occurs because field = NULL, which fails when using "TREE_TYPE(field)".

module fgsl
  use, intrinsic :: iso_c_binding
  implicit none
  type, public :: fgsl_matrix
     private
     type(c_ptr) :: gsl_matrix = c_null_ptr
  end type fgsl_matrix
  type, public :: fgsl_multifit_fdfsolver
     private
     type(c_ptr) :: gsl_multifit_fdfsolver = c_null_ptr
  end type fgsl_multifit_fdfsolver
interface
  function gsl_multifit_fdfsolver_jac(s) bind(c)
    import :: c_ptr
    type(c_ptr), value :: s
    type(c_ptr) :: gsl_multifit_fdfsolver_jac
  end function gsl_multifit_fdfsolver_jac
end interface
contains
  function fgsl_multifit_fdfsolver_jac(s)
    type(fgsl_multifit_fdfsolver), intent(in) :: s
    type(fgsl_matrix) :: fgsl_multifit_fdfsolver_jac
    fgsl_multifit_fdfsolver_jac%gsl_matrix = &
         gsl_multifit_fdfsolver_jac(s%gsl_multifit_fdfsolver)
  end function fgsl_multifit_fdfsolver_jac
end module fgsl
Comment 1 Tobias Burnus 2007-08-09 22:23:24 UTC
Some more debugging - I modified gfc_trans_structure_assign to:
----------------------------------
  cm = expr->ts.derived->components; i = 1;
  for (c = expr->value.constructor; c; c = c->next, cm = cm->next)
    {
printf("DEBUG: gfc_trans_structure_assign, loop %i; cm->name = %s\n",i++, cm->name);
      if (!c->expr)
        continue;
      field = cm->backend_decl;
if (field == NULL) {
    gfc_error ("field is zero at %L", &c->expr->where); continue;
}
----------------------------------

DEBUG: gfc_trans_structure_assign, loop 1; cm->name = __c_ptr_c_address
fgsl.f90:2.33:
  use, intrinsic :: iso_c_binding
                                1
Error: field is zero at (1)
Comment 2 Tobias Burnus 2007-08-29 18:15:43 UTC
At least for it currently crashes already for the following reduced example; the problem seems to be the default initializer. (Another hint that this is indeed the problem: After commenting out the "if (f->sym && f->sym->attr.intent == INTENT_OUT" etc. in gfc_trans_deferred_vars, gfortran does not crash anymore.)

module m
  use, intrinsic :: iso_c_binding
  implicit none
  type t
    type(c_ptr) :: matrix  = c_null_ptr
  end type t
contains
  subroutine func(a)
    type(t), intent(out) :: a
  end subroutine func
end module m
Comment 3 patchapp@dberlin.org 2007-09-10 21:31:26 UTC
Subject: Bug number PR 33040

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-09/msg00918.html
Comment 4 Tobias Burnus 2007-09-11 15:53:37 UTC
Subject: Bug 33040

Author: burnus
Date: Tue Sep 11 15:53:22 2007
New Revision: 128385

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=128385
Log:
2007-09-11  Christopher D. Rickett  <crickett@lanl.gov>

	PR fortran/33040
	* trans-expr.c (gfc_trans_structure_assign): Convert component
	C_NULL_PTR and C_NULL_FUNPTR component initializers to (void *).
	* trans-types.c (gfc_get_derived_type): Create a backend_decl for
	the c_address field of C_PTR and C_FUNPTR and ensure initializer
	is of proper type/kind for (void *).

2007-09-11  Christopher D. Rickett  <crickett@lanl.gov>

	PR fortran/33040
	* gfortran.dg/c_ptr_tests_11.f03: New test case.


Added:
    trunk/gcc/testsuite/gfortran.dg/c_ptr_tests_11.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/testsuite/ChangeLog

Comment 5 Tobias Burnus 2007-09-11 16:46:41 UTC
Fixed by Chris (thanks!).

gfortran still does not fully work with FGSL :-(
See PR 33395.