public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94130] New: Unintended result with optimization option when assigning two structures, memset and 0
@ 2020-03-11  1:47 minzkn at minzkn dot com
  2020-03-11  7:55 ` [Bug tree-optimization/94130] [8/9/10 Regression] " rguenth at gcc dot gnu.org
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: minzkn at minzkn dot com @ 2020-03-11  1:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94130

            Bug ID: 94130
           Summary: Unintended result with optimization option when
                    assigning two structures, memset and 0
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: minzkn at minzkn dot com
  Target Milestone: ---

Created attachment 48014
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48014&action=edit
test source

It seems that there is an optimization bug (-O1) for gcc version 7.4.0 and
above optimization level 1. (Same as gcc 7.5, it seems to occur from 7.x to
latest 9.2.)

The following verification code was written shortly and when compiled with
optimization options such as -O1, -O2, -O3, and -Os instead of -O0, it was
confirmed that the correct result was not produced.

/ *
1. Assign memset to the member of the first structure along with the address of
the second structure.
2. Initialize the second structure to the desired member value. Only reset to 0
3. Call the first structure by passing it to the dump function. This is not
what you want.
4. Put a memory barrier between steps 1 and 2 to avoid the problem.
* /

#include <stdio.h>
#include <memory.h>

struct myreq {
    char m_name[6];
    void *m_data;
};

struct myvalue {
    unsigned int m_type;
    unsigned int m_value;
};

#define mytest_type 1234
#define mytest_value 0 /* only zero case issue */

#define mytest_barrier() __asm__ __volatile__("": : :"memory")

static void myprint(struct myreq *s_myreq)
{
    struct myvalue *s_myvalue = (struct myvalue *)s_myreq->m_data;

    printf(
        "%s: n=%s, cmd=%u, data=%u\n",
        ((s_myvalue->m_type == mytest_type) && (s_myvalue->m_value ==
mytest_value)) ? "NO BUG" : "!! BUG",
        s_myreq->m_name,
        s_myvalue->m_type,
        s_myvalue->m_value
    );
}

static void mytest_with_barrier(void)
{
    struct myreq s_myreq;
    struct myvalue s_myvalue;

    memset(&s_myreq, 0, sizeof(s_myreq));
    strncpy(s_myreq.m_name, "Hello", sizeof(s_myreq.m_name));

    s_myreq.m_data = memset(&s_myvalue, 0, sizeof(s_myvalue));
    /* memory barrier */ mytest_barrier();
    s_myvalue.m_type = mytest_type;
    s_myvalue.m_value = mytest_value;

    myprint(&s_myreq);
}

static void mytest_without_barrier(void)
{
    struct myreq s_myreq;
    struct myvalue s_myvalue;

    memset(&s_myreq, 0, sizeof(s_myreq));
    strncpy(s_myreq.m_name, "Hello", sizeof(s_myreq.m_name));

    s_myreq.m_data = memset(&s_myvalue, 0, sizeof(s_myvalue));
    s_myvalue.m_type = mytest_type;
    s_myvalue.m_value = mytest_value;

    myprint(&s_myreq);
}

int main(void)
{
    mytest_with_barrier();
    mytest_without_barrier();

    return(0);
}


>>> "gcc -O2" OUTPUT
NO BUG: n=Hello, cmd=1234, data=0
!! BUG: n=Hello, cmd=0, data=1819043144

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

end of thread, other threads:[~2021-03-06 18:10 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-11  1:47 [Bug c/94130] New: Unintended result with optimization option when assigning two structures, memset and 0 minzkn at minzkn dot com
2020-03-11  7:55 ` [Bug tree-optimization/94130] [8/9/10 Regression] " rguenth at gcc dot gnu.org
2020-03-11  7:58 ` rguenth at gcc dot gnu.org
2020-03-11 12:05 ` jakub at gcc dot gnu.org
2020-03-11 12:15 ` jakub at gcc dot gnu.org
2020-03-11 12:17 ` jakub at gcc dot gnu.org
2020-03-11 12:21 ` jakub at gcc dot gnu.org
2020-03-11 13:06 ` jakub at gcc dot gnu.org
2020-03-12 10:14 ` [Bug tree-optimization/94130] [8/9 " jakub at gcc dot gnu.org
2020-03-12 12:38 ` marxin at gcc dot gnu.org
2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
2020-03-17 19:14 ` [Bug tree-optimization/94130] [8 " jakub at gcc dot gnu.org
2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
2020-09-17 17:18 ` jakub at gcc dot gnu.org
2021-03-06 18:10 ` 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).