This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the EGCS project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: gcc 2.95 optimizer bug?


   cc: gcc-bugs@gcc.gnu.org
   Date: Sat, 31 Jul 1999 20:39:23 -0500
   From: Mumit Khan <khan@xraylith.wisc.EDU>

   Joe Ramey <ramey@csc.ti.com> writes:
   > I am using gcc 2.95 on a Sun Ultra-2 workstation running Solaris
   > 2.5.1.  I was compiling the teTeX-1.0 package and found that the mf
   > (MetaFont) program was dumping core.  When I compiled with gcc 2.8.1 I
   > did not have this problem.  Also I found that it will dump core if
   > compiled with gcc 2.95 using the -O2 option, but not when I am using
   > the -O1 option.

   Unless I'm missing something very obvious, this should bomb with any
   compiler.

   First of all, you allocate 100 entries for mem, but then access 1024'th
   entry (lomemmax = rover + 1000).

      lomemmax = rover + 1000 ;
      mem [lomemmax ].hhfield .v.RH = 0 ;

   Then, in the following snippet:

      {register integer for_end; k = memtop - 2 ;for_end = memtop ; if ( k <= 
      for_end) do 
	mem [k ]= mem [lomemmax ];
      while ( k++ < for_end ) ;} 

   k becomes -2 (memtop is initialized to 0), which is obviously a problem.

   Did you run this code under a memory debugger?

   Feel free to point out if I'm overlooking something fairly obvious here.

   Regards,
   Mumit

Ok, here's a fixed version of the program that no longer has these
illegal accesses, but still dumps core with a Bus Error.  I took your
suggestion and ran the code under a memory debugger (Purify).  It
found 6 problems with the version of the code that I sent in before,
but this version comes through cleanly.  Again, the program gets a Bus
Error if I compile with -O2, but with -O1 it runs with no error, and
compiling with gcc 2.8.1 -O2 also produces code that runs with no
error.

FYI I also compiled with -O2 -g and then ran the program under gdb.
It dies here:

Program received signal SIGBUS, Bus error.
0x10c98 in inittab () at trybug.c:120
120       hashused = 9757 ;
(gdb) 

That was my first clue that something very strange is going on, since
it's not dying on a line that looks like the typical bus error point
(dereference a null pointer, divide by zero maybe, stuff like that).

So again here's the C code (the fixed version) that seems to
demonstrate the problem.

------------------------------------------------------------------------------

#include <malloc.h>

typedef long  integer;
typedef integer halfword  ;
typedef unsigned char quarterword  ;
typedef integer strnumber  ;

typedef struct
{
  struct
  {
    quarterword B0, B1, B2, B3;

  } u;
} fourquarters;

typedef union
{
  struct
  {
    halfword RH, LH;
  } v;
  struct
  {  
    halfword junk;
    short B0, B1;
  } u;
} twohalves;

typedef union
{
  twohalves hhfield;
  integer cint;
  fourquarters qqqq;
} memoryword;

 integer memtop = 100  ;
 halfword memend  ;
 integer varused  ;
 memoryword * mem  ;
 halfword hashused  ;
 halfword himemmin  ;
 twohalves eqtb[9770]  ;
 halfword rover  ;
 twohalves hash[9770]  ;
 strnumber intname[( 300 )  + 1]  ;
 halfword lomemmax  ;

void inittab();

main()
{
    mem= calloc(10000, sizeof  (memoryword));
    

    inittab();
}


void 
inittab ( ) 
{
  integer k  ;
  rover = 23 ;
  mem [rover ].hhfield .v.RH = 268435455L ;
  mem [rover ].hhfield .v.LH = 1000 ;
  mem [rover + 1 ].hhfield .v.LH = rover ;
  mem [rover + 1 ].hhfield .v.RH = rover ;
  lomemmax = rover + 1000 ;
  mem [lomemmax ].hhfield .v.RH = 0 ;
  mem [lomemmax ].hhfield .v.LH = 0 ;
  {register integer for_end; k = memtop - 2 ;for_end = memtop ; if ( k <= 
  for_end) do 
    mem [k ]= mem [lomemmax ];
  while ( k++ < for_end ) ;} 
  memend = memtop ;
  himemmin = memtop - 2 ;
  varused = 23 ;
  intname [1 ]= 408 ;
  intname [2 ]= 409 ;
  intname [3 ]= 410 ;
  intname [4 ]= 411 ;
  intname [5 ]= 412 ;
  intname [6 ]= 413 ;
  intname [7 ]= 414 ;
  intname [8 ]= 415 ;
  intname [9 ]= 416 ;
  intname [10 ]= 417 ;
  intname [11 ]= 418 ;
  intname [12 ]= 419 ;
  intname [13 ]= 420 ;
  intname [14 ]= 421 ;
  intname [15 ]= 422 ;
  intname [16 ]= 423 ;
  intname [17 ]= 424 ;
  intname [18 ]= 425 ;
  intname [19 ]= 426 ;
  intname [20 ]= 427 ;
  intname [21 ]= 428 ;
  intname [22 ]= 429 ;
  intname [23 ]= 430 ;
  intname [24 ]= 431 ;
  intname [25 ]= 432 ;
  intname [26 ]= 433 ;
  intname [27 ]= 434 ;
  intname [28 ]= 435 ;
  intname [29 ]= 436 ;
  intname [30 ]= 437 ;
  intname [31 ]= 438 ;
  intname [32 ]= 439 ;
  intname [33 ]= 440 ;
  intname [34 ]= 441 ;
  intname [35 ]= 442 ;
  intname [36 ]= 443 ;
  intname [37 ]= 444 ;
  intname [38 ]= 445 ;
  intname [39 ]= 446 ;
  intname [40 ]= 447 ;
  intname [41 ]= 448 ;
  hashused = 9757 ;
  hash [9768 ].v.RH = 450 ;
  hash [9766 ].v.RH = 451 ;
  hash [9767 ].v.RH = 452 ;
  hash [9765 ].v.RH = 453 ;
  hash [9764 ].v.RH = 454 ;
  hash [9763 ].v.RH = 59 ;
  hash [9762 ].v.RH = 58 ;
  hash [9761 ].v.RH = 47 ;
  hash [9760 ].v.RH = 91 ;
  hash [9759 ].v.RH = 41 ;
  hash [9757 ].v.RH = 455 ;
  eqtb [9759 ].v.LH = 62 ;
  mem [19 ].hhfield .v.LH = 9770 ;
  mem [19 ].hhfield .v.RH = 0 ;
  mem [memtop ].hhfield .v.LH = 268435455L ;
  mem [3 ].hhfield .v.LH = 0 ;
  mem [3 ].hhfield .v.RH = 0 ;
  mem [4 ].hhfield .v.LH = 1 ;
  mem [4 ].hhfield .v.RH = 0 ;
  {register integer for_end; k = 5 ;for_end = 11 ; if ( k <= for_end) do 
    mem [k ]= mem [4 ];
  while ( k++ < for_end ) ;} 
} 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]