@@ -138,17 +138,50 @@ IWYU, you should run the tool like so:
138
138
If you would prefer to not use that flag, then you can replace ``/path/to/include-what-you-use/share/libcxx.imp` ``
139
139
file with the libc++-provided ``libcxx.imp `` file.
140
140
141
+ .. _assertions-mode :
142
+
143
+ Enabling the "safe libc++" mode
144
+ ===============================
145
+
146
+ Libc++ contains a number of assertions whose goal is to catch undefined behavior in the
147
+ library, usually caused by precondition violations. Those assertions do not aim to be
148
+ exhaustive -- instead they aim to provide a good balance between safety and performance.
149
+ In particular, these assertions do not change the complexity of algorithms. However, they
150
+ might, in some cases, interfere with compiler optimizations.
151
+
152
+ By default, these assertions are turned off. Vendors can decide to turn them on while building
153
+ the compiled library by defining ``LIBCXX_ENABLE_ASSERTIONS=ON `` at CMake configuration time.
154
+ When ``LIBCXX_ENABLE_ASSERTIONS `` is used, the compiled library will be built with assertions
155
+ enabled, **and ** user code will be built with assertions enabled by default. If
156
+ ``LIBCXX_ENABLE_ASSERTIONS=OFF `` at CMake configure time, the compiled library will not contain
157
+ assertions and the default when building user code will be to have assertions disabled.
158
+ As a user, you can consult your vendor to know whether assertions are enabled by default.
159
+
160
+ Furthermore, independently of any vendor-selected default, users can always control whether
161
+ assertions are enabled in their code by defining ``_LIBCPP_ENABLE_ASSERTIONS=0|1 `` before
162
+ including any libc++ header (we recommend passing ``-D_LIBCPP_ENABLE_ASSERTIONS=X `` to the
163
+ compiler). Note that if the compiled library was built by the vendor without assertions,
164
+ functions compiled inside the static or shared library won't have assertions enabled even
165
+ if the user defines ``_LIBCPP_ENABLE_ASSERTIONS=1 `` (the same is true for the inverse case
166
+ where the static or shared library was compiled **with ** assertions but the user tries to
167
+ disable them). However, most of the code in libc++ is in the headers, so the user-selected
168
+ value for ``_LIBCPP_ENABLE_ASSERTIONS `` (if any) will usually be respected.
169
+
170
+ When an assertion fails, the program is aborted through a special verbose termination function. This
171
+ behavior is customizable; see the :ref: `Overriding the default termination handler
172
+ <termination-handler>` section for more information.
173
+
141
174
.. _termination-handler :
142
175
143
176
Overriding the default termination handler
144
177
==========================================
145
178
146
- When the library wants to terminate due to an unforeseen condition (such as a hardening assertion
147
- failure), the program is aborted through a special verbose termination function. The library provides
148
- a default function that prints an error message and calls ``std::abort() ``. Note that this function is
149
- provided by the static or shared library, so it is only available when deploying to a platform where
150
- the compiled library is sufficiently recent. On older platforms, the program will terminate in an
151
- unspecified unsuccessful manner, but the quality of diagnostics won't be great.
179
+ When the library wants to terminate due to an unforeseen condition (such as an assertion failure),
180
+ the program is aborted through a special verbose termination function. The library provides
181
+ a default function that prints an error message and calls ``std::abort() ``. Note that this function
182
+ is provided by the static or shared library, so it is only available when deploying to a platform
183
+ where the compiled library is sufficiently recent. On older platforms, the program will terminate in
184
+ an unspecified unsuccessful manner, but the quality of diagnostics won't be great.
152
185
153
186
However, users can also override that mechanism at two different levels. First, the mechanism can be
154
187
overridden at compile time by defining the ``_LIBCPP_VERBOSE_ABORT(format, args...) `` variadic macro.
@@ -191,7 +224,7 @@ and ``operator delete``. For example:
191
224
192
225
int main() {
193
226
std::vector<int> v;
194
- int& x = v[0]; // Your termination function will be called here if hardening is enabled.
227
+ int& x = v[0]; // Your termination function will be called here if _LIBCPP_ENABLE_ASSERTIONS=1
195
228
}
196
229
197
230
Also note that the verbose termination function should never return. Since assertions in libc++
@@ -206,20 +239,14 @@ Libc++ Configuration Macros
206
239
===========================
207
240
208
241
Libc++ provides a number of configuration macros which can be used to enable
209
- or disable extended libc++ behavior, including enabling hardening or thread
242
+ or disable extended libc++ behavior, including enabling the safe mode or thread
210
243
safety annotations.
211
244
212
245
**_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS **:
213
246
This macro is used to enable -Wthread-safety annotations on libc++'s
214
247
``std::mutex `` and ``std::lock_guard ``. By default, these annotations are
215
248
disabled and must be manually enabled by the user.
216
249
217
- **_LIBCPP_ENABLE_HARDENED_MODE **:
218
- This macro is used to enable the :ref: `hardened mode <using-hardened-mode >`.
219
-
220
- **_LIBCPP_ENABLE_DEBUG_MODE **:
221
- This macro is used to enable the :ref: `debug mode <using-hardened-mode >`.
222
-
223
250
**_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS **:
224
251
This macro is used to disable all visibility annotations inside libc++.
225
252
Defining this macro and then building libc++ with hidden visibility gives a
0 commit comments