std::vector<std::aligned_storage>格式不正确吗?

Is std::vector<std::aligned_storage> ill-formed?

提问人:blonded04 提问时间:3/2/2023 最后编辑:blonded04 更新时间:3/2/2023 访问量:135

问:

当我尝试操纵时,地址消毒器会发疯:std::vector<std::aligned_storage<sizeof(T), alignof(T)>> vec

  1. 有时,在向量元素处放置新位置效果很好,除非是小::new(&vec[some_index]) T(std::forward<Args>(args)...)vec[some_index]vecTstd::string
  2. std::destroy_at(std::launder(reinterpret_cast<T *>(&vec[some_index])))根本不起作用——而且,正如 cppreference 所说:“如果是数组类型,则程序格式不正确”——而 std::aligned_storage 上的 cppreference 示例使用 finely 的实例。Tstd::destroy_atstd::aligned_storage

提供了以下示例:

#include <string>
#include <type_traits>
#include <vector>
#include <new>
#include <memory>

int main() {
    std::vector<std::aligned_storage<sizeof(std::string), alignof(std::string)>>
        vec(1);
    
    ::new(&vec[0]) std::string(334, 'a');  // Fine
    std::destroy_at(std::launder(reinterpret_cast<std::string *>(&vec[0])));  // Still fine

    ::new(&vec[0]) std::string(334, 'a');  // Okay
    std::destroy_at(std::launder(reinterpret_cast<std::string *>(&vec[0])));  // Still okay

    ::new(&vec[0]) std::string(3, 'a');  // Cool
    std::destroy_at(std::launder(reinterpret_cast<std::string *>(&vec[0])));  // Breaks the code at previous line
}

ASAN 输出如下:

==22152==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000020 at pc 0x7fa1a8c27f26 bp 0x7fff54d86410 sp 0x7fff54d85bb8
WRITE of size 3 at 0x602000000020 thread T0
    #0 0x7fa1a8c27f25 in __interceptor_memset ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:795
    #1 0x7fa1a8b4ef56 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct(unsigned long, char) (/lib/x86_64-linux-gnu/libstdc++.so.6+0x142f56)
    #2 0x557bafbcb8a5 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(unsigned long, char, std::allocator<char> const&) /usr/include/c++/10/bits/basic_string.h:542
    #3 0x557bafbcb613 in main testing.cpp:14
    #4 0x7fa1a86d4082 in __libc_start_main ../csu/libc-start.c:308
    #5 0x557bafbcb34d in _start (/home/valery/repos/effective-cpp/tasks/a.out+0x234d)

0x602000000020 is located 15 bytes to the right of 1-byte region [0x602000000010,0x602000000011)
allocated by thread T0 here:
    #0 0x7fa1a8c9ff27 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:99
    #1 0x557bafbcc532 in __gnu_cxx::new_allocator<std::aligned_storage<32ul, 8ul> >::allocate(unsigned long, void const*) /usr/include/c++/10/ext/new_allocator.h:115
    #2 0x557bafbcc39d in std::allocator_traits<std::allocator<std::aligned_storage<32ul, 8ul> > >::allocate(std::allocator<std::aligned_storage<32ul, 8ul> >&, unsigned long) /usr/include/c++/10/bits/alloc_traits.h:460
    #3 0x557bafbcc2d7 in std::_Vector_base<std::aligned_storage<32ul, 8ul>, std::allocator<std::aligned_storage<32ul, 8ul> > >::_M_allocate(unsigned long) /usr/include/c++/10/bits/stl_vector.h:346
    #4 0x557bafbcc034 in std::_Vector_base<std::aligned_storage<32ul, 8ul>, std::allocator<std::aligned_storage<32ul, 8ul> > >::_M_create_storage(unsigned long) /usr/include/c++/10/bits/stl_vector.h:361
    #5 0x557bafbcbc70 in std::_Vector_base<std::aligned_storage<32ul, 8ul>, std::allocator<std::aligned_storage<32ul, 8ul> > >::_Vector_base(unsigned long, std::allocator<std::aligned_storage<32ul, 8ul> > const&) /usr/include/c++/10/bits/stl_vector.h:305
    #6 0x557bafbcb952 in std::vector<std::aligned_storage<32ul, 8ul>, std::allocator<std::aligned_storage<32ul, 8ul> > >::vector(unsigned long, std::allocator<std::aligned_storage<32ul, 8ul> > const&) /usr/include/c++/10/bits/stl_vector.h:511
    #7 0x557bafbcb4e8 in main testing.cpp:9
    #8 0x7fa1a86d4082 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: heap-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:795 in __interceptor_memset
Shadow bytes around the buggy address:
  0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 01 fa[fa]fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==22152==ABORTING

我的设置是 / 在 x86_64 Ubuntu 20.04 上。g++-10clang++-12

我找不到任何关于 的信息,可能是因为它已被弃用。std::aligned_storage

C++ 内存管理 C++17 类型特征 放置 - 新增

评论

0赞 NathanOliver 3/2/2023
可以肯定的是,向量只需要一个可擦除的完整类型。如果您要提供一个最小的可重现代码示例,而代码不起作用,我们也许能够帮助您找到问题所在。 只是一个花哨,一个矢量是完全可以的。alligned_storagealigned_storagestd::array<char, sizeof(T)>std::array
1赞 blonded04 3/2/2023
@NathanOliver添加了一个示例,重新打开了问题
1赞 yeputons 3/2/2023
我在 Ubuntu 22.04 上遇到了类似的问题,示例更简单:不,只是堆栈分配std::vectorstd::aligned_storage

答:

2赞 Dave S 3/2/2023 #1

如上所述,问题是这是不正确的。std::vector<std::aligned_storage<sizeof(T),alignof(T)>>

std::aligned_storage<size,size>具有具有所需属性的嵌套 typedef。type

你想要的是std::vector<std::aligned_storage<sizeof(T),alignof(T)>::type>