Skip to content

[libc++][hardening] Introduce assertion semantics. #149459

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/libcxx-build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ jobs:
'generic-abi-unstable',
'generic-hardening-mode-debug',
'generic-hardening-mode-extensive',
'generic-hardening-mode-extensive-observe-semantic',
'generic-hardening-mode-fast',
'generic-hardening-mode-fast-with-abi-breaks',
'generic-merged',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(LIBCXX_HARDENING_MODE "extensive" CACHE STRING "")
set(LIBCXX_TEST_PARAMS "assertion_semantic=observe" CACHE STRING "")
68 changes: 68 additions & 0 deletions libcxx/docs/Hardening.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ modes are:

Enabling hardening has no impact on the ABI.

.. _notes-for-users:

Notes for users
---------------

Expand Down Expand Up @@ -72,6 +74,10 @@ to control the level by passing **one** of the following options to the compiler
pre-built components. Most libc++ code is header-based, so a user-provided
value for ``_LIBCPP_HARDENING_MODE`` will be mostly respected.

In some cases, users might want to override the assertion semantic used by the
library. This can be done similarly to setting the hardening mode; please refer
to the :ref:`relevant section <assertion-semantics>`.

Notes for vendors
-----------------

Expand Down Expand Up @@ -260,6 +266,68 @@ output. This is less secure and increases the size of the binary (among other
things, it has to store the error message strings) but makes the failure easier
to debug. It also allows testing the error messages in our test suite.

This default behavior can be customized by users via :ref:`assertion semantics
<assertion-semantics>`; it can also be completely overridden by vendors by
providing a :ref:`custom assertion failure handler
<override-assertion-handler>`.

.. _assertion-semantics:

Assertion semantics
-------------------

.. warning::

Assertion semantics are currently an experimental feature.

.. note::

Assertion semantics are not available in the C++03 mode.

What happens when an assertion fails depends on the assertion semantic being
used. Four assertion semantics are available, based on C++26 Contracts
evaluation semantics:

- ``ignore`` evaluates the assertion but has no effect if it fails (note that it
differs from the Contracts ``ignore`` semantic which would not evaluate
the assertion at all);
- ``observe`` logs an error (indicating, if possible on the platform, that the
error is fatal) but continues execution;
- ``quick-enforce`` terminates the program as fast as possible via a trap
instruction. It is the default semantic for the production modes (``fast`` and
``extensive``);
- ``enforce`` logs an error and then terminates the program. It is the default
semantic for the ``debug`` mode.

Notes:

- Continuing execution after a hardening check fails results in undefined
behavior; the ``observe`` semantic is meant to make adopting hardening easier
but should not be used outside of the adoption period;
- C++26 wording for Library Hardening precludes a conforming Hardened
implementation from using the Contracts ``ignore`` semantic when evaluating
hardened preconditions in the Library. Libc++ allows using this semantic for
hardened preconditions, but please be aware that using ``ignore`` does not
produce a conforming "Hardened" implementation, unlike the other semantics
above.

The default assertion semantics are as follows:

- ``fast``: ``quick-enforce``;
- ``extensive``: ``quick-enforce``;
- ``debug``: ``enforce``.

The default assertion semantics can be overridden by passing **one** of the
following options to the compiler:

- ``-D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_IGNORE``
- ``-D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE``
- ``-D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE``
- ``-D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_ENFORCE``

All the :ref:`same notes <notes-for-users>` apply to setting this macro as for
setting ``_LIBCPP_HARDENING_MODE``.

.. _override-assertion-handler:

Overriding the assertion failure handler
Expand Down
6 changes: 6 additions & 0 deletions libcxx/docs/ReleaseNotes/21.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ Improvements and New Features

- ``ctype::tolower`` and ``ctype::toupper`` have been optimized, resulting in a 2x performance improvement.

- As an experimental feature, Hardening now supports assertion semantics that allow customizing how a hardening
assertion failure is handled. The four available semantics, modeled on C++26 Contracts, are ``ignore``, ``observe``,
``quick-enforce`` and ``enforce``. The ``observe`` semantic is intended to make it easier to adopt Hardening in
production but should not be used outside of this scenario. Please refer to the :ref:`Hardening documentation
<hardening>` for details.

Deprecations and Removals
-------------------------

Expand Down
5 changes: 5 additions & 0 deletions libcxx/docs/UserDocumentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ when ``-fexperimental-library`` is passed:
* ``std::chrono::tzdb`` and related time zone functionality
* ``<syncstream>``

Additionally, assertion semantics are an experimental feature that can be used
to customize the behavior of Hardening (see :ref:`here <assertion-semantics>`).
Assertion semantics mirror the evaluation semantics of C++26 Contracts but are
not a standard feature.

.. note::
Experimental libraries are experimental.
* The contents of the ``<experimental/...>`` headers and the associated static
Expand Down
90 changes: 66 additions & 24 deletions libcxx/include/__config
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@
# define _LIBCPP_FREESTANDING
# endif

// NOLINTNEXTLINE(libcpp-cpp-version-check)
# if __cplusplus < 201103L
# define _LIBCPP_CXX03_LANG
# endif

# if __has_feature(experimental_library)
# ifndef _LIBCPP_ENABLE_EXPERIMENTAL
# define _LIBCPP_ENABLE_EXPERIMENTAL
# endif
# endif

// Incomplete features get their own specific disabling flags. This makes it
// easier to grep for target specific flags once the feature is complete.
# if defined(_LIBCPP_ENABLE_EXPERIMENTAL) || defined(_LIBCPP_BUILDING_LIBRARY)
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 1
# else
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 0
# endif

# define _LIBCPP_HAS_EXPERIMENTAL_PSTL _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY

// HARDENING {

// TODO: Remove in LLVM 21. We're making this an error to catch folks who might not have migrated.
Expand Down Expand Up @@ -147,16 +171,53 @@ _LIBCPP_HARDENING_MODE_EXTENSIVE, \
_LIBCPP_HARDENING_MODE_DEBUG
# endif

// Hardening assertion semantics generally mirror the evaluation semantics of C++26 Contracts:
// - `ignore` evaluates the assertion but doesn't do anything if it fails (note that it differs from the Contracts
// `ignore` semantic which wouldn't evaluate the assertion at all);
// - `observe` logs an error (indicating, if possible, that the error is fatal) and continues execution;
// - `quick-enforce` terminates the program as fast as possible (via trapping);
// - `enforce` logs an error and then terminates the program.
//
// Notes:
// - Continuing execution after a hardening check fails results in undefined behavior; the `observe` semantic is meant
// to make adopting hardening easier but should not be used outside of this scenario;
// - C++26 wording for Library Hardening precludes a conforming Hardened implementation from using the Contracts
// `ignore` semantic when evaluating hardened preconditions in the Library. Libc++ allows using this semantic for
// hardened preconditions, however, be aware that using `ignore` does not produce a conforming "Hardened"
// implementation, unlike the other semantics above.
// clang-format off
# define _LIBCPP_ASSERTION_SEMANTIC_IGNORE (1 << 1)
# define _LIBCPP_ASSERTION_SEMANTIC_OBSERVE (1 << 2)
# define _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE (1 << 3)
# define _LIBCPP_ASSERTION_SEMANTIC_ENFORCE (1 << 4)
// clang-format on

// Allow users to define an arbitrary assertion semantic; otherwise, use the default mapping from modes to semantics.
// The default is for production-capable modes to use `quick-enforce` (i.e., trap) and for the `debug` mode to use
// `enforce` (i.e., log and abort).
# ifndef _LIBCPP_ASSERTION_SEMANTIC

# if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
# define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
# else
# define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
# endif

# else
# if !_LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# error "Assertion semantics are an experimental feature."
# endif
# if defined(_LIBCPP_CXX03_LANG)
# error "Assertion semantics are not available in the C++03 mode."
# endif

# endif // _LIBCPP_ASSERTION_SEMANTIC

// } HARDENING

# define _LIBCPP_TOSTRING2(x) #x
# define _LIBCPP_TOSTRING(x) _LIBCPP_TOSTRING2(x)

// NOLINTNEXTLINE(libcpp-cpp-version-check)
# if __cplusplus < 201103L
# define _LIBCPP_CXX03_LANG
# endif
Comment on lines -156 to -158
Copy link
Member

Choose a reason for hiding this comment

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

How about moving this to <__configuration/language.h> instead?


# ifndef __has_constexpr_builtin
# define __has_constexpr_builtin(x) 0
# endif
Expand Down Expand Up @@ -190,25 +251,6 @@ _LIBCPP_HARDENING_MODE_DEBUG
# define _LIBCPP_ABI_VCRUNTIME
# endif

# if __has_feature(experimental_library)
# ifndef _LIBCPP_ENABLE_EXPERIMENTAL
# define _LIBCPP_ENABLE_EXPERIMENTAL
# endif
# endif

// Incomplete features get their own specific disabling flags. This makes it
// easier to grep for target specific flags once the feature is complete.
# if defined(_LIBCPP_ENABLE_EXPERIMENTAL) || defined(_LIBCPP_BUILDING_LIBRARY)
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 1
# else
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 0
# endif

# define _LIBCPP_HAS_EXPERIMENTAL_PSTL _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY
# define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY

# if defined(__MVS__)
# include <features.h> // for __NATIVE_ASCII_F
# endif
Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/__cxx03/__config
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ _LIBCPP_HARDENING_MODE_EXTENSIVE, \
_LIBCPP_HARDENING_MODE_DEBUG
# endif

# ifdef _LIBCPP_ASSERTION_SEMANTIC
# error "Assertion semantics are not available in the C++03 mode."
# endif

// } HARDENING

# define _LIBCPP_TOSTRING2(x) #x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// UNSUPPORTED: no-threads
// UNSUPPORTED: c++03, c++11, c++14, c++17
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// Without the assertion, the test will most likely time out.
// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}

// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

// REQUIRES: has-unix-headers
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// Without the assertion, the test will most likely time out.
// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

#include <latch>
Expand Down
Loading
Loading