Skip to content

Commit 109e940

Browse files
committed
Make it an error to disable container checks when libc++ is built with them
1 parent 5d91a59 commit 109e940

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

libcxx/include/__debug_utils/sanitizers.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,31 @@
1717
# pragma GCC system_header
1818
#endif
1919

20-
// _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS determines whether the containers should provide ASAN container
21-
// overflow checks. Some containers like std::string need stricter requirements in order to enable these
22-
// checks and also need to check that the library was built with sanitizer support (_LIBCPP_INSTRUMENTED_WITH_ASAN).
20+
// Within libc++, _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS determines whether the containers should
21+
// provide ASAN container overflow checks. That setting attempts to honour ASAN's documented option
22+
// __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ which can be defined by users to disable container overflow
23+
// checks.
24+
//
25+
// However, since parts of some containers (e.g. std::string) are compiled separately into the built
26+
// library, there are caveats:
27+
// - __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ can't always be honoured, i.e. if the built library
28+
// was compiled with ASAN container checks, it's impossible to turn them off afterwards. We diagnose
29+
// this with an error to avoid the proliferation of invalid configurations that appear to work.
30+
//
31+
// - The container overflow checks themselves are not always available even when the user is compiling
32+
// with -fsanitize=address. If a container is compiled separately like std::string, it can't provide
33+
// container checks unless the separately compiled code was built with container checks enabled. These
34+
// containers need to also conditionalize whether they provide overflow checks on `_LIBCPP_INSTRUMENTED_WITH_ASAN`.
2335
#if __has_feature(address_sanitizer) && !defined(__SANITIZER_DISABLE_CONTAINER_OVERFLOW__)
2436
# define _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS 1
2537
#else
2638
# define _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS 0
2739
#endif
2840

41+
#if _LIBCPP_INSTRUMENTED_WITH_ASAN && !_LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
42+
# error "We can't disable ASAN container checks when libc++ has been built with these checks enabled"
43+
#endif
44+
2945
#if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS
3046

3147
extern "C" {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
10+
11+
// REQUIRES: libcpp-instrumented-with-asan
12+
13+
// Check that we diagnose when libc++ has been built with ASAN instrumentation
14+
// and the user requests turning off the ASAN container checks. Since that is
15+
// impossible to implement, we diagnose this with an error instead.
16+
//
17+
// ADDITIONAL_COMPILE_FLAGS: -D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__
18+
19+
#include <deque>
20+
#include <string>
21+
#include <vector>
22+
23+
// expected-error@*:* {{We can't disable ASAN container checks when libc++ has been built with these checks enabled}}

libcxx/test/extensions/libcxx/asan/disable_container_overflow_checks.pass.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77
//===----------------------------------------------------------------------===//
88

99
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
10-
// REQUIRES: asan
1110

1211
// Check that libc++ honors when __SANITIZER_DISABLE_CONTAINER_OVERFLOW__ is set
1312
// and disables the container overflow checks.
13+
//
14+
// ADDITIONAL_COMPILE_FLAGS: -fsanitize=address -D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__
15+
16+
// When libc++ is build with ASAN instrumentation, we can't turn off the ASAN checks,
17+
// and that is diagnosed as an error.
18+
// UNSUPPORTED: libcpp-instrumented-with-asan
1419

15-
// ADDITIONAL_COMPILE_FLAGS: -D__SANITIZER_DISABLE_CONTAINER_OVERFLOW__
20+
// MSAN, TSAN and ASAN are mutually exclusive
21+
// UNSUPPORTED: msan, tsan
1622

1723
#include <deque>
1824
#include <string>

libcxx/utils/libcxx/test/features/libcxx_macros.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
true_false_macros = {
4545
"_LIBCPP_HAS_THREAD_API_EXTERNAL": "libcpp-has-thread-api-external",
4646
"_LIBCPP_HAS_THREAD_API_PTHREAD": "libcpp-has-thread-api-pthread",
47+
"_LIBCPP_INSTRUMENTED_WITH_ASAN": "libcpp-instrumented-with-asan",
4748
}
4849
for macro, feature in true_false_macros.items():
4950
features.append(

0 commit comments

Comments
 (0)