Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASan][libc++] Correct (explicit) annotation size #79292

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__
__old_sz = __n_copy + __n_add + __sec_cp_sz;
__set_long_size(__old_sz);
traits_type::assign(__p[__old_sz], value_type());
__annotate_new(__old_cap + __delta_cap);
__annotate_new(__old_sz);
}

// __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ TEST_CONSTEXPR_CXX20 void test_string() {
test(S(), "12345678901234567890", 1, S("1"));
test(S(), "12345678901234567890", 3, S("123"));
test(S(), "12345678901234567890", 20, S("12345678901234567890"));
test(S(), "1234567890123456789012345678901234567890", 40, S("1234567890123456789012345678901234567890"));

test(S("12345"), "", 0, S("12345"));
test(S("12345"), "12345", 5, S("1234512345"));
Expand All @@ -44,6 +45,23 @@ TEST_CONSTEXPR_CXX20 void test_string() {
test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
test(S("12345678901234567890"), "12345678901234567890", 20, S("1234567890123456789012345678901234567890"));

// Starting from long string (no SSO)
test(S("1234567890123456789012345678901234567890"), "", 0, S("1234567890123456789012345678901234567890"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests fail without the changes in <string>?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I think old code is correct (at leas, has correct results) with how the __grow_by_and_replace function is used right now.

However, analogical tests helped me detect a problem some time ago in a different place, so I just added those test cases here for future, as I was already looking at that file.

Trying to find an example of incorrect behavior, I ran locally test below and both, old and new version passed. (I tested with and without short string annotations.)

template <class S>
void test_asan(size_t const a, size_t const b, size_t const c) {
  S sa(a, 'a'), sb(b, 'b'), sc(c, 'c');
  LIBCPP_ASSERT(is_string_asan_correct(sa));
  sa.append(sb.c_str(), sb.size());
  LIBCPP_ASSERT(is_string_asan_correct(sa));
  LIBCPP_ASSERT(is_string_asan_correct(sb));
  LIBCPP_ASSERT(sa.size() == a + b);
  sa.append(sc.c_str(), sc.size());
  LIBCPP_ASSERT(is_string_asan_correct(sa));
  LIBCPP_ASSERT(is_string_asan_correct(sc));
  LIBCPP_ASSERT(sa.size() == a + b + c);
}

void test_loop() {
  for(size_t i = 0; i < 120; ++i)
    for(size_t j = 0; j < 120; ++j)
      for(size_t k = 0; k < 120; ++k) {
        test_asan<std::string>(i, j, k);
      }
}

I briefly review other functions using __grow_by_and_replace and I think there is no possible test case causing incorrect annotation with old code.

But I'm focused on understanding why #79049 got reverted now, so here I was mostly checking that the new version works as expected.

Copy link
Member Author

@AdvenamTacet AdvenamTacet Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, I could create two separate PRs, but those tests are related (one of them tests this function, and rest I added just because I was already modifying that file.).

Edit: I added one more test case, to test changing buffer from long string.

test(S("1234567890123456789012345678901234567890"), "a", 1, S("1234567890123456789012345678901234567890a"));
test(S("1234567890123456789012345678901234567890"),
"aaaaaaaaaa",
10,
S("1234567890123456789012345678901234567890aaaaaaaaaa"));
test(S("1234567890123456789012345678901234567890"),
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
300,
S("1234567890123456789012345678901234567890aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaa"));
}

TEST_CONSTEXPR_CXX20 bool test() {
Expand Down
Loading