| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <string> |
| 10 | |
| 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). |
| 14 | |
| 15 | #include <string> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "asan_testing.h" |
| 20 | |
| 21 | template <class CharT> |
| 22 | void test(const CharT val) { |
| 23 | using S = std::basic_string<CharT>; |
| 24 | |
| 25 | S empty_s; |
| 26 | S short_s(3, val); |
| 27 | S long_s(1100, val); |
| 28 | |
| 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)); |
| 35 | |
| 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)); |
| 39 | |
| 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)); |
| 43 | |
| 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)); |
| 47 | |
| 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)); |
| 51 | |
| 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)); |
| 55 | |
| 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)); |
| 59 | |
| 60 | S long_s2(11100, val); |
| 61 | |
| 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)); |
| 65 | |
| 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)); |
| 69 | |
| 70 | S long_s3(111, val); |
| 71 | |
| 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)); |
| 75 | } |
| 76 | |
| 77 | int main(int, char**) { |
| 78 | test<char>(val: 'x'); |
| 79 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 80 | test<wchar_t>(val: L'x'); |
| 81 | #endif |
| 82 | #if TEST_STD_VER >= 11 |
| 83 | test<char16_t>(u'x'); |
| 84 | test<char32_t>(U'x'); |
| 85 | #endif |
| 86 | #if TEST_STD_VER >= 20 |
| 87 | test<char8_t>(u8'x'); |
| 88 | #endif |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |