[Bug c++/81977] New: Possible issue with inline memcpy with packed structures

2017-08-24 Thread vvarada at codeaurora dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81977

Bug ID: 81977
   Summary: Possible issue with inline memcpy with packed
structures
   Product: gcc
   Version: 6.3.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: vvarada at codeaurora dot org
  Target Milestone: ---

g++ -DOUTER  bug.cc -Wall -Wextra -fno-strict-aliasing -fwrapv

The following source yields different results when building with and without
optimizations (-O2). Compiling source did not result in any warnings.

Without: optimizations it prints 0xabcd which is expected
With -O2 optimization it prints 0x1617 

The issue appears to be due to incorrect offsets being computed for the
relevant fields resulting from the inlining of memcpy. 

If you look at the disassembly with -O2 the %edi argument on X64 is set to a
constant 0x1617 (5655 decimal) indicating the bug is at the compiler optimizer
itself.

By changing the size of *uint16_t header_info[2];* one gets different results
with -O2 as it picks different portions of _prefz.z for _pref.x.   

Thanks,
-Vijay

---


#include 
#include 
#include 

using namespace std;

void printval(int x) __attribute__((noinline));
void printval(int x)
{
   cout << hex << x << endl;
}

#define PACKED __attribute__((packed, aligned(1)))

typedef struct
{
   uint16_t  x ;
   uint16_t  y ;
   uint64_t  z ;
} PACKED TestMsgType;


struct Payload
{
  uint16_t header_info[2];
  TestMsgType _pref;
  void Pack(uint8_t *buffer)
  {
 memcpy(buffer, &_pref, sizeof(_pref));
  }
  void UnPack(uint8_t *buffer)
  {
 memcpy(&_pref, buffer, sizeof(_pref));
  }
};


struct Msg
{
   Payload _payload;
   void Pack(uint8_t *buffer)
   {
  _payload.Pack(buffer);
   }

   void UnPack(uint8_t *buffer)
   {
  _payload.UnPack(buffer);
   }
};

int main()
{
   uint8_t * buffer = new uint8_t [30];
   Msg msg;
   Msg msg1;
   msg._payload._pref.x = 0xabcd;
   msg._payload._pref.y = 0xa;
   msg._payload._pref.z = 0x0001020304051617;
   msg.Pack(&buffer[0]);
   msg1.UnPack(&buffer[0]);
   printval(msg1._payload._pref.x);
   delete [] buffer;
}

[Bug c++/81977] Possible issue with inline memcpy with packed structures

2017-08-24 Thread vvarada at codeaurora dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81977

--- Comment #1 from vvarada at codeaurora dot org ---
FYI: The issue shows up on previous versions of GCC compiler as well, as early
as 4.9.2 (have not tried earlier versions). 

Also if the "buffer" in main is declared as a static uint8_t array, the issue
does not show up. For some reason it shows up only when buffer is a pointer as
shown in the source snippet.

[Bug tree-optimization/81977] [5/6 Regression] Issue with inline memcpy with optimizations enabled

2017-08-30 Thread vvarada at codeaurora dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81977

--- Comment #6 from vvarada at codeaurora dot org ---
Thank you, Richard.