From nobody@FreeBSD.org  Mon Feb 17 17:45:13 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 5015AAD1
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Feb 2014 17:45:13 +0000 (UTC)
Received: from newred.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id 1E9391C16
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Feb 2014 17:45:13 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by newred.freebsd.org (8.14.7/8.14.7) with ESMTP id s1HHjCae010827
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Feb 2014 17:45:12 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.7/8.14.7/Submit) id s1HHjCsG010826;
	Mon, 17 Feb 2014 17:45:12 GMT
	(envelope-from nobody)
Message-Id: <201402171745.s1HHjCsG010826@cgiserv.freebsd.org>
Date: Mon, 17 Feb 2014 17:45:12 GMT
From: David Hines <freebsd@dph.fluff.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: CLANG/LLVM code generation bug with optimisation on i386
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         186848
>Category:       i386
>Synopsis:       CLANG/LLVM code generation bug with optimisation on i386
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-i386
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Feb 17 17:50:00 UTC 2014
>Closed-Date:    Sun Mar 02 23:27:50 UTC 2014
>Last-Modified:  Sun Mar 02 23:27:50 UTC 2014
>Originator:     David Hines
>Release:        FreeBSD 10.0-RELEASE #0 r260789
>Organization:
>Environment:
FreeBSD free100 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789: Fri Jan 17 01:46:25 UTC 2014     root@snap.freebsd.org:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
"cc -O -o clang_bug-O clang_bug.c" generates incorrect code on i386, with the attached sample code. Without the "-O", or on an amd64 install the problem does not occur.

>How-To-Repeat:
Compile and run the attached programme.

When this is compiled without optimisation the output is correct.
  cc -o clang_bug clang_bug.c; ./clang_bug
  An int has 32 bits

With optimisation it is incorrect.
  cc -O -o clang_bug-O clang_bug.c; ./clang_bug-O
  An int has 33 bits

>Fix:
No fix known. Work-arounds include not using optimiser, or using gcc.

Patch attached with submission follows:

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	clang_bug.c
#
echo x - clang_bug.c
sed 's/^X//' >clang_bug.c << '7cdc9fc910e8be01c1758f1d1bf4e799'
X#include <stdio.h>
X
X/*
X * This demonstrates a bug in the code generation of the C compiler
X * distributed as part of FreeBSD 10.0 on i386 when using optimisation.
X *
X * To identify the affected system:
X * Output from uname -ap:
X *	FreeBSD free100 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789:
X *	Fri Jan 17 01:46:25 UTC 2014
X *	root@snap.freebsd.org:/usr/obj/usr/src/sys/GENERIC i386 i386
X * Output from cc -v:
X *	FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
X *	Target: i386-unknown-freebsd10.0
X *	Thread model: posix
X *
X * When this is compiled without optimisation the output is correct.
X *	cc -o clang_bug clang_bug.c; ./clang_bug
X *	An int has 32 bits
X *
X * With optimisation it is incorrect.
X *	cc -O -o clang_bug-O clang_bug.c; ./clang_bug-O
X *	An int has 33 bits
X */
X
Xunion
X{
X    int i;
X} u;
X
X
Xint
Xmain(int argc, char *argv[])
X{
X    int j = 1;
X
X    for (u.i = 1;  u.i += u.i;  ++j)
X	;
X    printf("An int has %d bits\n", j);
X
X    return 0;
X}
7cdc9fc910e8be01c1758f1d1bf4e799
exit



>Release-Note:
>Audit-Trail:

From: Bruce Evans <brde@optusnet.com.au>
To: David Hines <freebsd@dph.fluff.org>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-i386@freebsd.org
Subject: Re: i386/186848: CLANG/LLVM code generation bug with optimisation
 on i386
Date: Thu, 20 Feb 2014 14:06:55 +1100 (EST)

 On Mon, 17 Feb 2014, David Hines wrote:
 
 >> Description:
 > "cc -O -o clang_bug-O clang_bug.c" generates incorrect code on i386, with the attached sample code. Without the "-O", or on an amd64 install the problem does not occur.
 
 This is a bug in clang_bug.c.  It's behaviour is undefined.
 
 > Xunion
 > X{
 > X    int i;
 > X} u;
 > X
 > X
 > Xint
 > Xmain(int argc, char *argv[])
 > X{
 > X    int j = 1;
 > X
 > X    for (u.i = 1;  u.i += u.i;  ++j)
 > X	;
 > X    printf("An int has %d bits\n", j);
 > X
 > X    return 0;
 > X}
 
 Undefined behaviour occurs when the addition overflows.  clang somehow
 notices this (u.i starts as 1, and repeated doublings of it cannot
 make it 0 unless overflow occurs).  The addition does in fact overflow.
 The result can be anything.
 
 Some people (not me) don't like clang not giving the "normal" behaviour
 on overflow.
 
 Bruce
State-Changed-From-To: open->closed 
State-Changed-By: dim 
State-Changed-When: Sun Mar 2 23:25:32 UTC 2014 
State-Changed-Why:  
The program relies on integer overflow, which is undefined behavior. 
Either fix the program (recommended), or compile with -fwrapv. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=186848 
>Unformatted:
