Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 1 | ========== |
| 2 | Debug Mode |
| 3 | ========== |
| 4 | |
| 5 | .. contents:: |
Eric Fiselier | 5392399 | 2017-02-05 01:16:25 +0000 | [diff] [blame] | 6 | :local: |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 7 | |
| 8 | .. _using-debug-mode: |
| 9 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 10 | Using the debug mode |
| 11 | ==================== |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 12 | |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 13 | Libc++ provides a debug mode that enables special debugging checks meant to detect |
| 14 | incorrect usage of the standard library. These checks are disabled by default, but |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 15 | they can be enabled by vendors when building the library by using ``LIBCXX_ENABLE_DEBUG_MODE``. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 16 | |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 17 | Since the debug mode has ABI implications, users should compile their whole program, |
| 18 | including any dependent libraries, against a Standard library configured identically |
| 19 | with respect to the debug mode. In other words, they should not mix code built against |
| 20 | a Standard library with the debug mode enabled with code built against a Standard library |
| 21 | where the debug mode is disabled. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 22 | |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 23 | Furthermore, users should not rely on a stable ABI being provided when the debug mode is |
| 24 | enabled -- we reserve the right to change the ABI at any time. If you need a stable ABI |
| 25 | and still want some level of hardening, you should look into enabling :ref:`assertions <assertions-mode>` |
| 26 | instead. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 27 | |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 28 | The debug mode provides various checks to aid application debugging. |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 29 | |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 30 | Comparator consistency checks |
| 31 | ----------------------------- |
Louis Dionne | 0656936 | 2022-03-07 11:31:45 -0500 | [diff] [blame] | 32 | Libc++ provides some checks for the consistency of comparators passed to algorithms. Specifically, |
| 33 | many algorithms such as ``binary_search``, ``merge``, ``next_permutation``, and ``sort``, wrap the |
| 34 | user-provided comparator to assert that `!comp(y, x)` whenever `comp(x, y)`. This can cause the |
| 35 | user-provided comparator to be evaluated up to twice as many times as it would be without the |
| 36 | debug mode, and causes the library to violate some of the Standard's complexity clauses. |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 37 | |
Louis Dionne | dd059fa | 2022-11-14 10:33:03 -1000 | [diff] [blame] | 38 | Iterator bounds checking |
| 39 | ------------------------ |
| 40 | The library provides iterators that ensure they are within the bounds of their container when dereferenced. |
| 41 | Arithmetic can be performed on these iterators to create out-of-bounds iterators, but they cannot be dereferenced |
| 42 | when out-of-bounds. The following classes currently provide iterators that have bounds checking: |
| 43 | |
| 44 | - ``std::string`` |
| 45 | - ``std::vector<T>`` (``T != bool``) |
| 46 | - ``std::span`` |
| 47 | |
| 48 | .. TODO: Add support for iterator bounds checking in ``std::string_view`` and ``std::array`` |
| 49 | |
| 50 | Iterator ownership checking |
| 51 | --------------------------- |
| 52 | The library provides iterator ownership checking, which allows catching cases where e.g. |
| 53 | an iterator from container ``X`` is used as a position to insert into container ``Y``. |
| 54 | The following classes support iterator ownership checking: |
Louis Dionne | 0a4b2b8 | 2021-06-08 16:32:53 -0400 | [diff] [blame] | 55 | |
| 56 | - ``std::string`` |
| 57 | - ``std::vector<T>`` (``T != bool``) |
| 58 | - ``std::list`` |
| 59 | - ``std::unordered_map`` |
| 60 | - ``std::unordered_multimap`` |
| 61 | - ``std::unordered_set`` |
| 62 | - ``std::unordered_multiset`` |
| 63 | |
Louis Dionne | 510450b | 2022-04-01 16:38:30 -0400 | [diff] [blame] | 64 | Randomizing unspecified behavior |
| 65 | -------------------------------- |
| 66 | The library supports the randomization of unspecified behavior. For example, randomizing |
| 67 | the relative order of equal elements in ``std::sort`` or randomizing both parts of the |
| 68 | partition after calling ``std::nth_element``. This effort helps migrating to potential |
| 69 | future faster versions of these algorithms that might not have the exact same behavior. |
| 70 | In particular, it makes it easier to deflake tests that depend on unspecified behavior. |
| 71 | A seed can be used to make such failures reproducible: use ``_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED=seed``. |