-
Notifications
You must be signed in to change notification settings - Fork 16.2k
[libc++] Honor __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ in libc++ #168955
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
Merged
ldionne
merged 17 commits into
llvm:main
from
ldionne:review/use-sanitizer-container-macro
Jan 27, 2026
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7a4cab0
[libc++] Honor __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ in libc++
ldionne 172c16a
Improve test
ldionne b29c9dc
Merge branch 'main' into review/use-sanitizer-container-macro
ldionne 5d91a59
Add comment about disablement not being honored in string
ldionne 109e940
Make it an error to disable container checks when libc++ is built wit…
ldionne e87eb82
Clarify and deduplicate string comment
ldionne ed69558
Fix typo in comment
ldionne b4a74c6
Disable test in C++03 due to string::data()
ldionne 252e211
Add UNSUPPORTED: gcc
ldionne 5150405
UNSUPPORTED until C++17
ldionne b35f27e
Merge branch 'main' into review/use-sanitizer-container-macro
ldionne c3a9e52
Add a new CI job with libc++ built without ASAN but the test suite wi…
ldionne 80852ea
Merge branch 'main' into review/use-sanitizer-container-macro
ldionne ff6af78
Only chck is_string_asan_correct if string asan are compiled in
ldionne 529c621
Merge branch 'main' into review/use-sanitizer-container-macro
ldionne a5be789
Try XFAIL instead of UNSUPPORTED
ldionne edb1ba3
Reword error message
ldionne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Don't build the library itself with ASAN support, but enable ASAN in the test suite. | ||
| set(LIBCXX_TEST_PARAMS "use_sanitizer=Address" CACHE STRING "") | ||
| set(LIBCXXABI_TEST_PARAMS "${LIBCXX_TEST_PARAMS}" CACHE STRING "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
libcxx/test/extensions/libcxx/asan/diagnose_container_overflow_checks_disablement.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // XFAIL: FROZEN-CXX03-HEADERS-FIXME | ||
|
|
||
| // Check that we diagnose when libc++ has been built with ASAN instrumentation | ||
| // and the user requests turning off the ASAN container checks. Since that is | ||
| // impossible to implement, we diagnose this with an error instead. | ||
| // | ||
| // REQUIRES: libcpp-instrumented-with-asan | ||
| // ADDITIONAL_COMPILE_FLAGS: -D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__ | ||
|
|
||
| #include <deque> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| // expected-error@*:* {{We can't disable ASAN container checks when libc++ has been built with ASAN container checks enabled}} |
83 changes: 83 additions & 0 deletions
83
libcxx/test/extensions/libcxx/asan/disable_container_overflow_checks.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // Check that libc++ honors when __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ is set | ||
| // and disables the container overflow checks. | ||
| // | ||
| // REQUIRES: asan | ||
| // ADDITIONAL_COMPILE_FLAGS: -D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__ | ||
|
|
||
| // When libc++ is built with ASAN instrumentation, we can't turn off the ASAN checks, | ||
| // and that is diagnosed as an error. | ||
| // XFAIL: libcpp-instrumented-with-asan | ||
|
|
||
| // std::basic_string::data is const util C++17 | ||
| // UNSUPPORTED: c++03, c++11, c++14 | ||
|
|
||
| // The protocol checked by this test is specific to Clang and compiler-rt | ||
| // UNSUPPORTED: gcc | ||
|
|
||
| #include <deque> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| // This check is somewhat weak because it would pass if we renamed the libc++-internal | ||
| // macro and forgot to update this test. But it doesn't hurt to check it in addition to | ||
| // the tests below. | ||
| #if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS | ||
| # error "Container overflow checks should be disabled in libc++" | ||
| #endif | ||
|
|
||
| void vector() { | ||
| std::vector<int> v; | ||
| v.reserve(100); | ||
| int* data = v.data(); | ||
|
|
||
| // This is illegal with respect to std::vector, but legal from the core language perspective since | ||
| // we do own that allocated memory and `int` is an implicit lifetime type. If container overflow | ||
| // checks are enabled, this would fail. | ||
| data[4] = 42; | ||
| } | ||
|
|
||
| // For std::string, we must use a custom char_traits class to reliably test this behavior. Since | ||
| // std::string is externally instantiated in the built library, __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ | ||
| // will not be honored for any function that happens to be in the built library. Using a custom | ||
| // char_traits class ensures that this doesn't get in the way. | ||
| struct my_char_traits : std::char_traits<char> {}; | ||
|
|
||
| void string() { | ||
| std::basic_string<char, my_char_traits> s; | ||
| s.reserve(100); | ||
| char* data = s.data(); | ||
| data[4] = 'x'; | ||
| } | ||
|
|
||
| void deque() { | ||
| std::deque<int> d; | ||
| d.push_back(1); | ||
| d.push_back(2); | ||
| d.push_back(3); | ||
| int* last_element = &d[2]; | ||
| d.pop_back(); | ||
|
|
||
| // This reference is technically invalidated according to the library. However since | ||
| // we know std::deque is implemented using segments of a fairly large size and we know | ||
| // the non-erased elements are not invalidated by pop_front() (per the Standard), we can | ||
| // rely on the fact that the last element still exists in memory that is owned by the | ||
| // std::deque. | ||
| // | ||
| // If container overflow checks were enabled, this would obviously fail. | ||
| *last_element = 42; | ||
| } | ||
|
|
||
| int main(int, char**) { | ||
| vector(); | ||
| string(); | ||
| deque(); | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DanBlackwell @padriff
I realized something rather important while re-writing this test. Basically,
__SANITIZER_DISABLE_CONTAINER_OVERFLOW__doesn't really work for any container that is (partly or fully) compiled in a separate library. That's kind of obvious when you think about it, but it means that e.g.std::stringwon't be able to honour this setting because thecharspecialization with defaultchar_traitsis partly instantiated in the dylib. That's really unfortunate.So, basically, if
libc++.dylibhas been built with ASAN enabled, you'll be unable to turn off the sanitizer checks by defining__SANITIZER_DISABLE_CONTAINER_OVERFLOW__in your own translation unit. The reverse is not a problem per se: iflibc++.dylibhas been built with ASAN disabled,std::stringwill never enable container overflow checks, because it knows it can't actually implement it. Hence, it doesn't matter whether you define__SANITIZER_DISABLE_CONTAINER_OVERFLOW__or not, you're never getting any checks.IMO this goes back to my initial comments about ASAN being a fundamentally ABI-affecting property that really needs "a different slice". Mixing TUs built with ASAN and TUs built without ASAN is always going to be fragile.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this some more, we could actually "prevent" the invalid/surprising behaviour in
std::string. We could say that attempting to disable container overflow checks when using alibc++.dylibthat has been built with ASAN enabled is just not valid, and produce an error. Something like:That way, we catch the potential misuse immediately. In practice, this should affect a much smaller number of users since I'm not aware of anyone shipping a sanitized version of libc++ in their toolchain.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this, it seems in line with what I'd expect as a user. My only comment would be that the wording should indicate that it's libcxx (rather than just 'the library'), but I presume that's not the final wording in your comment anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the approach. I agree that it is very unlikely for someone to ship a sanitized libcxx. If they are building that way it is more than likely to allow them to have a fully sanitized stack for testing purposes