16176 – [3.4 Regression] Miscompilation of unaligned data in MIPS backend (SB1 flavor) (original) (raw)

| Description Paul Koning 2004-06-24 15:38:24 UTC Configure spec: Configured with: /p3/jim/GCC/gcc-3.4.0/configure --prefix=/usr/local/EQLGCC_v340/mips64el --enable-languages=c,c++ --target=mips64el-elf --with-newlib --with-arch=sb1 --with-tune=sb1 --with-gnu-as --with-gnu-ld Thread model: single The following trivial test program is miscompiled in a way that causes a crash: struct foo { short x; struct foo *p __attribute__((packed)); short z; }; struct foo *list; int walklist(struct foo *p) { int t = 0; while (p) { t += p->x; p = p->p; } return t; } The generated code looks like this: .file 1 "test.c" .section .mdebug.abiO64 .previous .text .align 2 .globl walklist .ent walklist walklist: .frame sp,0,sp,0,sp,0,31 # vars= 0, regs= 0/0, args= 0, gp= 0 .mask 0x00000000,0 .fmask 0x00000000,0 .set noreorder .set nomacro beq 4,4,4,0,$L6 move 3,3,3,0 L4:lhL4: lh L4:lh2,0($4) lwl 4,5(4,5(4,5(4) lwr 4,2(4,2(4,2(4) bne 4,4,4,0,$L4 addu 3,3,3,3,$2 L6:jL6: j L6:j31 move 2,2,2,3 .set macro .set reorder .end walklist .comm list,4,4 The error is that an unaligned load is a two-instruction sequence, so you CANNOT use the same register as source and destination. The code as shown will crash when it comes to the null pointer at the end of the list. The above is with -O1; with -O2 the code is reordered slightly but the code is wrong in the same way. Comment 1 Paul Koning 2004-06-24 15:42:31 UTC The processor selection doesn't actually matter; I tried the compile with march/mtune set to sr71000, r5000, rm9000, and r4000; all generate the same broken code. Comment 2 Paul Koning 2004-06-24 17:24:05 UTC GCC 3.4.1 in the 20040618 snapshot has the same bug. Comment 3 Richard Sandiford 2004-06-24 18:19:10 UTC Mea culpa. Working on a fix. Comment 4 Richard Sandiford 2004-06-24 22:19:09 UTC Created attachment 6624 [details] Proposed patch Please try the attached patch. I'll test it tomorrow (once the regression tests for 16144 are complete). Comment 6 Paul Koning 2004-06-25 18:36:53 UTC Sorry for the delayed response. This patch appears to fix the problem. Comment 7 Richard Sandiford 2004-06-25 18:42:49 UTC Patch applied: http://gcc.gnu.org/ml/gcc-patches/2004-06/msg02085.html Mark, you know what I'm going to ask ;) Is this OK for 3.4.1? Or should it wait until 3.4.2? Perhaps the patch isn't quite as obvious as the one you approved earlier today. But IMO it _is_ still obvious. And safe. The bug here is also much more serious, since it only shows up at run time (rather than link time). I suppose every patch carries some amount of risk. But like I say, I think the risk in this case is very small, and I think it's obvious that the new code is theoretically more correct. From that POV, I'd imagine the risk is more of introducing an ICE than introducing another wrong code bug. Err... couldn't decide whether to delete that last paragraph or not ;) Comment 8 Mark Mitchell 2004-06-25 18:53:25 UTC Subject: Re: [3.4 Regression] Miscompilation of unaligned data in MIPS backend (SB1 flavor) rsandifo at gcc dot gnu dot org wrote: > ------- Additional Comments From rsandifo at gcc dot gnu dot org 2004-06-25 18:42 ------- > Patch applied: > > http://gcc.gnu.org/ml/gcc-patches/2004-06/msg02085.html > > Mark, you know what I'm going to ask ;) Is this OK for 3.4.1? Or should > it wait until 3.4.2? OK for 3.4.1. Comment 10 Richard Sandiford 2004-06-28 14:00:33 UTC Patch applied to 3.4. | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |