1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // This test validates that ASan annotations are correctly updated after swaps.
12 // This test meticulously interchanges objects that store data both within their internal memory (Short
13 // String Optimization) and in external buffers (non-SSO).
18 #include "test_macros.h"
19 #include "asan_testing.h"
21 template <class CharT
>
22 void test(const CharT val
) {
23 using S
= std::basic_string
<CharT
>;
29 std::swap(empty_s
, empty_s
);
30 std::swap(short_s
, short_s
);
31 std::swap(long_s
, long_s
);
32 LIBCPP_ASSERT(is_string_asan_correct(empty_s
));
33 LIBCPP_ASSERT(is_string_asan_correct(short_s
));
34 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
36 std::swap(empty_s
, short_s
);
37 LIBCPP_ASSERT(is_string_asan_correct(empty_s
));
38 LIBCPP_ASSERT(is_string_asan_correct(short_s
));
40 std::swap(empty_s
, short_s
);
41 LIBCPP_ASSERT(is_string_asan_correct(empty_s
));
42 LIBCPP_ASSERT(is_string_asan_correct(short_s
));
44 std::swap(empty_s
, long_s
);
45 LIBCPP_ASSERT(is_string_asan_correct(empty_s
));
46 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
48 std::swap(empty_s
, long_s
);
49 LIBCPP_ASSERT(is_string_asan_correct(empty_s
));
50 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
52 std::swap(short_s
, long_s
);
53 LIBCPP_ASSERT(is_string_asan_correct(short_s
));
54 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
56 std::swap(short_s
, long_s
);
57 LIBCPP_ASSERT(is_string_asan_correct(short_s
));
58 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
60 S
long_s2(11100, val
);
62 std::swap(long_s
, long_s2
);
63 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
64 LIBCPP_ASSERT(is_string_asan_correct(long_s2
));
66 std::swap(long_s
, long_s2
);
67 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
68 LIBCPP_ASSERT(is_string_asan_correct(long_s2
));
72 std::swap(long_s
, long_s3
);
73 LIBCPP_ASSERT(is_string_asan_correct(long_s
));
74 LIBCPP_ASSERT(is_string_asan_correct(long_s2
));
77 int main(int, char**) {
79 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
82 #if TEST_STD_VER >= 11
86 #if TEST_STD_VER >= 20