xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_swap/swap.asan.pass.cpp (revision 9ed20568e7de53dce85f1631d7d8c1415e7930ae)
1*9ed20568STacet //===----------------------------------------------------------------------===//
2*9ed20568STacet //
3*9ed20568STacet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*9ed20568STacet // See https://llvm.org/LICENSE.txt for license information.
5*9ed20568STacet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*9ed20568STacet //
7*9ed20568STacet //===----------------------------------------------------------------------===//
8*9ed20568STacet 
9*9ed20568STacet // <string>
10*9ed20568STacet 
11*9ed20568STacet // This test validates that ASan annotations are correctly updated after swaps.
12*9ed20568STacet // This test meticulously interchanges objects that store data both within their internal memory (Short
13*9ed20568STacet // String Optimization) and in external buffers (non-SSO).
14*9ed20568STacet 
15*9ed20568STacet #include <string>
16*9ed20568STacet #include <cassert>
17*9ed20568STacet 
18*9ed20568STacet #include "test_macros.h"
19*9ed20568STacet #include "asan_testing.h"
20*9ed20568STacet 
21*9ed20568STacet template <class CharT>
test(const CharT val)22*9ed20568STacet void test(const CharT val) {
23*9ed20568STacet   using S = std::basic_string<CharT>;
24*9ed20568STacet 
25*9ed20568STacet   S empty_s;
26*9ed20568STacet   S short_s(3, val);
27*9ed20568STacet   S long_s(1100, val);
28*9ed20568STacet 
29*9ed20568STacet   std::swap(empty_s, empty_s);
30*9ed20568STacet   std::swap(short_s, short_s);
31*9ed20568STacet   std::swap(long_s, long_s);
32*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(empty_s));
33*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(short_s));
34*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
35*9ed20568STacet 
36*9ed20568STacet   std::swap(empty_s, short_s);
37*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(empty_s));
38*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(short_s));
39*9ed20568STacet 
40*9ed20568STacet   std::swap(empty_s, short_s);
41*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(empty_s));
42*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(short_s));
43*9ed20568STacet 
44*9ed20568STacet   std::swap(empty_s, long_s);
45*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(empty_s));
46*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
47*9ed20568STacet 
48*9ed20568STacet   std::swap(empty_s, long_s);
49*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(empty_s));
50*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
51*9ed20568STacet 
52*9ed20568STacet   std::swap(short_s, long_s);
53*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(short_s));
54*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
55*9ed20568STacet 
56*9ed20568STacet   std::swap(short_s, long_s);
57*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(short_s));
58*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
59*9ed20568STacet 
60*9ed20568STacet   S long_s2(11100, val);
61*9ed20568STacet 
62*9ed20568STacet   std::swap(long_s, long_s2);
63*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
64*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s2));
65*9ed20568STacet 
66*9ed20568STacet   std::swap(long_s, long_s2);
67*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
68*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s2));
69*9ed20568STacet 
70*9ed20568STacet   S long_s3(111, val);
71*9ed20568STacet 
72*9ed20568STacet   std::swap(long_s, long_s3);
73*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s));
74*9ed20568STacet   LIBCPP_ASSERT(is_string_asan_correct(long_s2));
75*9ed20568STacet }
76*9ed20568STacet 
main(int,char **)77*9ed20568STacet int main(int, char**) {
78*9ed20568STacet   test<char>('x');
79*9ed20568STacet #ifndef TEST_HAS_NO_WIDE_CHARACTERS
80*9ed20568STacet   test<wchar_t>(L'x');
81*9ed20568STacet #endif
82*9ed20568STacet #if TEST_STD_VER >= 11
83*9ed20568STacet   test<char16_t>(u'x');
84*9ed20568STacet   test<char32_t>(U'x');
85*9ed20568STacet #endif
86*9ed20568STacet #if TEST_STD_VER >= 20
87*9ed20568STacet   test<char8_t>(u8'x');
88*9ed20568STacet #endif
89*9ed20568STacet 
90*9ed20568STacet   return 0;
91*9ed20568STacet }
92