Bug 21479 - optimizer removes incorrectly variable comparision in if clause
Summary: optimizer removes incorrectly variable comparision in if clause
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.4.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 21305
Blocks:
  Show dependency treegraph
 
Reported: 2005-05-09 22:20 UTC by Vesa Jääskeläinen
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host: i686-winxp-sp2
Target: avr-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vesa Jääskeläinen 2005-05-09 22:20:03 UTC
Hi,

avr-gcc seems to occasinally remove compare for ppv in following code. This 
changes semantics in the if clause and causes incorrect code execution as ppv is 
not compared first before *ppv compare.

void test(int req, void *conf)
{
    void **ppv = (void **) conf;
    unsigned long *lvp = (unsigned long *) conf;
    unsigned long lv = *lvp;
    unsigned char bv = (unsigned char) lv;

    switch (req) {
    case 1:
        if (bv)
        {
            asm ( "nop ; bv needs to be defined and used");
        }
        break;

    case 2:
        asm ( "nop ; entry to case 2");

        /* bug: first compare for ppv is missing, second is only left */
        if (ppv && (*ppv != 0)) {
            asm ( "nop ; ppv and *ppv are not zeros");
        } else {
            asm ( "nop ; either ppv or *ppv is zero");
        }

        break;
    }
}

Known workaround is to use -fno-delete-null-pointer-checks.

After a small debate on this issue on avr-gcc mailing list I was adviced to post 
a bug report.

I used following command line:
avr-gcc -c -mmcu=atmega128 -Os -Wall -Wstrict-prototypes -Wa,-ahlms=test.lst 
test.c -o test.o 

I am using avr-gcc supplied in WinAVR-20050214 (gcc 3.4.3).

> avr-gcc -v
Reading specs from C:/WinAVR/bin/../lib/gcc/avr/3.4.3/specs
Configured with: ../gcc-3.4.3/configure --prefix=m:/WinAVR --build=mingw32 --
host=mingw32 --target=avr --enable-languages=c,c++ --with-dwarf2
Thread model: single
gcc version 3.4.3
Comment 1 Andrew Pinski 2005-05-09 22:28:15 UTC
I don't think this is a bug since conf and ppv cannot be null as you deferenced them already and would 
trap on most machines.  (there is another bug about this recently filed too).
Comment 2 Andrew Pinski 2005-05-09 22:31:13 UTC
Do you have a pointer to the mail on that list?
Comment 3 Andrew Pinski 2005-05-09 22:32:34 UTC
Oh, one more thing, deferencing a null pointer is undefined by the C standard.
Comment 4 Paul Schlie 2005-05-09 23:19:49 UTC
(In reply to comment #1)
> I don't think this is a bug since conf and ppv cannot be null as you deferenced them already
> and would  trap on most machines.  (there is another bug about this recently filed too).
> Oh, one more thing, deferencing a null pointer is undefined by the C standard.

??? Although dereferencing a null pointer may be "undefined" as some machines MAY trap,
 it certainly doesn't give a conformant C compiler license to ignore a comparison of a
pointer against null, which is well defined.

Comment 5 marcus 2005-05-10 06:31:34 UTC
see comment #1 ...  
  
you already derefenced the pointer in ppv (in the line  
 unsigned long lv = *lvp; 
) 
 
so the compiler assumes that anohter NULL ptr check is not needed. 
Comment 6 Vesa Jääskeläinen 2005-05-10 08:00:31 UTC
Andrew,

Here is a pointer to the mailing list:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21479

Topic started on May 08, 2005 with subject "WinAVR 20050214 (gcc 3.4.3) and 
optimizer bug."
Comment 7 Vesa Jääskeläinen 2005-05-10 08:06:34 UTC
In AVR's reading memory address 0 is valid thing though. In a way I can 
understand why to optimization in x86 but shouldn't this be disabled by default 
on AVR's?
Comment 8 Paul Schlie 2005-05-10 08:31:59 UTC
(In reply to comment #5)
> see comment #1 ...  
>   
> you already derefenced the pointer in ppv (in the line  
>  unsigned long lv = *lvp; 
> ) 
>  
> so the compiler assumes that anohter NULL ptr check is not needed. 

- yes, however as the loigical extention of:
   "a null reference is undefined" => "may trap" => "will trap"
   is simply wrong, and is not justifyable; such an optimization
   is target specific, as it depends on "will trap" target semantics.

   (not to mention that even if it is trapped for a particular target,
    that the target won't simply return some value, so pointer null
    comparsions can't be reliably optimized away unless the compiler
    can also enforce dereferenced null pointer trap semantics for that
    particualr target, which GCC does not appear to do.)

Comment 9 Vesa Jääskeläinen 2005-05-10 08:41:18 UTC
Sorry about wrong mailing list pointer :)

http://lists.gnu.org/archive/html/avr-gcc-list/2005-05/index.html

Here is correct URL :)
Comment 10 Falk Hueffner 2005-05-17 12:30:42 UTC
(In reply to comment #8)
> - yes, however as the loigical extention of:
>    "a null reference is undefined" => "may trap" => "will trap"
>    is simply wrong, and is not justifyable; such an optimization
>    is target specific, as it depends on "will trap" target semantics.

Right. However, the logic here is simply "a null pointer dereference is
undefined" => "if you still do it, your code may behave however gcc feels
like", which is backed by the C standard. So this is invalid.
Comment 11 Paul Schlie 2005-05-17 21:24:59 UTC
(In reply to comment #10)
> (In reply to comment #8)
> > - yes, however as the loigical extention of:
> >    "a null reference is undefined" => "may trap" => "will trap"
> >    is simply wrong, and is not justifyable; such an optimization
> >    is target specific, as it depends on "will trap" target semantics.
> 
> Right. However, the logic here is simply "a null pointer dereference is
> undefined" => "if you still do it, your code may behave however gcc feels
> like", which is backed by the C standard. So this is invalid.

No, only the "null pointer dereference" itself is undefined. which means
that upon a null pointer reference any or no value may be returned.

Is says, implies, and grants no rights what so ever to an implementation,
to define that an arbitrary behavior will occure which may be subsequenlty
relied upon to occured unless the implementation inforces that behavior.

More specifically, unless GCC can warrent that a "null pointer dereference"
will trap will terminate program execution, it must preserve the semantics
of the remaining programs execution as defined by the standard, which
includes but not limited to preserving null-pointer comparision semantics,
as defined by the standard; as not to do so would be in violation of the same.