Bug 65550 - [4.8/4.9 Regression] ICE (segfault) with pch
Summary: [4.8/4.9 Regression] ICE (segfault) with pch
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: pch (show other bugs)
Version: 5.0
: P2 normal
Target Milestone: 4.8.5
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-invalid-code
Depends on:
Blocks: 55399
  Show dependency treegraph
 
Reported: 2015-03-25 14:32 UTC by Matthias Klose
Modified: 2018-02-13 04:52 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.7.4, 5.0
Known to fail: 4.8.3, 4.9.2, 5.0
Last reconfirmed: 2015-03-25 00:00:00


Attachments
Reproducer (227 bytes, application/x-shellscript)
2015-04-04 04:45 UTC, Mikhail Maltsev
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthias Klose 2015-03-25 14:32:18 UTC
seen building the mp4v2 package in Debian unstable with r221551 and pch enabled. disabling pch lets the package build succeed.

Program received signal SIGABRT, Aborted.
0x00007ffff690a107 in raise () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0  0x00007ffff690a107 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff690b4e8 in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x000000000136f69a in linemap_add(line_maps*, lc_reason, unsigned int, char const*, unsigned int) ()
#3  0x000000000136f913 in linemap_line_start(line_maps*, unsigned int, unsigned int) ()
#4  0x000000000136fae1 in linemap_position_for_column(line_maps*, unsigned int) ()
#5  0x000000000136dbc2 in _cpp_lex_direct ()
#6  0x000000000136eaee in _cpp_lex_token ()
#7  0x0000000001373a98 in cpp_get_token_1(cpp_reader*, unsigned int*) ()
#8  0x00000000008760ed in c_lex_with_flags(tree_node**, unsigned int*, unsigned char*, int) ()
#9  0x0000000000715a50 in cp_lexer_get_preprocessor_token(cp_lexer*, cp_token*) ()
#10 0x000000000074c9df in c_parse_file() ()
#11 0x000000000087e673 in c_common_parse_file() ()
#12 0x0000000000cc2662 in compile_file() ()
#13 0x0000000000611a3d in toplev::main(int, char**) ()
#14 0x00000000006128fa in main ()
Comment 1 Markus Trippelsdorf 2015-03-25 15:12:35 UTC
Doesn't happen with --enable-checking=release.

src/bmff/impl.h:126:5: internal compiler error: Aborted
0xcaf40f crash_signal
        ../../gcc/gcc/toplev.c:383
0x1339c09 linemap_add(line_maps*, lc_reason, unsigned int, char const*, unsigned int)
        ../../gcc/libcpp/line-map.c:297
0x1339e82 linemap_line_start(line_maps*, unsigned int, unsigned int)
        ../../gcc/libcpp/line-map.c:566
0x133a050 linemap_position_for_column(line_maps*, unsigned int)
        ../../gcc/libcpp/line-map.c:611
0x1338131 _cpp_lex_direct
        ../../gcc/libcpp/lex.c:2329
0x133905d _cpp_lex_token
        ../../gcc/libcpp/lex.c:2166
0x133e007 cpp_get_token_1
        ../../gcc/libcpp/macro.c:2442
0x86168c c_lex_with_flags(tree_node**, unsigned int*, unsigned char*, int)
        ../../gcc/gcc/c-family/c-lex.c:408
0x70101f cp_lexer_get_preprocessor_token
        ../../gcc/gcc/cp/parser.c:779
0x737fae cp_lexer_new_main
        ../../gcc/gcc/cp/parser.c:660
0x737fae cp_parser_new
        ../../gcc/gcc/cp/parser.c:3484
0x737fae c_parse_file()
        ../../gcc/gcc/cp/parser.c:33190
0x869c12 c_common_parse_file()
        ../../gcc/gcc/c-family/c-opts.c:1057
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.
Comment 2 Jack Howarth 2015-03-27 13:52:23 UTC
This bug may be a duplicate of Bug 61250.
Comment 3 Richard Biener 2015-03-31 13:09:44 UTC
Bisect?  Reduce to a set of files to produce the PCH and a file to use it?
Comment 4 Mikhail Maltsev 2015-04-04 04:45:40 UTC
Created attachment 35228 [details]
Reproducer

Reduced testcase. The script should be invoked like this:
CXX=/path/to/gcc-5.0/bin/g++ ice_pch.sh

It creates 3 files and invokes GCC 2 times (one for PCH creation, one for compilation which causes ICE on current trunk). Note, that whitespaces in source files matter.
Comment 5 Mikhail Maltsev 2015-04-04 08:26:35 UTC
Or even simpler:

#!/bin/bash
mkdir -p ./src
mkdir -p ./build/src/preproc.h.gch

touch ./src/preproc.h

cat << EOF >./src/include.h
#include "src/preproc.h"
/* line 1 */
/* line 2 */
EOF

cat << EOF >./src/source.cpp
#include "include.h"
/* a single line with at least 128 columns and one token */                                                                                                token
EOF

cd ./build
cc1plus ../src/preproc.h --output-pch=src/preproc.h.gch/pch_file
cc1plus -I . ../src/source.cpp
Comment 6 Richard Biener 2015-04-07 10:55:49 UTC
Confirmed.

0xfb7742 crash_signal
        /space/rguenther/src/svn/trunk2/gcc/toplev.c:383
0x197315c linemap_add(line_maps*, lc_reason, unsigned int, char const*, unsigned int)
        /space/rguenther/src/svn/trunk2/libcpp/line-map.c:303
0x1973b1e linemap_line_start(line_maps*, unsigned int, unsigned int)
        /space/rguenther/src/svn/trunk2/libcpp/line-map.c:566
0x1973d55 linemap_position_for_column(line_maps*, unsigned int)
        /space/rguenther/src/svn/trunk2/libcpp/line-map.c:611
0x1970744 _cpp_lex_direct
        /space/rguenther/src/svn/trunk2/libcpp/lex.c:2340
0x197029a _cpp_lex_token
        /space/rguenther/src/svn/trunk2/libcpp/lex.c:2176
0x197996f cpp_get_token_1
        /space/rguenther/src/svn/trunk2/libcpp/macro.c:2442
0x1979db7 cpp_get_token_with_location(cpp_reader*, unsigned int*)
        /space/rguenther/src/svn/trunk2/libcpp/macro.c:2628
0x9e2ea3 c_lex_with_flags(tree_node**, unsigned int*, unsigned char*, int)
        /space/rguenther/src/svn/trunk2/gcc/c-family/c-lex.c:408
#2  0x000000000197315d in linemap_add (set=0x10000fe000, reason=LC_RENAME, 
    sysp=0, to_file=0x24ad430 "../src/include.h", to_line=3)
    at /space/rguenther/src/svn/trunk2/libcpp/line-map.c:303
303       linemap_assert (!(set->depth == 0 && reason == LC_RENAME));
(gdb) p set->depth
$2 = 0


It's correctly initialized by PCH but re-set at

Hardware watchpoint 1: *$10

Old value = 1
New value = 0
linemap_add (set=0x10000fe000, reason=LC_LEAVE, sysp=0, to_file=0x0, to_line=0)
    at /space/rguenther/src/svn/trunk2/libcpp/line-map.c:311
311           return NULL;
(gdb) bt
#0  linemap_add (set=0x10000fe000, reason=LC_LEAVE, sysp=0, to_file=0x0, 
    to_line=0) at /space/rguenther/src/svn/trunk2/libcpp/line-map.c:311
#1  0x000000000195e29f in _cpp_do_file_change (pfile=0x249a670, 
    reason=LC_LEAVE, to_file=0x0, file_line=0, sysp=0)
    at /space/rguenther/src/svn/trunk2/libcpp/directives.c:1067
#2  0x0000000001961a5d in _cpp_pop_buffer (pfile=0x249a670)
    at /space/rguenther/src/svn/trunk2/libcpp/directives.c:2606
#3  0x00000000019704a7 in _cpp_get_fresh_line (pfile=0x249a670)
    at /space/rguenther/src/svn/trunk2/libcpp/lex.c:2255
#4  0x0000000001970587 in _cpp_lex_direct (pfile=0x249a670)
    at /space/rguenther/src/svn/trunk2/libcpp/lex.c:2302
#5  0x000000000197029b in _cpp_lex_token (pfile=0x249a670)
    at /space/rguenther/src/svn/trunk2/libcpp/lex.c:2176
#6  0x0000000001979970 in cpp_get_token_1 (pfile=0x249a670, 
    location=0x7fffffffdc58)
...
#10 0x000000000086afd3 in cp_parser_initial_pragma (first_token=0x7fffffffdc50)
    at /space/rguenther/src/svn/trunk2/gcc/cp/parser.c:32854
#11 0x000000000082eaf7 in cp_lexer_new_main ()
    at /space/rguenther/src/svn/trunk2/gcc/cp/parser.c:650
#12 0x0000000000831d4a in cp_parser_new ()
    at /space/rguenther/src/svn/trunk2/gcc/cp/parser.c:3484
#13 0x000000000086b787 in c_parse_file ()
    at /space/rguenther/src/svn/trunk2/gcc/cp/parser.c:33189



But note that this is an usupported use of PCH - it's included indirectly.
We should have rejected it but we don't.  Appearantly my fix for that doesn't
trigger:

2009-09-22  Richard Guenther  <rguenther@suse.de>

        PR pch/38987
        * files.c (pch_open_file): Disallow non-toplevel PCH inclusion.

because of the implicit-preinclude check.

(gdb) p pfile->all_files->implicit_preinclude
$30 = false
(gdb) p pfile->all_files->next_file->implicit_preinclude
$31 = true

I don't understand why we check two files here...

(gdb) p pfile->all_files->name
$33 = 0x24ad0c0 "include.h"
(gdb) p pfile->all_files->next_file->name
$34 = 0x24ac120 "stdc-predef.h"
(gdb) p pfile->all_files->next_file->next_file->name
$35 = 0x24e3be0 "../src/source.cpp"

note how the main file is "last".  My patch got two revisions - r192715 by
Joseph and r193709 by Steve.

I think the code should be changed to sth like

  /* If the file is not included as first include from either the toplevel
     file or the command-line it is not a valid use of PCH.  */
  for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
    if (f->implicit_preinclude)
      continue;
    else if (f->main_file)
      break;
    else
      return false;

note the change triggering this bug is in GCC 4.8 already (but not in 4.7).


Index: libcpp/files.c
===================================================================
--- libcpp/files.c      (revision 221891)
+++ libcpp/files.c      (working copy)
@@ -291,11 +291,13 @@ pch_open_file (cpp_reader *pfile, _cpp_f
 
   /* If the file is not included as first include from either the toplevel
      file or the command-line it is not a valid use of PCH.  */
-  if (pfile->all_files
-      && pfile->all_files->next_file
-      && !(pfile->all_files->implicit_preinclude
-          || pfile->all_files->next_file->implicit_preinclude))
-    return false;
+  for (_cpp_file *f = pfile->all_files; f; f = f->next_file)
+    if (f->implicit_preinclude)
+      continue;
+    else if (f->main_file)
+      break;
+    else
+      return false;
 
   flen = strlen (path);
   len = flen + sizeof (extension);
Comment 7 Richard Biener 2015-04-09 13:38:18 UTC
Fixed on trunk sofar.
Comment 8 Richard Biener 2015-04-09 13:38:25 UTC
Author: rguenth
Date: Thu Apr  9 13:37:53 2015
New Revision: 221949

URL: https://gcc.gnu.org/viewcvs?rev=221949&root=gcc&view=rev
Log:
2015-04-09  Richard Biener  <rguenther@suse.de>

	PR pch/65550
	* files.c (pch_open_file): Allow main and pre-included files
	when trying to open a PCH.

Modified:
    trunk/libcpp/ChangeLog
    trunk/libcpp/files.c
Comment 9 Jakub Jelinek 2015-06-12 07:55:30 UTC
I suppose this is backportable to both 4.8 and 4.9.
Comment 10 rguenther@suse.de 2015-06-12 11:39:38 UTC
On June 12, 2015 9:55:30 AM GMT+02:00, "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> wrote:
>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65550
>
>Jakub Jelinek <jakub at gcc dot gnu.org> changed:
>
>           What    |Removed                     |Added
>----------------------------------------------------------------------------
>               CC|                            |jakub at gcc dot gnu.org
>
>--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
>I suppose this is backportable to both 4.8 and 4.9.

Yes.  I must have missed it because the bug is not assigned to me...  Let's see if I can get to it before the RC
Comment 11 Jakub Jelinek 2015-06-12 11:56:40 UTC
Author: jakub
Date: Fri Jun 12 11:56:08 2015
New Revision: 224422

URL: https://gcc.gnu.org/viewcvs?rev=224422&root=gcc&view=rev
Log:
	Backported from mainline
	2015-04-09  Richard Biener  <rguenther@suse.de>

	PR pch/65550
	* files.c (pch_open_file): Allow main and pre-included files
	when trying to open a PCH.

Modified:
    branches/gcc-4_9-branch/libcpp/ChangeLog
    branches/gcc-4_9-branch/libcpp/files.c
Comment 12 Jakub Jelinek 2015-06-12 11:57:11 UTC
Author: jakub
Date: Fri Jun 12 11:56:39 2015
New Revision: 224423

URL: https://gcc.gnu.org/viewcvs?rev=224423&root=gcc&view=rev
Log:
	Backported from mainline
	2015-04-09  Richard Biener  <rguenther@suse.de>

	PR pch/65550
	* files.c (pch_open_file): Allow main and pre-included files
	when trying to open a PCH.

Modified:
    branches/gcc-4_8-branch/libcpp/ChangeLog
    branches/gcc-4_8-branch/libcpp/files.c
Comment 13 Jakub Jelinek 2015-06-12 11:59:20 UTC
Fixed.