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

            Bug ID: 102984
           Summary: strange alignment issues with
                    std::vector::emplace/push_back and overaligned type
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mail at milianw dot de
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at 
gcc dot gnu.org
  Target Milestone: ---

I'm unsure what component to report this bug to, is it an issue with the
sanitizer, compiler or libstdc++?

test.cpp:
```
#include <vector>

struct alignas(64) spinlock { int i = 0; };

int main()
{
    std::vector<spinlock> locks;
    locks.push_back(spinlock());
    return 0;
}
```

Compile with GCC 11.1.0 with ubsan and run:
```
g++ -std=c++20 -O0 -g -fsanitize=address,undefined test.cpp && ./a.out
```

Will produce this output for me:

```
test.cpp:3:20: runtime error: member access within misaligned address
0x7fe2d4898060 for type 'struct spinlock', which requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5e453 in spinlock::spinlock() /tmp/test.cpp:3
    #1 0x559ef4c5e166 in main /tmp/test.cpp:8
    #2 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #3 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

test.cpp:8:21: runtime error: reference binding to misaligned address
0x7fe2d4898060 for type 'struct value_type', which requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5e18f in main /tmp/test.cpp:8
    #1 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #2 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/stl_vector.h:1204:31: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct spinlock', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5e8fa in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #1 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #2 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #3 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/move.h:105:74: runtime error: reference binding to
misaligned address 0x7fe2d4898060 for type 'struct type', which requires 64
byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5ec0e in std::remove_reference<spinlock&>::type&&
std::move<spinlock&>(spinlock&) /usr/include/c++/11.1.0/bits/move.h:105
    #1 0x559ef4c5e906 in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #2 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #3 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #4 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/stl_vector.h:1204:21: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct spinlock', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5e92e in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #1 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #2 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #3 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/vector.tcc:121:21: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct type', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5ef70 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #1 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #2 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #3 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #4 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/move.h:78:36: runtime error: reference binding to
misaligned address 0x7fe2d4898060 for type 'struct spinlock', which requires 64
byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f11d in spinlock&&
std::forward<spinlock>(std::remove_reference<spinlock>::type&)
/usr/include/c++/11.1.0/bits/move.h:78
    #1 0x559ef4c5ef7c in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #2 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #3 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #4 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #5 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/vector.tcc:449:28: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct type', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f871 in void std::vector<spinlock, std::allocator<spinlock>
>::_M_realloc_insert<spinlock>(__gnu_cxx::__normal_iterator<spinlock*,
std::vector<spinlock, std::allocator<spinlock> > >, spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:449
    #1 0x559ef4c5efc8 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #2 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #3 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #4 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #5 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/alloc_traits.h:514:21: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct type', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f344 in void std::allocator_traits<std::allocator<spinlock>
>::construct<spinlock, spinlock>(std::allocator<spinlock>&, spinlock*,
spinlock&&) /usr/include/c++/11.1.0/bits/alloc_traits.h:514
    #1 0x559ef4c5f941 in void std::vector<spinlock, std::allocator<spinlock>
>::_M_realloc_insert<spinlock>(__gnu_cxx::__normal_iterator<spinlock*,
std::vector<spinlock, std::allocator<spinlock> > >, spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:449
    #2 0x559ef4c5efc8 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #3 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #4 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #5 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #6 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/stl_construct.h:97:62: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct type', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f15e in decltype (::new ((void*)(0))
spinlock((declval<spinlock>)())) std::construct_at<spinlock,
spinlock>(spinlock*, spinlock&&)
/usr/include/c++/11.1.0/bits/stl_construct.h:97
    #1 0x559ef4c5f362 in void std::allocator_traits<std::allocator<spinlock>
>::construct<spinlock, spinlock>(std::allocator<spinlock>&, spinlock*,
spinlock&&) /usr/include/c++/11.1.0/bits/alloc_traits.h:514
    #2 0x559ef4c5f941 in void std::vector<spinlock, std::allocator<spinlock>
>::_M_realloc_insert<spinlock>(__gnu_cxx::__normal_iterator<spinlock*,
std::vector<spinlock, std::allocator<spinlock> > >, spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:449
    #3 0x559ef4c5efc8 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #4 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #5 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #6 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #7 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/stl_construct.h:97:14: runtime error: reference
binding to misaligned address 0x7fe2d4898060 for type 'struct spinlock', which
requires 64 byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f1a6 in decltype (::new ((void*)(0))
spinlock((declval<spinlock>)())) std::construct_at<spinlock,
spinlock>(spinlock*, spinlock&&)
/usr/include/c++/11.1.0/bits/stl_construct.h:97
    #1 0x559ef4c5f362 in void std::allocator_traits<std::allocator<spinlock>
>::construct<spinlock, spinlock>(std::allocator<spinlock>&, spinlock*,
spinlock&&) /usr/include/c++/11.1.0/bits/alloc_traits.h:514
    #2 0x559ef4c5f941 in void std::vector<spinlock, std::allocator<spinlock>
>::_M_realloc_insert<spinlock>(__gnu_cxx::__normal_iterator<spinlock*,
std::vector<spinlock, std::allocator<spinlock> > >, spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:449
    #3 0x559ef4c5efc8 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #4 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #5 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #6 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #7 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)

/usr/include/c++/11.1.0/bits/stl_construct.h:97: runtime error: load of
misaligned address 0x7fe2d4898060 for type 'struct spinlock', which requires 64
byte alignment
0x7fe2d4898060: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00
00 00 00  00 00 00 00
              ^ 
    #0 0x559ef4c5f1f0 in decltype (::new ((void*)(0))
spinlock((declval<spinlock>)())) std::construct_at<spinlock,
spinlock>(spinlock*, spinlock&&)
/usr/include/c++/11.1.0/bits/stl_construct.h:97
    #1 0x559ef4c5f362 in void std::allocator_traits<std::allocator<spinlock>
>::construct<spinlock, spinlock>(std::allocator<spinlock>&, spinlock*,
spinlock&&) /usr/include/c++/11.1.0/bits/alloc_traits.h:514
    #2 0x559ef4c5f941 in void std::vector<spinlock, std::allocator<spinlock>
>::_M_realloc_insert<spinlock>(__gnu_cxx::__normal_iterator<spinlock*,
std::vector<spinlock, std::allocator<spinlock> > >, spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:449
    #3 0x559ef4c5efc8 in spinlock& std::vector<spinlock,
std::allocator<spinlock> >::emplace_back<spinlock>(spinlock&&)
/usr/include/c++/11.1.0/bits/vector.tcc:121
    #4 0x559ef4c5e93d in std::vector<spinlock, std::allocator<spinlock>
>::push_back(spinlock&&) /usr/include/c++/11.1.0/bits/stl_vector.h:1204
    #5 0x559ef4c5e1a1 in main /tmp/test.cpp:8
    #6 0x7fe2d7d4ab24 in __libc_start_main (/usr/lib/libc.so.6+0x27b24)
    #7 0x559ef4c5df9d in _start (/tmp/a.out+0x8f9d)
```

Reply via email to