public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56098] New: conditional write through volatile pointer produces unintended read
@ 2013-01-24 18:22 werner at almesberger dot net
  2013-01-25  8:43 ` [Bug middle-end/56098] " mikpe at it dot uu.se
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: werner at almesberger dot net @ 2013-01-24 18:22 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56098

             Bug #: 56098
           Summary: conditional write through volatile pointer produces
                    unintended read
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: werner@almesberger.net


If compiled with -O2 or greater and without -fno-strict-aliasing, the following
code

volatile int *ptr;

void problem(int flag)
{
        *ptr = 1;
        if (flag)
                *ptr = 2;
}

produces an unintended read, as if the "if" statement had continued with

        else
                *ptr = *ptr;

Code generated by gcc 4.7.2 on Ubuntu on x86-64 when invoked with
gcc-4.7 -Wall -Wextra -O2 -S bug.c

...
problem:
.LFB0:
        .cfi_startproc
        movq    ptr(%rip), %rax
        testl   %edi, %edi
        movl    $2, %edx
        movl    $1, (%rax)
        jne     .L2
        movl    (%rax), %edx    <<< unexpected
.L2:
        movl    %edx, (%rax)
        ret
        .cfi_endproc
...

This happens with
- gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 for x86-64,
- gcc-4.7 (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 for x86-64,
- mipsel-openwrt-linux-gcc (Linaro GCC 4.6-2012.02) 4.6.3 20120201 (prerelease)
for MIPS,
and I've had someone confirm it for an ARM target (without further details) as
well.

-Os instead of -O2 has the same effect. The unintended read disappears if
optimizing with -O1 or less, or with -fno-strict-aliasing.

This bears a striking resemblance to Bug 15456 and maybe Bug 5395, except that
these were for C++ while this is plain C.


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2013-04-03 18:20 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-24 18:22 [Bug c/56098] New: conditional write through volatile pointer produces unintended read werner at almesberger dot net
2013-01-25  8:43 ` [Bug middle-end/56098] " mikpe at it dot uu.se
2013-01-25 14:43 ` [Bug middle-end/56098] [4.6/4.7/4.8 Regression] " rguenth at gcc dot gnu.org
2013-01-25 15:35 ` jakub at gcc dot gnu.org
2013-01-25 20:04 ` jakub at gcc dot gnu.org
2013-01-25 20:06 ` [Bug middle-end/56098] [4.6/4.7 " jakub at gcc dot gnu.org
2013-01-25 22:46 ` werner at almesberger dot net
2013-01-26  2:10 ` werner at almesberger dot net
2013-02-01 14:17 ` jakub at gcc dot gnu.org
2013-02-01 14:38 ` [Bug middle-end/56098] [4.6 " jakub at gcc dot gnu.org
2013-02-27 19:47 ` pinskia at gcc dot gnu.org
2013-04-03 18:20 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).