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

            Bug ID: 93916
           Summary: Implicit copy/assignment alters padding bits of
                    storage
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andysem at mail dot ru
  Target Milestone: ---

This bug is related to bug #88101.

Consider this test case:

#include <cstdio>
#include <cstring>
#include <new>

struct struct_with_padding
{
    char x;
    short y;
};

template< typename To, typename From >
inline To bitwise_cast_clear_padding(From const& from) noexcept
{
    union cast_storage_t
    {
        unsigned char bytes[sizeof(To) < sizeof(From) ? sizeof(From) :
sizeof(To)];
        From aligner;

        cast_storage_t() {}
    }
    storage;

    // Clear any possible internal padding bits. Placement new is required to
perform member-wise initialization.
    std::memset(storage.bytes, 0, sizeof(storage.bytes));
    From* p = new (storage.bytes) From(from);

    To to;
    std::memcpy(&to, p, sizeof(to));

    return to;
}

int main()
{
    struct_with_padding a;
    std::memset(&a, 0xCC, sizeof(a));
    a.x = 1;
    a.y = 1;

    std::printf("%08x\n", bitwise_cast_clear_padding< unsigned int >(a));
}

Compile with: g++ -o clear_padding clear_padding.cpp

On x86-64 this code outputs 0001cc01, while the expected output is 00010001.
The problem is that during the placement new the compiler apparently copies the
padding bits from the `from` object to the new storage that was previously
zero-initialized. According to N4849 [class.copy.ctor]/14, the implicitly
generated copy constructor, which is called in this case, should initialize the
new object member-wise. It doesn't say anything about modifying padding bits,
and for trivially copyable types I would expect this to work.

This is important for Boost.Atomic implementation as it needs a way to
initialize padding bits of user-defined structs in order for compare_exchange
operations to work.

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.2.1-9ubuntu2'
--with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs
--enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr
--with-gcc-major-version-only --program-suffix=-9
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-gnu-unique-object
--disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib
--with-target-system-zlib=auto --enable-multiarch --disable-werror
--with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none,hsa
--without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2)

Reply via email to