From 7bef7d3d63e295a9beb3bfb6f31f73906bdcca07 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Fri, 12 Aug 2022 19:41:43 -0700 Subject: [PATCH 1/2] Test ASAN SSO string copies To avoid regressing DevCom-10116361 / VSO-1590908 Also: Document `_Construct` precondition that elements must be value-initialized, use in all branches. --- stl/inc/xstring | 7 +++--- .../GH_002030_asan_annotate_string/test.cpp | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/stl/inc/xstring b/stl/inc/xstring index 28593f1cca..0aa85d82cd 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -2690,7 +2690,9 @@ private: enum class _Construct_strategy : uint8_t { _From_char, _From_ptr, _From_string }; template <_Construct_strategy _Strat, class _Char_or_ptr> _CONSTEXPR20 void _Construct(const _Char_or_ptr _Arg, _CRT_GUARDOVERFLOW const size_type _Count) { - // Pre: *this is in SSO mode; the lifetime of the SSO elements has already begun + auto& _My_data = _Mypair._Myval2; + _STL_INTERNAL_CHECK(!_My_data._Large_string_engaged()); + _STL_INTERNAL_CHECK(_STD count(_My_data._Bx._Buf, _My_data._Bx._Buf + _BUF_SIZE, _Elem()) == _BUF_SIZE); if constexpr (_Strat == _Construct_strategy::_From_char) { _STL_INTERNAL_STATIC_ASSERT(is_same_v<_Char_or_ptr, _Elem>); @@ -2702,7 +2704,6 @@ private: _Xlen_string(); // result too long } - auto& _My_data = _Mypair._Myval2; auto& _Al = _Getal(); auto&& _Alproxy = _GET_PROXY_ALLOCATOR(_Alty, _Al); _Container_proxy_ptr<_Alty> _Proxy(_Alproxy, _My_data); @@ -2712,10 +2713,8 @@ private: _My_data._Myres = _BUF_SIZE - 1; if constexpr (_Strat == _Construct_strategy::_From_char) { _Traits::assign(_My_data._Bx._Buf, _Count, _Arg); - _Traits::assign(_My_data._Bx._Buf[_Count], _Elem()); } else if constexpr (_Strat == _Construct_strategy::_From_ptr) { _Traits::move(_My_data._Bx._Buf, _Arg, _Count); - _Traits::assign(_My_data._Bx._Buf[_Count], _Elem()); } else { // _Strat == _Construct_strategy::_From_string #ifdef _INSERT_STRING_ANNOTATION _Traits::move(_My_data._Bx._Buf, _Arg, _Count); diff --git a/tests/std/tests/GH_002030_asan_annotate_string/test.cpp b/tests/std/tests/GH_002030_asan_annotate_string/test.cpp index 0571ae4691..6c4ad22ba8 100644 --- a/tests/std/tests/GH_002030_asan_annotate_string/test.cpp +++ b/tests/std/tests/GH_002030_asan_annotate_string/test.cpp @@ -1822,6 +1822,28 @@ void run_allocator_matrix() { run_custom_allocator_matrix(); } +void test_DevCom_10116361() { + // We failed to null-terminate copies of SSO strings with ASAN annotations active. +#ifdef _WIN64 + constexpr const char* text = "testtest"; + constexpr size_t n = 8; +#else + constexpr const char* text = "test"; + constexpr size_t n = 4; +#endif + + string s0{text}; + assert(s0.c_str()[n] == '\0'); + + alignas(string) unsigned char space[sizeof(string)]; + memset(space, 0xff, sizeof(space)); + + string& s1 = *::new (&space) string{s0}; + assert(s1.c_str()[n] == '\0'); + + s1.~string(); +} + int main() { run_allocator_matrix(); #ifdef __cpp_char8_t @@ -1830,6 +1852,8 @@ int main() { run_allocator_matrix(); run_allocator_matrix(); run_allocator_matrix(); + + test_DevCom_10116361(); } #endif // TRANSITION, VSO-1586016 From 050d856c9a35f1fddbde3dd3bfab0c0fc4120c1a Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 18 Aug 2022 17:14:13 -0700 Subject: [PATCH 2/2] Include `` for True Placement New. --- tests/std/tests/GH_002030_asan_annotate_string/test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/std/tests/GH_002030_asan_annotate_string/test.cpp b/tests/std/tests/GH_002030_asan_annotate_string/test.cpp index 6c4ad22ba8..4d2b11aa43 100644 --- a/tests/std/tests/GH_002030_asan_annotate_string/test.cpp +++ b/tests/std/tests/GH_002030_asan_annotate_string/test.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #if _HAS_CXX17