Bug 27269 - Segfault with EQUIVALENCEs in modules together with ONLY clauses
Summary: Segfault with EQUIVALENCEs in modules together with ONLY clauses
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: 4.2.0
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2006-04-23 11:29 UTC by Tobias Schlüter
Modified: 2006-05-02 14:24 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-04-26 16:41:05


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Schlüter 2006-04-23 11:29:26 UTC
schluter@pcl247d:~/src/work/bug> cat t.f90
      module const
      ! Kind parameters
      integer, parameter :: dp = kind(1d0)

        double precision reimMUE(2), reMUE, imMUE
        equivalence (MUE, reimMUE)
        equivalence (reimMUE(1), reMUE), (reimMUE(2), imMUE)

      end module const

module cross_section
  use const, only: dp
end module cross_section


  use const
  use cross_section

  implicit none

end program
schluter@pcl247d:~/src/work/bug> ~/src/gcc/build/gcc/f951 t.f90
t.f90:0: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
schluter@pcl247d:~/src/work/bug> ~/src/gcc/build/gcc/f951 --version
GNU F95 version 4.2.0 20060421 (experimental) (i686-pc-linux-gnu)
        compiled by GNU C version 4.2.0 20060421 (experimental).
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
schluter@pcl247d:~/src/work/bug> gdb ~/src/gcc/build/gcc/f951
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) run t.f90
Starting program: /home/pcl331/schluter/src/gcc/build/gcc/f951 t.f90

Program received signal SIGSEGV, Segmentation fault.
0x0808dc3d in resolve_types (ns=0x870d288) at ../../gcc/fortran/resolve.c:5829
5829      last_ts = &eq->expr->symtree->n.sym->ts;
(gdb) bt
#0  0x0808dc3d in resolve_types (ns=0x870d288)
    at ../../gcc/fortran/resolve.c:5829
#1  0x080902bc in gfc_resolve (ns=0x870d288)
    at ../../gcc/fortran/resolve.c:6252
#2  0x08086059 in gfc_parse_file () at ../../gcc/fortran/parse.c:3179
#3  0x080a64dd in gfc_be_parse_file (set_yydebug=0)
    at ../../gcc/fortran/f95-lang.c:301
#4  0x08391fea in toplev_main (argc=2, argv=0xbffff984)
    at ../../gcc/toplev.c:999
#5  0x080d867f in main (argc=Cannot access memory at address 0x1
) at ../../gcc/main.c:35
(gdb)  p eq->expr->symtree
$2 = (gfc_symtree *) 0x0
(gdb)

For the bug to trigger both the direct and the indirect use as well as the ONLY clause are necessary.  Note that the actual code location is in resolve_equivalence, so the bug is probably due to gfortran trying to understand an equivalence whose member variables have not been loaded, due to the ONLY clause.
Comment 1 Tobias Schlüter 2006-04-23 11:45:34 UTC
Since I still have it on my screen:
(gdb) p eq->expr->where->nextc-18
$6 = 0x8708de9 " use cross_section"
Comment 2 Paul Thomas 2006-04-26 14:09:52 UTC
The testcase reduces to:

module a
  integer, parameter :: dp = kind (1d0)
  real(dp) :: reM, M
  equivalence (M, reM)
end module a

module b
  use a, only : dp
end module b

  use a
  use b
  print *, M
  reM = 0.5d1
  print *, M
end

and can be fixed by ensuring that equivalences with unused module variables never get to the namespace.  Here is a preliminary patch that accomplishes this.  Using gfc_free_equiv causes segfaults further down the line because eq->expr
is being freed.  I do not know, at the moment, why that should be.  To keep memory leaks to the minimum, I have just freed the gfc_equiv, for the time being.  Working on this has exposed another, in some ways more serious, bug, which I will add to the database.

Index: gcc/fortran/module.c
===================================================================
--- gcc/fortran/module.c        (r?®vision 113192)
+++ gcc/fortran/module.c        (copie de travail)
@@ -3013,7 +3022,8 @@
 static void
 load_equiv(void)
 {
-  gfc_equiv *head, *tail, *end;
+  gfc_equiv *head, *tail, *end, *eq;
+  bool unused;

   mio_lparen();

@@ -3039,12 +3049,33 @@
        mio_expr(&tail->expr);
       }

+    unused = false;
+    for (eq = head; eq; eq = eq->eq)
+      {
+       if (!(eq && eq->expr && eq->expr->symtree))
+         {
+           unused = true;
+           break;
+         }
+      }
+
+    if (unused)
+      {
+       for (eq = head; eq; eq = head)
+         {
+           head = eq->eq;
+           gfc_free (eq);
+         }
+      }
+
     if (end == NULL)
       gfc_current_ns->equiv = head;
     else
       end->next = head;

-    end = head;
+    if (head != NULL)
+      end = head;
+
     mio_rparen();
   }


Comment 3 patchapp@dberlin.org 2006-04-28 09:15:13 UTC
Subject: Bug number PR27269

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/2006-04/msg01079.html
Comment 4 Roger Sayle 2006-05-02 14:13:22 UTC
Subject: Bug 27269

Author: sayle
Date: Tue May  2 14:13:17 2006
New Revision: 113465

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113465
Log:
2006-05-02 Paul Thomas <pault@gcc.gnu.org>

        PR fortran/27269
        * module.c: Add static flag in_load_equiv.
        (mio_expr_ref): Return if no symtree and in_load_equiv.
        (load_equiv): If any of the equivalence members have no symtree, free
        the equivalence and the associated expressions.

        PR fortran/27324
        * trans-common.c (gfc_trans_common): Invert the order of calls to
        finish equivalences and gfc_commit_symbols.

        PR fortran/27269
        PR fortran/27324
        * gfortran.dg/module_equivalence_2.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/module_equivalence_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/module.c
    trunk/gcc/fortran/trans-common.c
    trunk/gcc/testsuite/ChangeLog

Comment 5 roger 2006-05-02 14:24:34 UTC
This should now be fixed on mainline, thanks to Paul's patch.
Comment 6 Paul Thomas 2006-05-08 05:02:14 UTC
Subject: Bug 27269

Author: pault
Date: Mon May  8 05:01:56 2006
New Revision: 113618

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=113618
Log:
2006-05-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/24813
	* trans-array.c (get_array_ctor_strlen): Remove static attribute.
	* trans.h: Add prototype for get_array_ctor_strlen.
	* trans-intrinsic.c (gfc_conv_intrinsic_len): Switch on EXPR_ARRAY
	and call get_array_ctor_strlen.

	PR fortran/27269
	* module.c: Add static flag in_load_equiv.
	(mio_expr_ref): Return if no symtree and in_load_equiv.
	(load_equiv): If any of the equivalence members have no symtree, free
	the equivalence and the associated expressions.

	PR fortran/27324
	* trans-common.c (gfc_trans_common): Invert the order of calls to
	finish equivalences and gfc_commit_symbols.

	PR fortran/25099
	* resolve.c (resolve_call): Check conformity of elemental
	subroutine actual arguments.

2006-05-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/24813
	* gfortran.dg/char_cons_len.f90: New test.

	PR fortran/27269
	PR fortran/27324
	* gfortran.dg/module_equivalence_2.f90: New test.

	PR fortran/25099
	* gfortran.dg/elemental_subroutine_4.f90: New test.
	* gfortran.dg/assumed_size_refs_1.f90: Add error to non-conforming
	call sub (m, x).



Added:
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/char_cons_len.f90
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/elemental_subroutine_4.f90
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/module_equivalence_2.f90
Modified:
    branches/gcc-4_1-branch/gcc/fortran/ChangeLog
    branches/gcc-4_1-branch/gcc/fortran/module.c
    branches/gcc-4_1-branch/gcc/fortran/resolve.c
    branches/gcc-4_1-branch/gcc/fortran/trans-array.c
    branches/gcc-4_1-branch/gcc/fortran/trans-common.c
    branches/gcc-4_1-branch/gcc/fortran/trans-intrinsic.c
    branches/gcc-4_1-branch/gcc/fortran/trans.h
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_1-branch/gcc/testsuite/gfortran.dg/assumed_size_refs_1.f90