Bug 62142 - [4.9/5 Regression] internal compiler error: Segmentation fault (X = X - L*floor(X/L))
Summary: [4.9/5 Regression] internal compiler error: Segmentation fault (X = X - L*flo...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.9.1
: P3 normal
Target Milestone: 4.9.2
Assignee: Thomas Koenig
URL:
Keywords: ice-on-invalid-code, ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2014-08-14 17:56 UTC by Ondřej Čertík
Modified: 2014-08-16 09:46 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.3
Known to fail: 4.9.1, 5.0
Last reconfirmed: 2014-08-15 00:00:00


Attachments
Source file to reproduce the segfault (126 bytes, text/x-fortran)
2014-08-14 17:56 UTC, Ondřej Čertík
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ondřej Čertík 2014-08-14 17:56:07 UTC
Created attachment 33327 [details]
Source file to reproduce the segfault

$ cat test_segfault.f90 
program test_segfault
implicit none
integer, parameter :: dp=kind(0.d0)
real(dp) :: L
real(dp), allocatable :: X(:)
X = X - L*floor(X/L)
end program

$ gfortran -v test_segfault.f90 
Driving: gfortran -v test_segfault.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/local/certik/bld/gcc/5att4mtprf52/libexec/gcc/x86_64-unknown-linux-gnu/4.9.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --prefix=/local/certik/bld/gcc/5att4mtprf52 --enable-checking=release --enable-languages=fortran,c,c++ --with-local-prefix=/local/certik/bld/gcc/5att4mtprf52 --with-gmp=/local/certik/bld/gmp/vadkrj43wtyr --with-mpc=/local/certik/bld/mpc/sushaq7ufe2f --with-mpfr=/local/certik/bld/mpfr/vxwmnxjsshse --with-cloog=/local/certik/bld/cloog/x3kd73rwqkzj --with-isl=/local/certik/bld/isl/lfrjunwloawr --enable-clocale=gnu --enable-__cxa_atexit --enable-shared --enable-threads=posix --disable-multilib --libdir=/local/certik/bld/gcc/5att4mtprf52/lib --with-stage1-ldflags='-L/local/certik/bld/cloog/x3kd73rwqkzj/lib -Wl,-rpath=/local/certik/bld/cloog/x3kd73rwqkzj/lib -L/local/certik/bld/gmp/vadkrj43wtyr/lib -Wl,-rpath=/local/certik/bld/gmp/vadkrj43wtyr/lib -L/local/certik/bld/isl/lfrjunwloawr/lib -Wl,-rpath=/local/certik/bld/isl/lfrjunwloawr/lib -L/local/certik/bld/mpc/sushaq7ufe2f/lib -Wl,-rpath=/local/certik/bld/mpc/sushaq7ufe2f/lib -L/local/certik/bld/mpfr/vxwmnxjsshse/lib -Wl,-rpath=/local/certik/bld/mpfr/vxwmnxjsshse/lib -L/local/certik/bld/patchelf/k3rloj265ogt/lib -Wl,-rpath=/local/certik/bld/patchelf/k3rloj265ogt/lib -L/local/certik/bld/zlib/3el5ccejre7b/lib -Wl,-rpath=/local/certik/bld/zlib/3el5ccejre7b/lib' --with-boot-ldflags='-L/local/certik/bld/cloog/x3kd73rwqkzj/lib -Wl,-rpath=/local/certik/bld/cloog/x3kd73rwqkzj/lib -L/local/certik/bld/gmp/vadkrj43wtyr/lib -Wl,-rpath=/local/certik/bld/gmp/vadkrj43wtyr/lib -L/local/certik/bld/isl/lfrjunwloawr/lib -Wl,-rpath=/local/certik/bld/isl/lfrjunwloawr/lib -L/local/certik/bld/mpc/sushaq7ufe2f/lib -Wl,-rpath=/local/certik/bld/mpc/sushaq7ufe2f/lib -L/local/certik/bld/mpfr/vxwmnxjsshse/lib -Wl,-rpath=/local/certik/bld/mpfr/vxwmnxjsshse/lib -L/local/certik/bld/patchelf/k3rloj265ogt/lib -Wl,-rpath=/local/certik/bld/patchelf/k3rloj265ogt/lib -L/local/certik/bld/zlib/3el5ccejre7b/lib -Wl,-rpath=/local/certik/bld/zlib/3el5ccejre7b/lib'
Thread model: posix
gcc version 4.9.1 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /local/certik/bld/gcc/5att4mtprf52/libexec/gcc/x86_64-unknown-linux-gnu/4.9.1/f951 test_segfault.f90 -quiet -dumpbase test_segfault.f90 -mtune=generic -march=x86-64 -auxbase test_segfault -version -fintrinsic-modules-path /local/certik/bld/gcc/5att4mtprf52/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/finclude -o /tmp/ccliCcO4.s
GNU Fortran (GCC) version 4.9.1 (x86_64-unknown-linux-gnu)
	compiled by GNU C version 4.9.1, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU Fortran (GCC) version 4.9.1 (x86_64-unknown-linux-gnu)
	compiled by GNU C version 4.9.1, GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
test_segfault.f90: In function ‘test_segfault’:
test_segfault.f90:6:0: internal compiler error: Segmentation fault
 X = X - L*floor(X/L)
 ^
0x90f44f crash_signal
	../.././gcc/toplev.c:337
0x604e0d is_runtime_conformable
	../.././gcc/fortran/trans-expr.c:7856
0x604e1b is_runtime_conformable
	../.././gcc/fortran/trans-expr.c:7856
0x611aa4 gfc_trans_assignment_1
	../.././gcc/fortran/trans-expr.c:8117
0x5e3fc5 trans_code
	../.././gcc/fortran/trans.c:1639
0x6033bb gfc_generate_function_code(gfc_namespace*)
	../.././gcc/fortran/trans-decl.c:5653
0x5a2f80 translate_all_program_units
	../.././gcc/fortran/parse.c:4953
0x5a2f80 gfc_parse_file()
	../.././gcc/fortran/parse.c:5150
0x5e0025 gfc_be_parse_file
	../.././gcc/fortran/f95-lang.c:212
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <http://gcc.gnu.org/bugs.html> for instructions.


When I execute "gfortran -v -save-temps  test_segfault.f90", it doesn't produce preprocessed sources, I assume the segfault happens in the parser. I have isolated the segfault, it happens in a large codebase and prevents its compilation (the array is allocated in our code, the above is the simplest code to reproduce the problem).

This is my first bug report, please let me know if you need further information.
Comment 1 Ondřej Čertík 2014-08-14 18:55:52 UTC
One can actually isolate it even further (the same stacktrace):

program test_segfault
implicit none
real, allocatable :: X(:)
X = floor(X)
end program
Comment 2 kargls 2014-08-14 19:03:14 UTC
(In reply to Ondřej Čertík from comment #0)
> Created attachment 33327 [details]
> Source file to reproduce the segfault
> 
> $ cat test_segfault.f90 
> program test_segfault
> implicit none
> integer, parameter :: dp=kind(0.d0)
> real(dp) :: L
> real(dp), allocatable :: X(:)
> X = X - L*floor(X/L)
> end program
> 

(snip)

> When I execute "gfortran -v -save-temps  test_segfault.f90", it doesn't
> produce preprocessed sources, I assume the segfault happens in the parser. I
> have isolated the segfault, it happens in a large codebase and prevents its
> compilation (the array is allocated in our code, the above is the simplest
> code to reproduce the problem).
> 
> This is my first bug report, please let me know if you need further
> information.

For such a short program, we don't necessarily need -save-temps.
Note, your reduce test case is nonconforming code because it 
uses L and X in a RHS expression without being defined.  With the
small change

program test_segfault
implicit none
integer, parameter :: dp=kind(0.d0)
real(dp) :: L
real(dp), allocatable :: X(:)
L = 42
X = [1, 2]
X = X - L*floor(X/L)
end program

the code is now conforming and it still exhibits the bug.

I suspect that the bug is due to a combination of -frealloc-lhs
and that floor is generated as inline code.  With -frealloc-lhs,
I get the same ICE that you find.  With -fno-realloc-lhs, I get
a compiled executable but it segfaults at runtime because x = [1,2]
obviously won't work.  If I add an allocate(x(2)) prior to 
x = [1,2], then -fno-realloc-lhs produced executable runs as
expected.  So, as a workaround explicitly allocate memory and
use -fno-realloc-lhs.
Comment 3 Ondřej Čertík 2014-08-14 19:17:24 UTC
Thanks Steve for the quick reply! I tried "-fno-realloc-lhs" and it fixes the problem for us (we always allocate arrays explicitly).
Comment 4 Dominique d'Humieres 2014-08-15 08:42:39 UTC
The codes in comments 0, 1, and 2 compile with gfortran 4.8, but not with 4.9 nor trunk (5.0). Revision r201916 (2013-08-22) is OK, r202111 (2013-08-30) gives the ICE, likely r202070.
Comment 5 Dominique d'Humieres 2014-08-15 08:51:58 UTC
I also get an ICE for

 X = ceiling(X)

and

 X = aint(X)

but not if I replace floor with sin or real.
Comment 6 Jakub Jelinek 2014-08-15 12:38:58 UTC
Indeed, this regressed with r202070.
Comment 7 Thomas Koenig 2014-08-15 20:34:06 UTC
A rather obvious fix:

Index: trans-expr.c
===================================================================
--- trans-expr.c        (Revision 213778)
+++ trans-expr.c        (Arbeitskopie)
@@ -7895,7 +7895,7 @@
          for (a = expr2->value.function.actual; a != NULL; a = a->next)                                  
            {                                                                                             
              e1 = a->expr;                                                                               
-             if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
+             if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
                return false;
            }
          return true;
@@ -7906,7 +7906,7 @@
          for (a = expr2->value.function.actual; a != NULL; a = a->next)
            {
              e1 = a->expr;
-             if (e1->rank > 0 && !is_runtime_conformable (expr1, e1))
+             if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
                return false;
            }
          return true;
Comment 8 Thomas Koenig 2014-08-15 21:20:06 UTC
Author: tkoenig
Date: Fri Aug 15 21:19:33 2014
New Revision: 214043

URL: https://gcc.gnu.org/viewcvs?rev=214043&root=gcc&view=rev
Log:
2014-08-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/62142
	* trans-expr.c (is_runtime_conformable):  Add NULL pointer checks.

2014-08-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/62142
	* gfortran.dg/realloc_on_assign_24.f90:  New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/realloc_on_assign_24.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Thomas Koenig 2014-08-16 09:45:35 UTC
Author: tkoenig
Date: Sat Aug 16 09:45:02 2014
New Revision: 214061

URL: https://gcc.gnu.org/viewcvs?rev=214061&root=gcc&view=rev
Log:
2014-08-16  Thomas Koenig  <tkoenig@gcc.gnu.org>

	Backport from trunk
	PR fortran/62142
	* trans-expr.c (is_runtime_conformable):  Add NULL pointer checks.

2014-08-15  Thomas Koenig  <tkoenig@gcc.gnu.org>

	Backport from trunk
	PR fortran/62142
	* gfortran.dg/realloc_on_assign_24.f90:  New test.


Added:
    branches/gcc-4_9-branch/gcc/testsuite/gfortran.dg/realloc_on_assign_24.f90
Modified:
    branches/gcc-4_9-branch/gcc/fortran/ChangeLog
    branches/gcc-4_9-branch/gcc/fortran/trans-expr.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
Comment 10 Thomas Koenig 2014-08-16 09:46:08 UTC
Fixed on all affected branches, closing.

Thanks for the bug report!