Dangerously mis-compiled program (w/ small testcase)

Max Lawson mlawson@drfmc.ceng.cea.fr
Tue Jan 13 04:58:00 GMT 1998


// Hi, 
// There's a (dangerous) problem pointed out by the following
// program. I've added all needed infos to track the problem 
// down (Hope so)
//
// Best regards, Max


// banane:$ cat z.cc

#include <iostream.h>
#include <cstddef>


void f(double j, double& v)
{
  size_t sz = size_t(2*j+1);
  double norm_ = j*(j+1);
  double m = j;
#ifdef _OK_
  double sign_ = -1.;
#else
  int sign_ = -1;
#endif
  for (size_t c=1;c<=sz;++c)
    for (size_t r=1;r<=sz;++r)
      if (r+sign_*1 == c)
	{
	  double val = (norm_-m*(m+sign_));
	  for (size_t k=1;k<2;++k) 
	    val *= (norm_ - (m+sign_*k)*(m+sign_*(k+1)));
	  v = val;
	}
}

int main()
{
  double v;
  f(1,v);
  cout << v << "\n";
  return 0;
}


// banane:$ g++ -O6 -fno-exceptions -D_OK_ z.cc; a.out
// 4

// the result above is the correct one. Now if I disable
// the _OK_ flag: 

// banane:$ g++ -O6 -fno-exceptions; a.out
// -3.68935e+19

// ?????????????

// Here are the species
// gcc version egcs-2.91.03 971225 (gcc-2.8.0) on i586-pc-linux-gnulibc1

// here are the assembler input file in for wrong case followed by the 
// diff file w/ the correct case assembler input file

/*
	.file	"z.cc"
	.version	"01.01"
/ GNU C++ version egcs-2.91.03 971225 (gcc-2.8.0) (i586-pc-linux-gnulibc1) compiled by GNU C version egcs-2.91.03 971225 (gcc-2.8.0).
/ options passed:  -O6 -fno-exceptions
/ options enabled:  -fdefer-pop -fcse-follow-jumps -fcse-skip-blocks
/ -fexpensive-optimizations -fthread-jumps -fstrength-reduce -fpeephole
/ -fforce-mem -ffunction-cse -finline-functions -finline
/ -fkeep-static-consts -fcaller-saves -fpcc-struct-return
/ -frerun-cse-after-loop -frerun-loop-opt -fschedule-insns2 -fcommon
/ -fverbose-asm -fgnu-linker -fregmove -fargument-alias -m80387
/ -mhard-float -mno-soft-float -mieee-fp -mfp-ret-in-387
/ -mschedule-prologue -mcpu=pentium -march=pentium

gcc2_compiled.:
.section	.rodata
.LC0:
	.string	"\n"
	.align 4
.LC1:
	.long 0x0,0x40080000
	.align 4
.LC2:
	.long 0x0,0x40000000
.text
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	subl $28,%esp
	pushl %edi
	fldl .LC1
	pushl %esi
	fldl .LC2
	pushl %ebx
	fld1
	fxch %st(2)
	leal -20(%ebp),%ebx
	movl $-1,%edi
	movl %ebx,-24(%ebp)
	movl $1,%esi
	fnstcw -4(%ebp)
	movl -4(%ebp),%ebx
	movb $12,%bh
	movl %ebx,-12(%ebp)
	fldcw -12(%ebp)
	fistpll -12(%ebp)
	movl -12(%ebp),%eax
	movl -8(%ebp),%edx
	fldcw -4(%ebp)
	.align 4
.L432:
	cmpl $3,%esi
	ja .L447
	movl $1,-28(%ebp)
	.align 4
.L435:
	cmpl $3,-28(%ebp)
	ja .L445
	movl -28(%ebp),%eax
	addl %edi,%eax
	cmpl %esi,%eax
	jne .L444
	fld %st(1)
	pushl %edi
	fiaddl (%esp)
	addl $4,%esp
	fmul %st(2),%st
	movl $1,%ecx
	fld %st(1)
	fsubp %st,%st(1)
	.align 4
.L441:
	movl %edi,%eax
	imull %ecx,%eax
	xorl %edx,%edx
	incl %ecx
	pushl %edx
	pushl %eax
	fildll (%esp)
	addl $8,%esp
	movl %edi,%eax
	imull %ecx,%eax
	xorl %edx,%edx
	pushl %edx
	pushl %eax
	fildll (%esp)
	addl $8,%esp
	fxch %st(1)
	fadd %st(4),%st
	fxch %st(1)
	fadd %st(4),%st
	fmulp %st,%st(1)
	fsubr %st(2),%st
	fmulp %st,%st(1)
	cmpl $1,%ecx
	jbe .L441
	movl -24(%ebp),%ebx
	fstpl (%ebx)
.L444:
	incl -28(%ebp)
	jmp .L435
	.align 4
.L445:
	incl %esi
	jmp .L432
	.align 4
.L447:
	fstp %st(0)
	fstp %st(0)
	pushl $.LC0
	pushl -16(%ebp)
	pushl -20(%ebp)
	pushl $cout
	call __ls__7ostreamd
	addl $12,%esp
	pushl %eax
	call __ls__7ostreamPCc
	leal -40(%ebp),%esp
	popl %ebx
	popl %esi
	popl %edi
	movl %ebp,%esp
	xorl %eax,%eax
	popl %ebp
	ret
.Lfe1:
	.size	 main,.Lfe1-main
	.align 4
.globl f__FdRd
	.type	 f__FdRd,@function
f__FdRd:
	pushl %ebp
	movl %esp,%ebp
	subl $20,%esp
	pushl %edi
	pushl %esi
	pushl %ebx
	fldl 8(%ebp)
	fld %st(0)
	fadd %st(1),%st
	fld1
	fadd %st,%st(1)
	fadd %st(2),%st
	movl $-1,%edi
	movl $1,%esi
	fld %st(2)
	fxch %st(2)
	fnstcw -4(%ebp)
	movl -4(%ebp),%ebx
	movb $12,%bh
	movl %ebx,-12(%ebp)
	fldcw -12(%ebp)
	fistpll -12(%ebp)
	movl -12(%ebp),%eax
	movl -8(%ebp),%edx
	fldcw -4(%ebp)
	fmulp %st,%st(1)
	movl %eax,-16(%ebp)
	.align 4
.L417:
	cmpl %esi,-16(%ebp)
	jb .L448
	movl $1,-20(%ebp)
	.align 4
.L421:
	movl -16(%ebp),%ebx
	cmpl %ebx,-20(%ebp)
	ja .L419
	movl -20(%ebp),%eax
	addl %edi,%eax
	cmpl %esi,%eax
	jne .L423
	fld %st(1)
	pushl %edi
	fiaddl (%esp)
	addl $4,%esp
	fmul %st(2),%st
	movl $1,%ecx
	fld %st(1)
	fsubp %st,%st(1)
	.align 4
.L429:
	movl %edi,%eax
	imull %ecx,%eax
	xorl %edx,%edx
	incl %ecx
	pushl %edx
	pushl %eax
	fildll (%esp)
	addl $8,%esp
	movl %edi,%eax
	imull %ecx,%eax
	xorl %edx,%edx
	pushl %edx
	pushl %eax
	fildll (%esp)
	addl $8,%esp
	fxch %st(1)
	fadd %st(4),%st
	fxch %st(1)
	fadd %st(4),%st
	fmulp %st,%st(1)
	fsubr %st(2),%st
	fmulp %st,%st(1)
	cmpl $1,%ecx
	jbe .L429
	movl 16(%ebp),%ebx
	fstpl (%ebx)
.L423:
	incl -20(%ebp)
	jmp .L421
	.align 4
.L419:
	incl %esi
	jmp .L417
	.align 4
.L448:
	fstp %st(0)
	fstp %st(0)
	leal -32(%ebp),%esp
	popl %ebx
	popl %esi
	popl %edi
	movl %ebp,%esp
	popl %ebp
	ret
.Lfe2:
	.size	 f__FdRd,.Lfe2-f__FdRd
	.ident	"GCC: (GNU) egcs-2.91.03 971225 (gcc-2.8.0)"
*/

// banane:$ diff -c ok.s  not-ok.s

/*
*** ok.s	Tue Jan 13 13:36:41 1998
--- not-ok.s	Tue Jan 13 13:33:07 1998
***************
*** 13,28 ****
  
  gcc2_compiled.:
  .section	.rodata
! .LC2:
  	.string	"\n"
  	.align 4
  .LC1:
- 	.long 0x0,0xbff00000
- 	.align 4
- .LC3:
  	.long 0x0,0x40080000
  	.align 4
! .LC4:
  	.long 0x0,0x40000000
  .text
  	.align 4
--- 13,25 ----
  
  gcc2_compiled.:
  .section	.rodata
! .LC0:
  	.string	"\n"
  	.align 4
  .LC1:
  	.long 0x0,0x40080000
  	.align 4
! .LC2:
  	.long 0x0,0x40000000
  .text
  	.align 4
***************
*** 30,112 ****
  	.type	 main,@function
  main:
  	pushl %ebp
- 	fldl .LC3
  	movl %esp,%ebp
! 	fldl .LC4
! 	subl $36,%esp
! 	fld1
  	pushl %edi
  	fldl .LC1
- 	fxch %st(3)
  	pushl %esi
! 	leal -20(%ebp),%edi
  	pushl %ebx
  	movl $1,%esi
  	fnstcw -4(%ebp)
! 	movl -4(%ebp),%eax
! 	movb $12,%ah
! 	movl %eax,-12(%ebp)
  	fldcw -12(%ebp)
! 	fistpll -36(%ebp)
  	fldcw -4(%ebp)
  	.align 4
  .L432:
  	cmpl $3,%esi
  	ja .L447
! 	movl $1,%ebx
! 	movl %esi,-28(%ebp)
! 	movl $0,-24(%ebp)
  	.align 4
  .L435:
! 	cmpl $3,%ebx
  	ja .L445
! 	movl %ebx,-36(%ebp)
! 	movl $0,-32(%ebp)
! 	fildll -36(%ebp)
! 	fadd %st(3),%st
! 	fildll -28(%ebp)
! 	fucompp
! 	fnstsw %ax
! 	andb $68,%ah
! 	xorb $64,%ah
  	jne .L444
! 	fld %st(0)
! 	fadd %st(3),%st
! 	fmul %st(1),%st
  	movl $1,%ecx
! 	fld %st(2)
  	fsubp %st,%st(1)
  	.align 4
  .L441:
! 	movl %ecx,%eax
  	xorl %edx,%edx
  	incl %ecx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
! 	movl %ecx,%eax
  	xorl %edx,%edx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
  	fxch %st(1)
! 	fmul %st(5),%st
  	fxch %st(1)
! 	fmul %st(5),%st
! 	fxch %st(1)
! 	fadd %st(3),%st
! 	fxch %st(1)
! 	fadd %st(3),%st
  	fmulp %st,%st(1)
! 	fsubr %st(3),%st
  	fmulp %st,%st(1)
  	cmpl $1,%ecx
  	jbe .L441
! 	fstpl (%edi)
  .L444:
! 	incl %ebx
  	jmp .L435
  	.align 4
  .L445:
--- 27,105 ----
  	.type	 main,@function
  main:
  	pushl %ebp
  	movl %esp,%ebp
! 	subl $28,%esp
  	pushl %edi
  	fldl .LC1
  	pushl %esi
! 	fldl .LC2
  	pushl %ebx
+ 	fld1
+ 	fxch %st(2)
+ 	leal -20(%ebp),%ebx
+ 	movl $-1,%edi
+ 	movl %ebx,-24(%ebp)
  	movl $1,%esi
  	fnstcw -4(%ebp)
! 	movl -4(%ebp),%ebx
! 	movb $12,%bh
! 	movl %ebx,-12(%ebp)
  	fldcw -12(%ebp)
! 	fistpll -12(%ebp)
! 	movl -12(%ebp),%eax
! 	movl -8(%ebp),%edx
  	fldcw -4(%ebp)
  	.align 4
  .L432:
  	cmpl $3,%esi
  	ja .L447
! 	movl $1,-28(%ebp)
  	.align 4
  .L435:
! 	cmpl $3,-28(%ebp)
  	ja .L445
! 	movl -28(%ebp),%eax
! 	addl %edi,%eax
! 	cmpl %esi,%eax
  	jne .L444
! 	fld %st(1)
! 	pushl %edi
! 	fiaddl (%esp)
! 	addl $4,%esp
! 	fmul %st(2),%st
  	movl $1,%ecx
! 	fld %st(1)
  	fsubp %st,%st(1)
  	.align 4
  .L441:
! 	movl %edi,%eax
! 	imull %ecx,%eax
  	xorl %edx,%edx
  	incl %ecx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
! 	movl %edi,%eax
! 	imull %ecx,%eax
  	xorl %edx,%edx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
  	fxch %st(1)
! 	fadd %st(4),%st
  	fxch %st(1)
! 	fadd %st(4),%st
  	fmulp %st,%st(1)
! 	fsubr %st(2),%st
  	fmulp %st,%st(1)
  	cmpl $1,%ecx
  	jbe .L441
! 	movl -24(%ebp),%ebx
! 	fstpl (%ebx)
  .L444:
! 	incl -28(%ebp)
  	jmp .L435
  	.align 4
  .L445:
***************
*** 116,123 ****
  .L447:
  	fstp %st(0)
  	fstp %st(0)
! 	fstp %st(0)
! 	pushl $.LC2
  	pushl -16(%ebp)
  	pushl -20(%ebp)
  	pushl $cout
--- 109,115 ----
  .L447:
  	fstp %st(0)
  	fstp %st(0)
! 	pushl $.LC0
  	pushl -16(%ebp)
  	pushl -20(%ebp)
  	pushl $cout
***************
*** 125,131 ****
  	addl $12,%esp
  	pushl %eax
  	call __ls__7ostreamPCc
! 	leal -48(%ebp),%esp
  	popl %ebx
  	popl %esi
  	popl %edi
--- 117,123 ----
  	addl $12,%esp
  	pushl %eax
  	call __ls__7ostreamPCc
! 	leal -40(%ebp),%esp
  	popl %ebx
  	popl %esi
  	popl %edi
***************
*** 135,152 ****
  	ret
  .Lfe1:
  	.size	 main,.Lfe1-main
- .section	.rodata
- 	.align 4
- .LC5:
- 	.long 0x0,0xbff00000
- .text
  	.align 4
  .globl f__FdRd
  	.type	 f__FdRd,@function
  f__FdRd:
  	pushl %ebp
  	movl %esp,%ebp
! 	subl $28,%esp
  	pushl %edi
  	pushl %esi
  	pushl %ebx
--- 127,139 ----
  	ret
  .Lfe1:
  	.size	 main,.Lfe1-main
  	.align 4
  .globl f__FdRd
  	.type	 f__FdRd,@function
  f__FdRd:
  	pushl %ebp
  	movl %esp,%ebp
! 	subl $20,%esp
  	pushl %edi
  	pushl %esi
  	pushl %ebx
***************
*** 156,234 ****
  	fld1
  	fadd %st,%st(1)
  	fadd %st(2),%st
! 	fldl .LC5
  	movl $1,%esi
! 	fld %st(3)
! 	fxch %st(3)
  	fnstcw -4(%ebp)
! 	movl -4(%ebp),%eax
! 	movb $12,%ah
! 	movl %eax,-12(%ebp)
  	fldcw -12(%ebp)
! 	fistpll -28(%ebp)
  	fldcw -4(%ebp)
- 	fxch %st(2)
  	fmulp %st,%st(1)
! 	movl -28(%ebp),%edi
  	.align 4
  .L417:
! 	cmpl %edi,%esi
! 	ja .L448
! 	movl $1,%ebx
! 	movl %esi,-20(%ebp)
! 	movl $0,-16(%ebp)
  	.align 4
  .L421:
! 	cmpl %edi,%ebx
  	ja .L419
! 	movl %ebx,-28(%ebp)
! 	movl $0,-24(%ebp)
! 	fildll -28(%ebp)
! 	fadd %st(2),%st
! 	fildll -20(%ebp)
! 	fucompp
! 	fnstsw %ax
! 	andb $68,%ah
! 	xorb $64,%ah
  	jne .L423
! 	fld %st(2)
! 	fadd %st(2),%st
! 	fmul %st(3),%st
  	movl $1,%ecx
  	fld %st(1)
  	fsubp %st,%st(1)
  	.align 4
  .L429:
! 	movl %ecx,%eax
  	xorl %edx,%edx
  	incl %ecx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
! 	movl %ecx,%eax
  	xorl %edx,%edx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
  	fxch %st(1)
! 	fmul %st(4),%st
  	fxch %st(1)
! 	fmul %st(4),%st
! 	fxch %st(1)
! 	fadd %st(5),%st
! 	fxch %st(1)
! 	fadd %st(5),%st
  	fmulp %st,%st(1)
  	fsubr %st(2),%st
  	fmulp %st,%st(1)
  	cmpl $1,%ecx
  	jbe .L429
! 	movl 16(%ebp),%eax
! 	fstpl (%eax)
  .L423:
! 	incl %ebx
  	jmp .L421
  	.align 4
  .L419:
--- 143,215 ----
  	fld1
  	fadd %st,%st(1)
  	fadd %st(2),%st
! 	movl $-1,%edi
  	movl $1,%esi
! 	fld %st(2)
! 	fxch %st(2)
  	fnstcw -4(%ebp)
! 	movl -4(%ebp),%ebx
! 	movb $12,%bh
! 	movl %ebx,-12(%ebp)
  	fldcw -12(%ebp)
! 	fistpll -12(%ebp)
! 	movl -12(%ebp),%eax
! 	movl -8(%ebp),%edx
  	fldcw -4(%ebp)
  	fmulp %st,%st(1)
! 	movl %eax,-16(%ebp)
  	.align 4
  .L417:
! 	cmpl %esi,-16(%ebp)
! 	jb .L448
! 	movl $1,-20(%ebp)
  	.align 4
  .L421:
! 	movl -16(%ebp),%ebx
! 	cmpl %ebx,-20(%ebp)
  	ja .L419
! 	movl -20(%ebp),%eax
! 	addl %edi,%eax
! 	cmpl %esi,%eax
  	jne .L423
! 	fld %st(1)
! 	pushl %edi
! 	fiaddl (%esp)
! 	addl $4,%esp
! 	fmul %st(2),%st
  	movl $1,%ecx
  	fld %st(1)
  	fsubp %st,%st(1)
  	.align 4
  .L429:
! 	movl %edi,%eax
! 	imull %ecx,%eax
  	xorl %edx,%edx
  	incl %ecx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
! 	movl %edi,%eax
! 	imull %ecx,%eax
  	xorl %edx,%edx
  	pushl %edx
  	pushl %eax
  	fildll (%esp)
  	addl $8,%esp
  	fxch %st(1)
! 	fadd %st(4),%st
  	fxch %st(1)
! 	fadd %st(4),%st
  	fmulp %st,%st(1)
  	fsubr %st(2),%st
  	fmulp %st,%st(1)
  	cmpl $1,%ecx
  	jbe .L429
! 	movl 16(%ebp),%ebx
! 	fstpl (%ebx)
  .L423:
! 	incl -20(%ebp)
  	jmp .L421
  	.align 4
  .L419:
***************
*** 238,245 ****
  .L448:
  	fstp %st(0)
  	fstp %st(0)
! 	fstp %st(0)
! 	leal -40(%ebp),%esp
  	popl %ebx
  	popl %esi
  	popl %edi
--- 219,225 ----
  .L448:
  	fstp %st(0)
  	fstp %st(0)
! 	leal -32(%ebp),%esp
  	popl %ebx
  	popl %esi
  	popl %edi

 */



More information about the Gcc-bugs mailing list