Bug 50303 - [C++0x] Segfault with variadic template template parameters
Summary: [C++0x] Segfault with variadic template template parameters
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: 4.7.1
Assignee: Jason Merrill
URL: http://ideone.com/yrIfa
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-06 14:44 UTC by Andrea Arteaga
Modified: 2012-04-17 15:28 UTC (History)
3 users (show)

See Also:
Host: x86_64
Target: x86_64
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-09-14 00:00:00


Attachments
Preprocessed source (76.22 KB, application/octet-stream)
2011-09-06 14:44 UTC, Andrea Arteaga
Details
Not-preprocessed source (344 bytes, text/x-c++src)
2011-09-14 22:46 UTC, Andrea Arteaga
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrea Arteaga 2011-09-06 14:44:43 UTC
Created attachment 25203 [details]
Preprocessed source

A function "f" taking a class and a set (variadic) of template classes as template parameters is written. If this function passes the variadic parameter to another one, the compiler fails with a segfault. Otherwise the compiler just works.

This happens on Gentoo with gcc 4.6.1, on Fedora 15 with gcc-4.6.0, on the online service ideone (see http://ideone.com/yrIfa -- there is the original source too).

On Gentoo I have the following output:

$ g++ -v -save-temps -std=c++0x variadic_template.cpp -pedantic-errors

Using built-in specs.
Reading specs from /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/asneeded.specs
COLLECT_GCC=/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.1/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.6.1-r1/work/gcc-4.6.1/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.6.1 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.1 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.1/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.6.1/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --disable-fixed-point --without-ppl --without-cloog --enable-lto --enable-nls --without-included-gettext --with-system-zlib --disable-werror --enable-secureplt --enable-multilib --enable-libmudflap --disable-libssp --enable-libgomp --with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/4.6.1/python --enable-checking=release --enable-languages=c,c++,java,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.6.1-r1 p1.0, pie-0.4.5'
Thread model: posix
gcc version 4.6.1 (Gentoo 4.6.1-r1 p1.0, pie-0.4.5) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-pedantic-errors' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/4.6.1/cc1plus -E -quiet -v -D_GNU_SOURCE variadic_template.cpp -D_FORTIFY_SOURCE=2 -mtune=generic -march=x86-64 -std=c++0x -pedantic-errors -fpch-preprocess -o variadic_template.ii
ignoring nonexistent directory "/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/x86_64-pc-linux-gnu
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include/g++-v4/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-pedantic-errors' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-pc-linux-gnu/4.6.1/cc1plus -fpreprocessed variadic_template.ii -quiet -dumpbase variadic_template.cpp -mtune=generic -march=x86-64 -auxbase variadic_template -pedantic-errors -std=c++0x -version -o variadic_template.s
GNU C++ (Gentoo 4.6.1-r1 p1.0, pie-0.4.5) version 4.6.1 (x86_64-pc-linux-gnu)
	compiled by GNU C version 4.6.1, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++ (Gentoo 4.6.1-r1 p1.0, pie-0.4.5) version 4.6.1 (x86_64-pc-linux-gnu)
	compiled by GNU C version 4.6.1, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 510fa67dba4694d68782d987f500559c
variadic_template.cpp: In function ‘void f() [with Interface = I1, Actions = A1, A2]’:
variadic_template.cpp:58:16:   instantiated from here
variadic_template.cpp:49:58: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugs.gentoo.org/> for instructions.
Comment 1 Paolo Carlini 2011-09-14 20:59:20 UTC
Can you try reducing your testcase? 563 KB is definitely unmanageable.
Comment 2 Andrea Arteaga 2011-09-14 22:46:19 UTC
Created attachment 25275 [details]
Not-preprocessed source

As requested, I attach a minimal source code with some comments. There are no external dependencies, just <iostream> gets #included. The compilation result is the same as already stated (segmentation fault during compilation).
Comment 3 Andrea Arteaga 2011-09-14 22:47:56 UTC
I recently blogged about this issue: http://wp.me/pzWEm-9N.
Comment 4 Paolo Carlini 2011-09-14 23:11:11 UTC
Ah, ok, the below seems enough:

template<typename Interface>
struct A1 {
};

template<template<class I> class... Actions>
void g2() {
  g2<Actions...>();
}

int main()
{
  g2<A1>();
}
Comment 5 Markus Trippelsdorf 2011-09-15 07:06:19 UTC
Program received signal SIGSEGV, Segmentation fault.
[Switching to process 30771]
tsubst_template_parms (parms=0x0, args=0x7ffff77a6d98, complain=3) at ../../gcc/gcc/cp/pt.c:9507
9507           TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);

(gdb) bt
#0  tsubst_template_parms (parms=0x0, args=0x7ffff77a6d98, complain=3) at ../../gcc/gcc/cp/pt.c:9507
#1  0x00000000004c6d9a in tsubst_decl (t=0x7ffff77a7e60, args=0x7ffff77a6d98, complain=3) at ../../gcc/gcc/cp/pt.c:9789
#2  0x00000000004c3ce5 in tsubst (in_decl=<optimized out>, complain=3, args=0x7ffff77a6d98, t=0x7ffff77a7e60) at ../../gcc/gcc/cp/pt.c:10851
#3  tsubst (t=0x7ffff77a7e60, args=0x7ffff77a6d98, complain=3, in_decl=<optimized out>) at ../../gcc/gcc/cp/pt.c:10836
#4  0x00000000004c821f in tsubst_pack_expansion (t=0x7ffff77afe70, args=0x7ffff77a6f78, complain=3, in_decl=0x7ffff77a7f18)
    at ../../gcc/gcc/cp/pt.c:9298
#5  0x00000000004c656b in tsubst_template_args (t=0x7ffff77a6ac8, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18)
    at ../../gcc/gcc/cp/pt.c:9400
#6  0x00000000004c1e46 in tsubst_copy_and_build (t=0x7ffff7fd9a50, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18, function_p=1 '\001', 
    integral_constant_expression_p=<optimized out>) at ../../gcc/gcc/cp/pt.c:13035
#7  0x00000000004c1c9e in tsubst_copy_and_build (t=0x7ffff77bd038, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18, function_p=0 '\000', 
    integral_constant_expression_p=0 '\000') at ../../gcc/gcc/cp/pt.c:13390
#8  0x00000000004bd239 in tsubst_expr (t=0x7ffff77bd038, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18, 
    integral_constant_expression_p=0 '\000') at ../../gcc/gcc/cp/pt.c:12935
#9  0x00000000004bdc70 in tsubst_expr (t=0x7ffff77a6b40, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18, 
    integral_constant_expression_p=0 '\000') at ../../gcc/gcc/cp/pt.c:12467
#10 0x00000000004bd2b1 in tsubst_expr (t=0x7ffff77bd000, args=0x7ffff77a6d98, complain=3, in_decl=0x7ffff77a7f18, 
    integral_constant_expression_p=0 '\000') at ../../gcc/gcc/cp/pt.c:12632
#11 0x00000000004d0ccc in instantiate_decl (d=0x7ffff77aee00, defer_ok=<optimized out>, expl_inst_class_mem_p=<optimized out>)
    at ../../gcc/gcc/cp/pt.c:18305
#12 0x00000000004d34dc in instantiate_pending_templates (retries=<optimized out>) at ../../gcc/gcc/cp/pt.c:18402
#13 0x00000000004e8a4d in cp_write_global_declarations () at ../../gcc/gcc/cp/decl2.c:3713
#14 0x00000000007f11c2 in compile_file () at ../../gcc/gcc/toplev.c:564
#15 do_compile () at ../../gcc/gcc/toplev.c:1886
#16 toplev_main (argc=13, argv=0x7fffffffdf28) at ../../gcc/gcc/toplev.c:1962
#17 0x00007ffff7b81f82 in __libc_start_main (main=0x5a11b0 <main>, argc=13, ubp_av=0x7fffffffdf28, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffdf18) at libc-start.c:226
#18 0x000000000048cf89 in _start () at ../sysdeps/x86_64/elf/start.S:113
(gdb)
Comment 6 Jason Merrill 2012-04-17 02:29:32 UTC
Author: jason
Date: Tue Apr 17 02:29:27 2012
New Revision: 186519

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186519
Log:
	PR c++/50303
	* pt.c (tsubst_pack_expansion): Use tsubst_expr for template
	template parameters.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/variadic128.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/pt.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Jason Merrill 2012-04-17 14:11:04 UTC
Author: jason
Date: Tue Apr 17 14:10:54 2012
New Revision: 186531

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=186531
Log:
	PR c++/50303
	* pt.c (tsubst_pack_expansion): Use tsubst_expr for template
	template parameters.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/g++.dg/cpp0x/variadic128.C
Modified:
    branches/gcc-4_7-branch/gcc/cp/ChangeLog
    branches/gcc-4_7-branch/gcc/cp/pt.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
Comment 8 Jason Merrill 2012-04-17 15:28:08 UTC
Fixed for 4.7.1.