diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt --- a/libcxx/CMakeLists.txt +++ b/libcxx/CMakeLists.txt @@ -48,10 +48,6 @@ include(HandleCompilerRT) # Basic options --------------------------------------------------------------- -option(LIBCXX_ENABLE_ASSERTIONS - "Enable assertions inside the compiled library, and at the same time make it the - default when compiling user code. Note that assertions can be enabled or disabled - by users in their own code regardless of this option." OFF) option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON) option(LIBCXX_ENABLE_FILESYSTEM @@ -69,6 +65,10 @@ message(FATAL_ERROR "Unsupported hardening mode: '${LIBCXX_HARDENING_MODE}'. Supported values are ${LIBCXX_SUPPORTED_HARDENING_MODES}.") endif() +option(LIBCXX_ENABLE_ASSERTIONS + "This is a deprecated option that is only provided for backward compatibility. + Enabling this option is equivalent to setting LIBCXX_HARDENING_MODE to + 'hardened'." OFF) option(LIBCXX_ENABLE_RANDOM_DEVICE "Whether to include support for std::random_device in the library. Disabling this can be useful when building the library for platforms that don't have @@ -777,11 +777,6 @@ config_define_if_not(LIBCXX_ENABLE_UNICODE _LIBCPP_HAS_NO_UNICODE) config_define_if_not(LIBCXX_ENABLE_WIDE_CHARACTERS _LIBCPP_HAS_NO_WIDE_CHARACTERS) config_define_if_not(LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS) -if (LIBCXX_ENABLE_ASSERTIONS) - config_define(1 _LIBCPP_ENABLE_ASSERTIONS_DEFAULT) -else() - config_define(0 _LIBCPP_ENABLE_ASSERTIONS_DEFAULT) -endif() if (LIBCXX_HARDENING_MODE STREQUAL "hardened") config_define(1 _LIBCPP_ENABLE_HARDENED_MODE_DEFAULT) config_define(0 _LIBCPP_ENABLE_DEBUG_MODE_DEFAULT) @@ -792,6 +787,11 @@ config_define(0 _LIBCPP_ENABLE_HARDENED_MODE_DEFAULT) config_define(0 _LIBCPP_ENABLE_DEBUG_MODE_DEFAULT) endif() +# TODO(LLVM 18): Remove this after branching for LLVM 17, this is a simple +# courtesy for vendors to be notified about this change. +if (LIBCXX_ENABLE_ASSERTIONS) + message(FATAL_ERROR "LIBCXX_ENABLE_ASSERTIONS has been replaced by LIBCXX_HARDENING_MODE=hardened") +endif() if (LIBCXX_PSTL_CPU_BACKEND STREQUAL "serial") config_define(1 _LIBCPP_PSTL_CPU_BACKEND_SERIAL) diff --git a/libcxx/cmake/caches/AIX.cmake b/libcxx/cmake/caches/AIX.cmake --- a/libcxx/cmake/caches/AIX.cmake +++ b/libcxx/cmake/caches/AIX.cmake @@ -6,7 +6,7 @@ set(CMAKE_AR "/usr/bin/ar" CACHE FILEPATH "") set(LIBCXX_USE_COMPILER_RT ON CACHE BOOL "") -set(LIBCXX_ENABLE_ASSERTIONS OFF CACHE BOOL "") +set(LIBCXX_HARDENING_MODE "unchecked" CACHE STRING "") set(LIBCXX_ABI_VERSION "1" CACHE STRING "") set(LIBCXX_ENABLE_ABI_LINKER_SCRIPT OFF CACHE BOOL "") set(LIBCXX_ENABLE_SHARED ON CACHE BOOL "") diff --git a/libcxx/cmake/caches/Apple.cmake b/libcxx/cmake/caches/Apple.cmake --- a/libcxx/cmake/caches/Apple.cmake +++ b/libcxx/cmake/caches/Apple.cmake @@ -2,7 +2,7 @@ set(CMAKE_POSITION_INDEPENDENT_CODE OFF CACHE BOOL "") set(LIBCXX_USE_COMPILER_RT ON CACHE BOOL "") -set(LIBCXX_ENABLE_ASSERTIONS OFF CACHE BOOL "") +set(LIBCXX_HARDENING_MODE "unchecked" CACHE STRING "") set(LIBCXX_ABI_VERSION "1" CACHE STRING "") set(LIBCXX_ENABLE_STATIC ON CACHE BOOL "") set(LIBCXX_ENABLE_SHARED ON CACHE BOOL "") @@ -12,7 +12,6 @@ set(LIBCXX_HERMETIC_STATIC_LIBRARY ON CACHE BOOL "") set(LIBCXXABI_HERMETIC_STATIC_LIBRARY ON CACHE BOOL "") -set(LIBCXXABI_ENABLE_ASSERTIONS OFF CACHE BOOL "") set(LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST ON CACHE BOOL "") set(LIBCXX_TEST_CONFIG "apple-libc++-shared.cfg.in" CACHE STRING "") diff --git a/libcxx/cmake/caches/FreeBSD.cmake b/libcxx/cmake/caches/FreeBSD.cmake --- a/libcxx/cmake/caches/FreeBSD.cmake +++ b/libcxx/cmake/caches/FreeBSD.cmake @@ -1,7 +1,7 @@ set(CMAKE_BUILD_TYPE Release CACHE STRING "") set(CMAKE_POSITION_INDEPENDENT_CODE ON CACHE BOOL "") -set(LIBCXX_ENABLE_ASSERTIONS OFF CACHE BOOL "") +set(LIBCXX_HARDENING_MODE "unchecked" CACHE STRING "") set(LIBCXX_ABI_VERSION "1" CACHE STRING "") set(LIBCXX_ENABLE_STATIC ON CACHE BOOL "") set(LIBCXX_ENABLE_SHARED ON CACHE BOOL "") diff --git a/libcxx/cmake/caches/Generic-assertions.cmake b/libcxx/cmake/caches/Generic-assertions.cmake deleted file mode 100644 --- a/libcxx/cmake/caches/Generic-assertions.cmake +++ /dev/null @@ -1,2 +0,0 @@ -set(LIBCXX_ENABLE_ASSERTIONS ON CACHE BOOL "") -set(LIBCXXABI_ENABLE_ASSERTIONS ON CACHE BOOL "") diff --git a/libcxx/docs/BuildingLibcxx.rst b/libcxx/docs/BuildingLibcxx.rst --- a/libcxx/docs/BuildingLibcxx.rst +++ b/libcxx/docs/BuildingLibcxx.rst @@ -211,15 +211,6 @@ Toggle the installation of the libc++ headers. -.. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL - - **Default**: ``OFF`` - - Build libc++ with assertions enabled in the compiled library, and enable assertions - by default when building user code as well. Assertions can be turned off by users - by defining ``_LIBCPP_ENABLE_ASSERTIONS=0``. For details, see - :ref:`the documentation `. - .. option:: LIBCXX_ENABLE_SHARED:BOOL **Default**: ``ON`` diff --git a/libcxx/include/__assert b/libcxx/include/__assert --- a/libcxx/include/__assert +++ b/libcxx/include/__assert @@ -17,31 +17,21 @@ # pragma GCC system_header #endif -#ifndef _LIBCPP_ENABLE_ASSERTIONS -# define _LIBCPP_ENABLE_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS_DEFAULT -#endif - -#if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1 -# error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1" -#endif +#define _LIBCPP_ASSERT(expression, message) \ + (__builtin_expect(static_cast(expression), 1) \ + ? (void)0 \ + : _LIBCPP_VERBOSE_ABORT( \ + "%s:%d: assertion %s failed: %s\n", __builtin_FILE(), __builtin_LINE(), #expression, message)) -#if _LIBCPP_ENABLE_ASSERTIONS -# define _LIBCPP_ASSERT(expression, message) \ - (__builtin_expect(static_cast(expression), 1) \ - ? (void)0 \ - : _LIBCPP_VERBOSE_ABORT( \ - "%s:%d: assertion %s failed: %s\n", __builtin_FILE(), __builtin_LINE(), #expression, message)) // TODO: __builtin_assume can currently inhibit optimizations. Until this has been fixed and we can add // assumptions without a clear optimization intent, disable that to avoid worsening the code generation. // See https://discourse.llvm.org/t/llvm-assume-blocks-optimization/71609 for a discussion. -#elif 0 && __has_builtin(__builtin_assume) -# define _LIBCPP_ASSERT(expression, message) \ +#if 0 && __has_builtin(__builtin_assume) +# define _LIBCPP_ASSUME(expression) \ (_LIBCPP_DIAGNOSTIC_PUSH _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wassume") \ __builtin_assume(static_cast(expression)) _LIBCPP_DIAGNOSTIC_POP) #else -# define _LIBCPP_ASSERT(expression, message) ((void)0) +# define _LIBCPP_ASSUME(expression) ((void)0) #endif -#define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message) - #endif // _LIBCPP___ASSERT diff --git a/libcxx/include/__config b/libcxx/include/__config --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -208,6 +208,19 @@ // HARDENING { +// TODO(hardening): remove this in LLVM 18. +// This is for backward compatibility -- make enabling `_LIBCPP_ENABLE_ASSERTIONS` (which predates hardening modes) +// equivalent to setting the hardened mode. +# ifdef _LIBCPP_ENABLE_ASSERTIONS +# warning "_LIBCPP_ENABLE_ASSERTIONS is deprecated, please use _LIBCPP_ENABLE_HARDENED_MODE instead." +# if _LIBCPP_ENABLE_ASSERTIONS != 0 && _LIBCPP_ENABLE_ASSERTIONS != 1 +# error "_LIBCPP_ENABLE_ASSERTIONS must be set to 0 or 1" +# endif +# if _LIBCPP_ENABLE_ASSERTIONS +# define _LIBCPP_ENABLE_HARDENED_MODE 1 +# endif +# endif + // Enables the hardened mode which consists of all checks intended to be used in production. Hardened mode prioritizes // security-critical checks that can be done with relatively little overhead in constant time. Mutually exclusive with // `_LIBCPP_ENABLE_DEBUG_MODE`. @@ -245,26 +258,21 @@ // Hardened mode checks. # if _LIBCPP_ENABLE_HARDENED_MODE -// Automatically enable assertions in hardened mode (unless the user explicitly turned them off). -# ifndef _LIBCPP_ENABLE_ASSERTIONS -# define _LIBCPP_ENABLE_ASSERTIONS 1 -# endif - +// TODO(hardening): Once we categorize assertions, only enable the ones that we really want here. +# define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message) // TODO(hardening): more checks to be added here... // Debug mode checks. # elif _LIBCPP_ENABLE_DEBUG_MODE -// Automatically enable assertions in debug mode (unless the user explicitly turned them off). -# ifndef _LIBCPP_ENABLE_ASSERTIONS -# define _LIBCPP_ENABLE_ASSERTIONS 1 -# endif - +// TODO(hardening): Once we categorize assertions, only enable the ones that we really want here. +# define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSERT(expression, message) // TODO(hardening): more checks to be added here... // Disable all checks if neither the hardened mode nor the debug mode is enabled. # else +# define _LIBCPP_ASSERT_UNCATEGORIZED(expression, message) _LIBCPP_ASSUME(expression) // TODO: more checks to be added here... # endif // _LIBCPP_ENABLE_HARDENED_MODE diff --git a/libcxx/include/__config_site.in b/libcxx/include/__config_site.in --- a/libcxx/include/__config_site.in +++ b/libcxx/include/__config_site.in @@ -28,7 +28,6 @@ #cmakedefine _LIBCPP_HAS_NO_RANDOM_DEVICE #cmakedefine _LIBCPP_HAS_NO_LOCALIZATION #cmakedefine _LIBCPP_HAS_NO_WIDE_CHARACTERS -#cmakedefine01 _LIBCPP_ENABLE_ASSERTIONS_DEFAULT // PSTL backends #cmakedefine _LIBCPP_PSTL_CPU_BACKEND_SERIAL diff --git a/libcxx/test/CMakeLists.txt b/libcxx/test/CMakeLists.txt --- a/libcxx/test/CMakeLists.txt +++ b/libcxx/test/CMakeLists.txt @@ -24,10 +24,6 @@ serialize_lit_param(enable_rtti False) endif() -if (LIBCXX_ENABLE_ASSERTIONS) - serialize_lit_param(enable_assertions True) -endif() - serialize_lit_param(hardening_mode "\"${LIBCXX_HARDENING_MODE}\"") if (CMAKE_CXX_COMPILER_TARGET) diff --git a/libcxx/utils/ci/buildkite-pipeline.yml b/libcxx/utils/ci/buildkite-pipeline.yml --- a/libcxx/utils/ci/buildkite-pipeline.yml +++ b/libcxx/utils/ci/buildkite-pipeline.yml @@ -468,24 +468,6 @@ limit: 2 timeout_in_minutes: 120 - - label: "Assertions enabled" - command: "libcxx/utils/ci/run-buildbot generic-assertions" - artifact_paths: - - "**/test-results.xml" - - "**/*.abilist" - env: - CC: "clang-${LLVM_HEAD_VERSION}" - CXX: "clang++-${LLVM_HEAD_VERSION}" - ENABLE_CLANG_TIDY: "On" - agents: - queue: "libcxx-builders" - os: "linux" - retry: - automatic: - - exit_status: -1 # Agent was lost - limit: 2 - timeout_in_minutes: 120 - - label: "Hardened mode" command: "libcxx/utils/ci/run-buildbot generic-hardened-mode" artifact_paths: diff --git a/libcxx/utils/ci/run-buildbot b/libcxx/utils/ci/run-buildbot --- a/libcxx/utils/ci/run-buildbot +++ b/libcxx/utils/ci/run-buildbot @@ -382,12 +382,6 @@ -DLIBUNWIND_TEST_CONFIG="llvm-libunwind-merged.cfg.in" check-runtimes ;; -generic-assertions) - clean - generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-assertions.cmake" - check-runtimes - check-abi-list -;; generic-hardened-mode) clean generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Generic-hardened-mode.cmake" @@ -495,7 +489,7 @@ # TODO: It would be better to run the tests against the fake-installed version of libc++ instead xcrun --sdk macosx ninja -vC "${BUILD_DIR}/${arch}" check-cxx check-cxxabi check-cxx-abilist ;; -apple-system-backdeployment-assertions-*) +apple-system-backdeployment-hardened-*) clean if [[ "${OSX_ROOTS}" == "" ]]; then @@ -506,7 +500,7 @@ curl "${PREVIOUS_DYLIBS_URL}" | tar -xz --strip-components=1 -C "${OSX_ROOTS}" fi - DEPLOYMENT_TARGET="${BUILDER#apple-system-backdeployment-assertions-}" + DEPLOYMENT_TARGET="${BUILDER#apple-system-backdeployment-hardened-}" # TODO: On Apple platforms, we never produce libc++abi.1.dylib or libunwind.1.dylib, # only libc++abi.dylib and libunwind.dylib. Fix that in the build so that the @@ -521,7 +515,7 @@ PARAMS+=";cxx_runtime_root=${OSX_ROOTS}/macOS/libc++/${DEPLOYMENT_TARGET}" PARAMS+=";abi_runtime_root=${OSX_ROOTS}/macOS/libc++abi/${DEPLOYMENT_TARGET}" PARAMS+=";unwind_runtime_root=${OSX_ROOTS}/macOS/libunwind/${DEPLOYMENT_TARGET}" - PARAMS+=";enable_assertions=True" + PARAMS+=";hardening_mode=hardened" generate-cmake -C "${MONOREPO_ROOT}/libcxx/cmake/caches/Apple.cmake" \ -DLIBCXX_TEST_CONFIG="apple-libc++-backdeployment.cfg.in" \ diff --git a/libcxx/utils/libcxx/test/params.py b/libcxx/utils/libcxx/test/params.py --- a/libcxx/utils/libcxx/test/params.py +++ b/libcxx/utils/libcxx/test/params.py @@ -283,18 +283,6 @@ help="Whether to enable tests that take longer to run. This can be useful when running on a very slow device.", actions=lambda enabled: [] if not enabled else [AddFeature("long_tests")], ), - Parameter( - name="enable_assertions", - choices=[True, False], - type=bool, - default=False, - help="Whether to enable assertions when compiling the test suite. This is only meaningful when " - "running the tests against libc++.", - actions=lambda assertions: [] if not assertions else [ - AddCompileFlag("-D_LIBCPP_ENABLE_ASSERTIONS=1"), - AddFeature("libcpp-has-assertions"), - ], - ), Parameter( name="hardening_mode", choices=["unchecked", "hardened", "debug"],