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

            Bug ID: 65147
           Summary: alignment of std::atomic object is not correct
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexey.lapshin at oracle dot com

According to the documentation -
https://gcc.gnu.org/wiki/Atomic/GCCMM/UnalignedPolicy alignment of atomic
object should match it`s size.
Alignment in the test case below does not match with documentation.

~/atomic_test$ cat unaligned_atomic.cpp

#include <atomic>
#include <stdio.h>

typedef struct {
    char c [8];
} power_of_two_obj;

typedef struct {
   char c[1];
   std::atomic<power_of_two_obj> ao;
} container_struct; 

int main ( void ) {

    std::atomic<power_of_two_obj> obj1;
    container_struct              obj2; 


    printf("\n Size and Alignment of std::atomic  object "); 
    printf(" : sizeof(obj1) %d alignof(obj1) %d ", sizeof(obj1), alignof(obj1)
);

    printf("\n Size and Alignment of std::atomic member object "); 
    printf(" : sizeof(obj2.ao) %d alignof(obj2.ao) %d \n", sizeof(obj2.ao),
alignof(obj2.ao) );

    return 0;
}

~/atomic_test$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc/libexec/gcc/i386-pc-solaris2.11/4.9.2/lto-wrapper
Target: i386-pc-solaris2.11
Configured with: ./configure --prefix=/opt/gcc/
Thread model: posix
gcc version 4.9.2 (GCC) 

~/atomic_test$ g++ -O -latomic -std=c++11 unaligned_atomic.cpp -m32

~/atomic_test$ ./a.out

 Size and Alignment of std::atomic  object  : sizeof(obj1) 8 alignof(obj1) 1 
 Size and Alignment of std::atomic member object  : sizeof(obj2.ao) 8
alignof(obj2.ao) 1 

Alignment should be 8-bytes in above test case.

This behavior also differs from gcc. Gcc aligns 8-bytes objects to 8-bytes.

The bug was found on Solarix x86 -m32, but it could be on other platforms -
SPARC, Linux, -m64.

There is another problem with similar test case. If size of object is not power
of two then size of corresponding atomic object is also not power of two.
According to  https://gcc.gnu.org/wiki/Atomic/GCCMM/UnalignedPolicy the size
should be upsized in such case.

Reply via email to