Bug 71649

Summary: Internal compiler error
Product: gcc Reporter: jack.saba <jack.saba>
Component: fortranAssignee: Jerry DeLisle <jvdelisle2>
Status: RESOLVED FIXED    
Severity: normal CC: fxcoudert, jvdelisle2
Priority: P3    
Version: unknown   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2016-06-24 00:00:00

Description jack.saba@nasa.gov 2016-06-24 15:31:11 UTC
MAC-bash: gfortran -c -Wall -Wextra -fno-strict-aliasing -fwrapv test.f90
f951: internal compiler error: Segmentation fault: 11

f951: internal compiler error: Abort trap: 6
gfortran: internal compiler error: Abort trap: 6 (program f951)
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
Computer info:
 Darwin Kernel Version 15.5.0: Tue Apr 19 18:36:36 PDT 2016; root:xnu-3248.50.21~8/RELEASE_X86_64 x86_64


Here's the code, based on the example in section 9.65 of gfortran.pdf

SUBROUTINE Compiler_Options ( Options, Version, WriteOpt )

   USE ISO_FORTRAN_ENV, ONLY : Compiler_Version, Compiler_Options
   IMPLICIT NONE
   CHARACTER (LEN=*), INTENT(OUT) :: Options
   CHARACTER (LEN=*), INTENT(OUT) :: Version
   LOGICAL, INTENT(IN), OPTIONAL  :: WriteOpt

   !----------------------------------------------------------------------------

   Version = Compiler_Version()
   Options = Compiler_Options()
   IF ( PRESENT(WriteOpt) ) THEN
      IF ( WriteOpt ) &
         PRINT '(A)', 'This file was compiled using gfortran by ' &
               // TRIM(Version) // ' using the options ' // TRIM(Options)
   END IF

   RETURN

END SUBROUTINE Compiler_Options
Comment 1 Dominique d'Humieres 2016-06-24 15:54:17 UTC
Confirmed from 4.8 up to trunk (7.0) without any option. The backtrace is

* thread #1: tid = 0xfd5298, 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp + 181, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp + 181
libsystem_platform.dylib`_platform_strcmp:
->  0x7fff9feb78b5 <+181>: movdqu (%rsi,%rcx), %xmm1
    0x7fff9feb78ba <+186>: pcmpeqb %xmm1, %xmm0
    0x7fff9feb78be <+190>: pcmpeqb %xmm2, %xmm1
    0x7fff9feb78c2 <+194>: pandn  %xmm0, %xmm1
(lldb) bt
* thread #1: tid = 0xfd5298, 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp + 181, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp + 181
    frame #1: 0x000000010006a3e2 f951`::create_intrinsic_function(name="compiler_options", id=27, modname="iso_fortran_env", module=INTMOD_ISO_FORTRAN_ENV, subroutine=<unavailable>, result_type=0x0000000000000000) + 82 at module.c:6170
    frame #2: 0x000000010006cc45 f951`::use_iso_fortran_env_module() + 773 at module.c:6752
    frame #3: 0x0000000100072910 f951`::gfc_use_module(module=0x000000014270a050) + 6384 at module.c:6881
    frame #4: 0x00000001000744eb f951`gfc_use_modules() + 539 at module.c:7112
    frame #5: 0x000000010008231e f951`::use_modules() + 46 at parse.c:114
    frame #6: 0x0000000100086335 f951`::decode_statement() + 949 at parse.c:332
    frame #7: 0x0000000100088174 f951`::next_statement() + 276 at parse.c:1080
    frame #8: 0x0000000100089dcd f951`::parse_spec(st=ST_USE) + 3325 at parse.c:3637
    frame #9: 0x000000010008d0d7 f951`::parse_progunit(st=<unavailable>) + 39 at parse.c:5420
    frame #10: 0x000000010008ed63 f951`gfc_parse_file() + 2003 at parse.c:5944
    frame #11: 0x00000001000d625b f951`::gfc_be_parse_file() + 59 at f95-lang.c:201
    frame #12: 0x0000000100b67aba f951`::compile_file() + 58 at toplev.c:465
    frame #13: 0x0000000101051114 f951`toplev::main(int, char**) + 1544 at toplev.c:1998
    frame #14: 0x0000000101050b0c f951`toplev::main(this=0x00007fff5fbff340, argc=<unavailable>, argv=<unavailable>) + 732
    frame #15: 0x0000000101052979 f951`main(argc=2, argv=0x00007fff5fbff380) + 41 at main.c:39
    frame #16: 0x00007fff9ea105ad libdyld.dylib`start + 1

(lldb) p *tmp_symtree
(gfc_symtree) $1 = {
  priority = 1978153288
  left = 0x84048d49a4048d4b
  right = 0x00000080c7848b41
  name = 0x5c415d5b08c48348 <no value available>
  ambiguous = 1581342017
  n = {
    sym = 0x6c266a358b00401f
    uop = 0x6c266a358b00401f
    common = 0x6c266a358b00401f
    tb = 0x6c266a358b00401f
    omp_udr = 0x6c266a358b00401f
  }
}
(lldb) p tmp_symtree->n.sym->module
error: Couldn't apply expression side effects : Couldn't dematerialize a result variable: couldn't read its memory

Could someone check that the problem is not darwin specific?
Comment 2 jack.saba@nasa.gov 2016-06-24 15:56:30 UTC
I tried it on a ubuntu machine and got the same error.

Jack Saba
jack.saba@nasa.gov
Science, Systems, and Applications, Inc.
Cryospheric Sciences Laboratory, Code 615
Bldg 33 Room A328
NASA/Goddard Space Flight Center
Greenbelt Md 20771
301-614-5878
Fax: 301-614-5644

On 6/24/16 11:54, dominiq at lps dot ens.fr wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71649
>
> Dominique d'Humieres <dominiq at lps dot ens.fr> changed:
>
>            What    |Removed                     |Added
> ----------------------------------------------------------------------------
>              Status|UNCONFIRMED                 |WAITING
>    Last reconfirmed|                            |2016-06-24
>                  CC|                            |fxcoudert at gcc dot gnu.org
>      Ever confirmed|0                           |1
>
> --- Comment #1 from Dominique d'Humieres <dominiq at lps dot ens.fr> ---
> Confirmed from 4.8 up to trunk (7.0) without any option. The backtrace is
>
> * thread #1: tid = 0xfd5298, 0x00007fff9feb78b5
> libsystem_platform.dylib`_platform_strcmp + 181, queue =
> 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
>     frame #0: 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp +
> 181
> libsystem_platform.dylib`_platform_strcmp:
> ->  0x7fff9feb78b5 <+181>: movdqu (%rsi,%rcx), %xmm1
>     0x7fff9feb78ba <+186>: pcmpeqb %xmm1, %xmm0
>     0x7fff9feb78be <+190>: pcmpeqb %xmm2, %xmm1
>     0x7fff9feb78c2 <+194>: pandn  %xmm0, %xmm1
> (lldb) bt
> * thread #1: tid = 0xfd5298, 0x00007fff9feb78b5
> libsystem_platform.dylib`_platform_strcmp + 181, queue =
> 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
>   * frame #0: 0x00007fff9feb78b5 libsystem_platform.dylib`_platform_strcmp +
> 181
>     frame #1: 0x000000010006a3e2
> f951`::create_intrinsic_function(name="compiler_options", id=27,
> modname="iso_fortran_env", module=INTMOD_ISO_FORTRAN_ENV,
> subroutine=<unavailable>, result_type=0x0000000000000000) + 82 at module.c:6170
>     frame #2: 0x000000010006cc45 f951`::use_iso_fortran_env_module() + 773 at
> module.c:6752
>     frame #3: 0x0000000100072910
> f951`::gfc_use_module(module=0x000000014270a050) + 6384 at module.c:6881
>     frame #4: 0x00000001000744eb f951`gfc_use_modules() + 539 at module.c:7112
>     frame #5: 0x000000010008231e f951`::use_modules() + 46 at parse.c:114
>     frame #6: 0x0000000100086335 f951`::decode_statement() + 949 at parse.c:332
>     frame #7: 0x0000000100088174 f951`::next_statement() + 276 at parse.c:1080
>     frame #8: 0x0000000100089dcd f951`::parse_spec(st=ST_USE) + 3325 at
> parse.c:3637
>     frame #9: 0x000000010008d0d7 f951`::parse_progunit(st=<unavailable>) + 39
> at parse.c:5420
>     frame #10: 0x000000010008ed63 f951`gfc_parse_file() + 2003 at parse.c:5944
>     frame #11: 0x00000001000d625b f951`::gfc_be_parse_file() + 59 at
> f95-lang.c:201
>     frame #12: 0x0000000100b67aba f951`::compile_file() + 58 at toplev.c:465
>     frame #13: 0x0000000101051114 f951`toplev::main(int, char**) + 1544 at
> toplev.c:1998
>     frame #14: 0x0000000101050b0c f951`toplev::main(this=0x00007fff5fbff340,
> argc=<unavailable>, argv=<unavailable>) + 732
>     frame #15: 0x0000000101052979 f951`main(argc=2, argv=0x00007fff5fbff380) +
> 41 at main.c:39
>     frame #16: 0x00007fff9ea105ad libdyld.dylib`start + 1
>
> (lldb) p *tmp_symtree
> (gfc_symtree) $1 = {
>   priority = 1978153288
>   left = 0x84048d49a4048d4b
>   right = 0x00000080c7848b41
>   name = 0x5c415d5b08c48348 <no value available>
>   ambiguous = 1581342017
>   n = {
>     sym = 0x6c266a358b00401f
>     uop = 0x6c266a358b00401f
>     common = 0x6c266a358b00401f
>     tb = 0x6c266a358b00401f
>     omp_udr = 0x6c266a358b00401f
>   }
> }
> (lldb) p tmp_symtree->n.sym->module
> error: Couldn't apply expression side effects : Couldn't dematerialize a result
> variable: couldn't read its memory
>
> Could someone check that the problem is not darwin specific?
>
Comment 3 Jerry DeLisle 2016-06-24 19:09:31 UTC
possible patch:

diff --git a/gcc/fortran/module.c b/gcc/fortran/module.c
index 6d3860e..c4c6cb4 100644
--- a/gcc/fortran/module.c
+++ b/gcc/fortran/module.c
@@ -6157,7 +6157,7 @@ create_intrinsic_function (const char *name, int id,
   gfc_symbol *sym;
 
   tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
-  if (tmp_symtree)
+  if (tmp_symtree && tmp_symtree->n.sym && tmp_symtree->n.sym->module)
     {
       if (strcmp (modname, tmp_symtree->n.sym->module) == 0)
         return;

With this code, the example compiles with errors. Does not like the subroutine to have same name as the function in ISO_FORTRAN_ENV Compiler_Options.
Comment 4 Jerry DeLisle 2016-06-24 22:55:05 UTC
(In reply to jack.saba@nasa.gov from comment #2)

> >
> > Could someone check that the problem is not darwin specific?
> >

Not darwin specific. Is the sample code valid?
Comment 5 Dominique d'Humieres 2016-06-25 09:01:20 UTC
> Not darwin specific. Is the sample code valid?

Good question! The ICE disappears if I rename the subroutine Compiler_Options or the ISO_FORTRAN_ENV one, e.g., opts => Compiler_Options. Anyway an ICE is not user friendly.
Comment 6 kargl 2016-06-25 14:37:11 UTC
(In reply to Jerry DeLisle from comment #4)
> (In reply to jack.saba@nasa.gov from comment #2)
> 
> > >
> > > Could someone check that the problem is not darwin specific?
> > >
> 
> Not darwin specific. Is the sample code valid?

Of course the code is invalid.  Op has 

subroutine foo()
   implicit none
   i = foo()
end subroutine foo

1) Subroutines are not invoked as a function.
2) For a subroutine to invoke itself, it needs
   the recursive attribute.
3) Subroutines have no type.

Your patch is almost correct (IMO).  It should move
the check to the next conditional.

   if (tmp_symtree)
     {
       if (tmp_symtree->n.sym
           && tmp_symtree->n.sym->module
           && strcmp (modname, tmp_symtree->n.sym->module) == 0)
         return;

The error message shuold then be generated.
Comment 7 Jerry DeLisle 2016-06-25 15:30:19 UTC
I will regtest and commit the patch sometime today.
Comment 8 Jerry DeLisle 2016-06-25 23:10:41 UTC
With:

  if (tmp_symtree)
    {
      if (tmp_symtree->n.sym && tmp_symtree->n.sym->module
	  && strcmp (modname, tmp_symtree->n.sym->module) == 0)
	return;
      gfc_error ("Symbol %qs at %C already declared", name);
      return;
    }

We now get:

$ gfc -c pr71649.f90 
pr71649.f90:3:7:

    USE ISO_FORTRAN_ENV, ONLY : Compiler_Version, Compiler_Options
       1
Error: Symbol ‘compiler_options’ at (1) already declared
pr71649.f90:12:29:

    Options = Compiler_Options()
                             1
Error: Unexpected use of subroutine name ‘compiler_options’ at (1)

Which now makes sense.
Comment 9 Jerry DeLisle 2016-06-26 01:03:51 UTC
Author: jvdelisle
Date: Sun Jun 26 01:03:19 2016
New Revision: 237789

URL: https://gcc.gnu.org/viewcvs?rev=237789&root=gcc&view=rev
Log:
2016-06-25  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/71649
	* module.c (create_intrinsic_function): Check for NULL values and
	return after giving error.

	PR fortran/71649
	* gfortran.dg/pr71649.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pr71649.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/module.c
    trunk/gcc/testsuite/ChangeLog
Comment 10 Jerry DeLisle 2016-06-26 01:09:32 UTC
Fixed on trunk. Closing