NetBSD Problem Report #40066

From tsutsui@ceres.dti.ne.jp  Sat Nov 29 14:42:17 2008
Return-Path: <tsutsui@ceres.dti.ne.jp>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by narn.NetBSD.org (Postfix) with ESMTP id D4E7E63B11D
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 29 Nov 2008 14:42:17 +0000 (UTC)
Message-Id: <200811291442.mATEgDfh025221@mirage.ceres.dti.ne.jp>
Date: Sat, 29 Nov 2008 23:42:13 +0900 (JST)
From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
Reply-To: tsutsui@ceres.dti.ne.jp
To: gnats-bugs@gnats.NetBSD.org
Cc: tsutsui@ceres.dti.ne.jp
Subject: gcc -O1 generates wrong code in loop optimize
X-Send-Pr-Version: 3.95

>Number:         40066
>Category:       toolchain
>Synopsis:       gcc -O1 generates wrong code in loop optimize
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    toolchain-manager
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Nov 29 14:45:00 +0000 2008
>Closed-Date:    Sat Jun 30 21:53:01 +0000 2012
>Last-Modified:  Sat Jun 30 21:53:01 +0000 2012
>Originator:     Izumi Tsutsui
>Release:        NetBSD 5.0_BETA
>Organization:
>Environment:
System: NetBSD 5.0_BETA i386
Architecture: i386
Machine: i386
also happens on m68k

>Description:
As described in bin/40036, gcc(1) generates wrong code around
an assignment of structure with union in nested loops
(like src/sbin/ifconfig.c:init_afs() ) if -O1 is specified.

This doesn't happens if -O2 or -fno-loop-optimize is specfied (see below).

>How-To-Repeat:

% uname -mrs
NetBSD 5.0_BETA i386
% gcc --version
gcc (GCC) 4.1.3 20080704 prerelease (NetBSD nb1 20080202)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

% cat test.c
#include <inttypes.h>
#include <stdio.h>

struct kwinst {
	int			k_type;
	const char		*k_word;
	union kwval {
	        int64_t		u_sint;
		char		*u_str;
	} k_u;
#define k_int   k_u.u_sint
};

struct kwinst fkw[10];
int num[] = { 11, 22, 33, 44 };
char *name[] = {"aaaaaa","bbbbbb","cccccc","dddddd"};

main()
{
	int a, i;
	struct kwinst kw = {.k_type = 1};

	for (a = 0; a < 4; a++) {
		kw.k_int = num[a];
		kw.k_word = name[a];
		for (i = 0; i < 10; i++) {
			if (fkw[i].k_word == NULL) {
				fkw[i] = kw;
				break;
			}
		}
		printf("a=%d: kw.k_word=%s kw.k_int=%d, "
		    "fkw[%d].k_word=%s fkw[%d].k_int=%d\n", a,
		    kw.k_word, (int)kw.k_int,
		    i, fkw[i].k_word, i, (int)fkw[i].k_int);
	}
}

% cc -O test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=(null) fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[0].k_word=aaaaaa fkw[0].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[1].k_word=bbbbbb fkw[1].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[2].k_word=cccccc fkw[2].k_int=44
% cc -O -fno-loop-optimize test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=aaaaaa fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[1].k_word=bbbbbb fkw[1].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[2].k_word=cccccc fkw[2].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[3].k_word=dddddd fkw[3].k_int=44
% cc -O2 test.c
% ./a.out
a=0: kw.k_word=aaaaaa kw.k_int=11, fkw[0].k_word=aaaaaa fkw[0].k_int=11
a=1: kw.k_word=bbbbbb kw.k_int=22, fkw[1].k_word=bbbbbb fkw[1].k_int=22
a=2: kw.k_word=cccccc kw.k_int=33, fkw[2].k_word=cccccc fkw[2].k_int=33
a=3: kw.k_word=dddddd kw.k_int=44, fkw[3].k_word=dddddd fkw[3].k_int=44
% 

>Fix:
Unknown.  Workaround is -O2 or -fno-loop-optimize.

>Release-Note:

>Audit-Trail:
From: Izumi Tsutsui <tsutsui@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/40066 CVS commit: src/sbin/ifconfig
Date: Sat, 29 Nov 2008 15:28:49 +0000 (UTC)

 Module Name:	src
 Committed By:	tsutsui
 Date:		Sat Nov 29 15:28:49 UTC 2008

 Modified Files:
 	src/sbin/ifconfig: Makefile

 Log Message:
 Specify -fno-loop-optimize on m68000 as workaround for gcc -O1 codegen bug
 mentioned in PR bin/40036 and PR toolchain/40066.


 To generate a diff of this commit:
 cvs rdiff -r1.38 -r1.39 src/sbin/ifconfig/Makefile

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: matthew green <mrg@eterna.com.au>
To: gnats-bugs@NetBSD.org
Cc: toolchain-manager@netbsd.org, gnats-admin@netbsd.org,
    netbsd-bugs@netbsd.org
Subject: re: toolchain/40066: gcc -O1 generates wrong code in loop optimize 
Date: Sun, 30 Nov 2008 08:47:38 +1100

 this problem does not occur on sparc64 or amd64 with 64 bit
 binaries.

 on amd64 with "cc -m32" it also works fine.  with "-march=i386" it fails.
 with "-march=i686" it works.  this is kind of bizarre.


 .mrg.

From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
To: gnats-bugs@NetBSD.org
Cc: toolchain-manager@NetBSD.org, gnats-admin@NetBSD.org,
        matthew green <mrg@eterna.com.au>, tsutsui@ceres.dti.ne.jp
Subject: Re: toolchain/40066: gcc -O1 generates wrong code in loop optimize

Date: Sun, 30 Nov 2008 13:53:54 +0900

 mrg@eterna.com.au wrote:

 >  this problem does not occur on sparc64 or amd64 with 64 bit
 >  binaries.
 >  
 >  on amd64 with "cc -m32" it also works fine.  with "-march=i386" it fails.
 >  with "-march=i686" it works.  this is kind of bizarre.

 More bizarre:

 ---
 struct kwinst {
         int                     k_type;
         const char              *k_word;
         union kwval {
                 int64_t         u_sint;
         } k_u;
 #define k_int   k_u.u_sint
 };
 ---

 This causes the similar problem (garbage in *k_word)
 while the following one works:

 ---
 struct kwinst {
         int                     k_type;
         const char              *k_word;
         int64_t			k_int;
 };
 ---

 Using int32_t instead of int64_t produces the same results,
 so the problem is around union in struct?
 Or any special behavior (un)defined in C99?
 ---
 Izumi Tsutsui

From: Izumi Tsutsui <tsutsui@ceres.dti.ne.jp>
To: gnats-bugs@NetBSD.org
Cc: toolchain-manager@NetBSD.org, gnats-admin@NetBSD.org,
        tsutsui@ceres.dti.ne.jp
Subject: Re: toolchain/40066: gcc -O1 generates wrong code in loop optimize
Date: Sun, 30 Nov 2008 16:56:02 +0900

 With some quick tests, plain gcc-4.1.2 has the same problem
 but gcc-4.2.0 doesn't.
 ---
 Izumi Tsutsui

From: Soren Jacobsen <snj@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/40066 CVS commit: [netbsd-5] src/sbin/ifconfig
Date: Sat,  6 Dec 2008 21:25:44 +0000 (UTC)

 Module Name:	src
 Committed By:	snj
 Date:		Sat Dec  6 21:25:44 UTC 2008

 Modified Files:
 	src/sbin/ifconfig [netbsd-5]: Makefile

 Log Message:
 Pull up following revision(s) (requested by tsutsui in ticket #164):
 	sbin/ifconfig/Makefile: revision 1.39
 Specify -fno-loop-optimize on m68000 as workaround for gcc -O1 codegen bug
 mentioned in PR bin/40036 and PR toolchain/40066.


 To generate a diff of this commit:
 cvs rdiff -r1.38 -r1.38.2.1 src/sbin/ifconfig/Makefile

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sat, 31 Dec 2011 18:59:55 +0000
State-Changed-Why:
Does this problem occur with gcc 4.5?


State-Changed-From-To: feedback->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sat, 30 Jun 2012 21:53:01 +0000
State-Changed-Why:
Since gcc 4.2 didn't have the bug, let's assume 4.5 doesn't either.
A workaround is in place for the stable branches that are still
affected.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.