Bug 14804 - [3.4/4.0 Regression] [unit-at-a-time] initializing const data with reinterpret_cast-ed pointer-to-member function crashes
Summary: [3.4/4.0 Regression] [unit-at-a-time] initializing const data with reinterpre...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 critical
Target Milestone: 3.4.0
Assignee: Mark Mitchell
URL:
Keywords: patch, wrong-code
: 14640 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-04-01 01:32 UTC by David Baron
Modified: 2004-10-30 21:11 UTC (History)
7 users (show)

See Also:
Host: i686-gnu-linux-gnu
Target: i686-gnu-linux-gnu
Build: i686-gnu-linux-gnu
Known to work: 3.3.3 3.3.4
Known to fail: 3.4.0 4.0.0
Last reconfirmed: 2004-04-01 01:38:53


Attachments
testcase (163 bytes, text/plain)
2004-04-01 01:33 UTC, David Baron
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Baron 2004-04-01 01:32:43 UTC
This is somewhat similar to bug 12218 (which was fixed), but is a regression
between 20040105 and 20040311.

Initializing const data with a reinterpret_cast of a pointer-to-member virtual
function causes that const data to be emitted in the .rodata section (as it
should be) but causes initialization code to be generated to fill in the data.
(The reinterpret_cast is required for compilers other than gcc -- static_cast
works fine with gcc 3.4 and doesn't show the bug.)

This causes gcc to generate code that crashes on static initialization.

Testcase to be attached (simplified from nsEventListenerManager.cpp in Mozilla).
Comment 1 David Baron 2004-04-01 01:33:51 UTC
Created attachment 6022 [details]
testcase
Comment 2 Andrew Pinski 2004-04-01 01:38:53 UTC
Confirmed.
Comment 3 Andrew Pinski 2004-04-01 01:39:22 UTC
*** Bug 14640 has been marked as a duplicate of this bug. ***
Comment 4 David Baron 2004-04-01 01:43:01 UTC
Further testing shows that the bug happens the same way even if the function is
not virtual, as long as reinterpret_cast (rather than static_cast) is present. 
(And in the non-virtual case, when -fPIC is used, the data should be in
.rel.ro.data instead of .rodata -- which again works fine when static_cast is
used instead of reinterpret_cast.)

Why was this marked as a 3.3 regression?  The bug is not present in 3.3.3.
Comment 5 David Baron 2004-04-01 01:44:07 UTC
Ah, never mind.  I see the comments on bug 14640 indicating that it's a
regression on the 3.3 branch since 3.3.3.
Comment 6 Andrew Pinski 2004-04-01 01:45:49 UTC
Because it is broken on the 3.3 branch, see the dup bug.
Comment 7 David Baron 2004-04-01 01:48:05 UTC
It's also probably worth noting here what bug 14640 comment 8 says: that the
regression was caused by the backport of PR10776.
Comment 8 Mark Mitchell 2004-04-01 02:06:40 UTC
Retargeted at 3.4.0, since Mozilla depends on this.

Richard, does this problem have to do with your optimizations to the way we emit
aggregate initializers?
Comment 9 Richard Henderson 2004-04-01 02:24:13 UTC
Mine; testing patch.
Comment 10 Richard Henderson 2004-04-01 20:21:16 UTC
patch: http://gcc.gnu.org/ml/gcc-patches/2004-04/msg00076.html
Comment 11 GCC Commits 2004-04-01 20:45:07 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	rth@gcc.gnu.org	2004-04-01 20:45:02

Modified files:
	gcc/cp         : ChangeLog decl.c typeck2.c 
Added files:
	gcc/testsuite/g++.dg/init: static2.C 

Log message:
	PR c++/14804
	* decl.c (cp_finish_decl): Preserve TREE_READONLY more often.
	* typeck2.c (split_nonconstant_init): Clear TREE_READONLY.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4023&r2=1.4024
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&r1=1.1200&r2=1.1201
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&r1=1.155&r2=1.156
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/static2.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 12 GCC Commits 2004-04-01 20:47:31 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	rth@gcc.gnu.org	2004-04-01 20:47:27

Modified files:
	gcc/cp         : ChangeLog decl.c typeck2.c 
Added files:
	gcc/testsuite/g++.dg/init: static2.C 

Log message:
	PR c++/14804
	* decl.c (cp_finish_decl): Preserve TREE_READONLY more often.
	* typeck2.c (split_nonconstant_init): Clear TREE_READONLY.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.92&r2=1.3892.2.93
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.1174.2.18&r2=1.1174.2.19
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.153.4.2&r2=1.153.4.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/static2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1

Comment 13 GCC Commits 2004-04-01 21:48:15 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	rth@gcc.gnu.org	2004-04-01 21:48:06

Modified files:
	gcc/cp         : ChangeLog decl.c typeck2.c 
Added files:
	gcc/testsuite/g++.dg/init: static2.C 

Log message:
	PR c++/14804
	* decl.c (cp_finish_decl): Preserve TREE_READONLY more often.
	* typeck2.c (split_nonconstant_init): Clear TREE_READONLY.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.264&r2=1.3076.2.265
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.965.2.80&r2=1.965.2.81
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck2.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.132.4.5&r2=1.132.4.6
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/init/static2.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.4.1

Comment 14 Richard Henderson 2004-04-01 21:48:58 UTC
Fixed.
Comment 15 Serge Belyshev 2004-04-03 19:50:34 UTC
No, it is not. Testcase still segfaults if compiled with -funit-at-a-time.

$ g++ -O3 -fno-unit-at-a-time 14804.cc
$ ./a.out
$ g++ -funit-at-a-time 14804.cc
$ ./a.out 
Segmentation fault
$

This is true for both 3.4.0 and 3.5.0 from 20040403.
Comment 16 Andrew Pinski 2004-04-04 06:15:59 UTC
This is now only a 3.4.0/3.5.0 regression as unit-at-a-time is only in 3.4.0 and above.
Jan or RTH could you look into why unit-at-a-time could cause this.  I saw that GCC was 
putting the symbol still in read only data for unit-at-a-time.
Comment 17 Mark Mitchell 2004-04-04 19:31:34 UTC
Working on a fix.
Comment 18 GCC Commits 2004-04-04 22:50:32 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	mmitchel@gcc.gnu.org	2004-04-04 22:50:28

Modified files:
	gcc            : ChangeLog varasm.c 

Log message:
	PR c++/14804
	* varasm.c (initializer_constant_valid_p): Allow NOP_EXPRs to
	RECORD_TYPEs.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=2.2326.2.378&r2=2.2326.2.379
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/varasm.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.405.2.1&r2=1.405.2.2

Comment 19 GCC Commits 2004-04-04 22:51:07 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	mmitchel@gcc.gnu.org	2004-04-04 22:51:03

Modified files:
	gcc            : ChangeLog varasm.c 

Log message:
	PR c++/14804
	* varasm.c (initializer_constant_valid_p): Allow NOP_EXPRs to
	RECORD_TYPEs.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.3340&r2=2.3341
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/varasm.c.diff?cvsroot=gcc&r1=1.417&r2=1.418

Comment 20 Mark Mitchell 2004-04-04 22:52:50 UTC
Fixed it harder.
Comment 21 GCC Commits 2004-07-19 12:08:58 UTC
Subject: Bug 14804

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	hammer-3_3-branch
Changes by:	matz@gcc.gnu.org	2004-07-19 12:08:46

Modified files:
	gcc            : ChangeLog.hammer varasm.c 

Log message:
	Backport (for #42522):
	2004-04-04  Mark Mitchell  <mark@codesourcery.com>
	PR c++/14804
	* varasm.c (initializer_constant_valid_p): Allow NOP_EXPRs to
	RECORD_TYPEs.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.hammer.diff?cvsroot=gcc&only_with_tag=hammer-3_3-branch&r1=1.1.2.414&r2=1.1.2.415
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/varasm.c.diff?cvsroot=gcc&only_with_tag=hammer-3_3-branch&r1=1.318.2.18&r2=1.318.2.19