This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Add assertions for potential OOB reads in std::sort
ClosedPublic

Authored by ldionne on Mar 28 2023, 2:32 PM.

Details

Summary

We introduced an optimization to std::sort in 4eddbf9f10a6. However,
that optimization led to issues where users that were passing invalid
comparators to std::sort could start seeing OOB reads. This led to
the revert of the std::sort optimization from the LLVM 16 release
(see https://llvm.org/D146421).

This patch introduces _LIBCPP_ASSERTs to the places in the algorithm
where we make an assumption that the comparator will be consistent and
hence avoid a bounds check based on that. If the comparator happens not
to be consistent with itself, these are the places where we would
incorrectly go out of bounds. This allows users that enable libc++
assertions to catch such misuse at the cost of basically a bounds
check. For users that do not enable libc++ assertions (which is 99.9%
of users since assertions are off by default), this is basically a
no-op, and in fact the assertion will turn into a __builtin_assume,
making it explicit to the compiler that it can rely on the fact that
we're not going out of bounds.

I think this patch strikes the right balance. Folks that want absolute
performance will get what they want, since it is a precondition for the
comparator to be consistent, so the bounds checks are technically not
mandatory. Folks who want more safety *already* need to be enabling
libc++ assertions to catch other types of bugs (like operator[] OOB),
so this solution should also work for them.

I do think we have a lot of work towards popularizing the use of libc++
assertions and integrating it better so that users don't have to know
about the obscure _LIBCPP_ENABLE_ASSERTIONS macro to enable them, but
that's a separate concern.

rdar://106897934

Diff Detail

Event Timeline

ldionne created this revision.Mar 28 2023, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 2:32 PM
Herald added a subscriber: mgrang. · View Herald Transcript
ldionne requested review of this revision.Mar 28 2023, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 2:32 PM
Herald added a reviewer: Restricted Project. · View Herald Transcript
ldionne updated this revision to Diff 509143.Mar 28 2023, 2:33 PM

Reupload with parent relationship

ldionne updated this revision to Diff 509144.Mar 28 2023, 2:35 PM

Re-add release note that I removed from LLVM 16 when reverting the patch.

ldionne updated this revision to Diff 509352.Mar 29 2023, 6:53 AM

Remove tab from test file.

jloser added a subscriber: jloser.Mar 29 2023, 7:50 AM
jloser added inline comments.
libcxx/include/__algorithm/sort.h
298

I like the idea here, but can we spell out OOB here to be out of bounds? Given that this is a user facing message, I don't think we should be using the shorthand form. This applies below too in the other uses.

ldionne updated this revision to Diff 509452.Mar 29 2023, 12:54 PM
ldionne marked an inline comment as done.

Address comment and fix CI on GCC due to unused variable.

Thanks for working on this. Does it resolve https://github.com/llvm/llvm-project/issues/61663 ?

I like to see the CI green before approving, the issues seem to be introduced by this patch.

libcxx/include/__algorithm/sort.h
298

I think the current message looks weird.

libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp
29
44

Since we only do this in C++20 and later we could use a inline constexpr std::string_view in a header file.

EricWF accepted this revision as: EricWF.Apr 11 2023, 11:18 AM
EricWF added a subscriber: EricWF.

Have we done any benchmarking on this? I know the optimizer can be quite sensitive to the exact formulation of the loop.

Thanks for working on this. Does it resolve https://github.com/llvm/llvm-project/issues/61663 ?

It does not address that bug. That bug was about statically diagnosing that we don't have a strict-weak order when we can do that easily.

Have we done any benchmarking on this? I know the optimizer can be quite sensitive to the exact formulation of the loop.

Before the change:

Run on (20 X 24 MHz CPU s)
CPU Caches:
  L1 Data 64 KiB (x20)
  L1 Instruction 128 KiB (x20)
  L2 Unified 4096 KiB (x10)
Load Average: 4.65, 4.07, 4.41   (BEFORE CHANGE)
Load Average: 5.55, 5.82, 5.54   (AFTER CHANGE)
                                                                                        BEFORE                                           AFTER    
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                Time             CPU   Iterations                Time             CPU   Iterations   
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_Sort_uint32_Random_1                                               2.54 ns         2.54 ns    276824064             2.52 ns         2.52 ns    276561920
BM_Sort_uint32_Random_4                                              0.888 ns        0.888 ns    784596992            0.901 ns        0.901 ns    772014080
BM_Sort_uint32_Random_16                                              7.28 ns         7.28 ns     96206848             7.31 ns         7.31 ns     96206848
BM_Sort_uint32_Random_64                                              10.8 ns         10.8 ns     64487424             11.2 ns         11.2 ns     62652416
BM_Sort_uint32_Random_256                                             13.1 ns         13.1 ns     53477376             13.6 ns         13.6 ns     51642368
BM_Sort_uint32_Random_1024                                            14.7 ns         14.7 ns     47710208             15.3 ns         15.3 ns     45613056
BM_Sort_uint32_Random_16384                                           17.7 ns         17.7 ns     40108032             20.2 ns         19.3 ns     36962304
BM_Sort_uint32_Random_262144                                          20.6 ns         20.6 ns     34603008             21.0 ns         21.0 ns     33554432
BM_Sort_uint32_Ascending_1                                            2.53 ns         2.53 ns    277348352             2.53 ns         2.53 ns    276299776
BM_Sort_uint32_Ascending_4                                           0.892 ns        0.892 ns    786694144            0.902 ns        0.902 ns    776470528
BM_Sort_uint32_Ascending_16                                          0.510 ns        0.510 ns   1000079360            0.510 ns        0.510 ns   1000079360
BM_Sort_uint32_Ascending_64                                           1.05 ns         1.05 ns    667418624             1.06 ns         1.06 ns    661389312
BM_Sort_uint32_Ascending_256                                          1.12 ns         1.12 ns    618659840             1.11 ns         1.11 ns    624689152
BM_Sort_uint32_Ascending_1024                                        0.993 ns        0.993 ns    705429504            0.993 ns        0.993 ns    705691648
BM_Sort_uint32_Ascending_16384                                       0.948 ns        0.948 ns    744226816            0.941 ns        0.940 ns    741081088
BM_Sort_uint32_Ascending_262144                                      0.938 ns        0.938 ns    746586112            0.937 ns        0.937 ns    747896832
BM_Sort_uint32_Descending_1                                           2.53 ns         2.53 ns    277086208             2.53 ns         2.52 ns    277872640
BM_Sort_uint32_Descending_4                                          0.893 ns        0.893 ns    785645568            0.901 ns        0.901 ns    774897664
BM_Sort_uint32_Descending_16                                          3.48 ns         3.48 ns    200540160             3.45 ns         3.45 ns    203423744
BM_Sort_uint32_Descending_64                                          2.03 ns         2.03 ns    346292224             2.00 ns         2.00 ns    351010816
BM_Sort_uint32_Descending_256                                         1.85 ns         1.85 ns    378798080             1.85 ns         1.85 ns    380370944
BM_Sort_uint32_Descending_1024                                        1.87 ns         1.87 ns    374865920             1.86 ns         1.86 ns    376700928
BM_Sort_uint32_Descending_16384                                       1.73 ns         1.73 ns    405536768             1.72 ns         1.72 ns    405274624
BM_Sort_uint32_Descending_262144                                      1.72 ns         1.72 ns    406323200             1.73 ns         1.73 ns    407896064
BM_Sort_uint32_SingleElement_1                                        2.54 ns         2.54 ns    276824064             2.85 ns         2.85 ns    246153216
BM_Sort_uint32_SingleElement_4                                       0.892 ns        0.892 ns    781451264            0.905 ns        0.905 ns    775946240
BM_Sort_uint32_SingleElement_16                                      0.509 ns        0.510 ns   1000079360            0.530 ns        0.530 ns   1000079360
BM_Sort_uint32_SingleElement_64                                       1.27 ns         1.27 ns    554958848             1.30 ns         1.30 ns    537395200
BM_Sort_uint32_SingleElement_256                                      1.34 ns         1.34 ns    520880128             1.34 ns         1.34 ns    522452992
BM_Sort_uint32_SingleElement_1024                                     1.28 ns         1.27 ns    550764544             1.27 ns         1.27 ns    551288832
BM_Sort_uint32_SingleElement_16384                                    1.25 ns         1.25 ns    556531712             1.25 ns         1.25 ns    554696704
BM_Sort_uint32_SingleElement_262144                                   1.25 ns         1.25 ns    559939584             1.25 ns         1.25 ns    560726016
BM_Sort_uint32_PipeOrgan_1                                            2.54 ns         2.54 ns    274989056             2.52 ns         2.52 ns    277086208
BM_Sort_uint32_PipeOrgan_4                                           0.892 ns        0.892 ns    786694144            0.900 ns        0.900 ns    772800512
BM_Sort_uint32_PipeOrgan_16                                           1.45 ns         1.45 ns    477102080             1.40 ns         1.40 ns    498597888
BM_Sort_uint32_PipeOrgan_64                                           2.53 ns         2.53 ns    276561920             2.51 ns         2.51 ns    277872640
BM_Sort_uint32_PipeOrgan_256                                          2.01 ns         2.01 ns    347340800             2.00 ns         2.00 ns    348651520
BM_Sort_uint32_PipeOrgan_1024                                         3.45 ns         3.45 ns    202899456             3.55 ns         3.55 ns    197656576
BM_Sort_uint32_PipeOrgan_16384                                        4.29 ns         4.29 ns    163053568             4.36 ns         4.36 ns    160956416
BM_Sort_uint32_PipeOrgan_262144                                       4.92 ns         4.92 ns    142082048             5.01 ns         5.01 ns    139984896
BM_Sort_uint32_QuickSortAdversary_1                                   2.53 ns         2.53 ns    270794752             2.52 ns         2.52 ns    276299776
BM_Sort_uint32_QuickSortAdversary_4                                  0.890 ns        0.890 ns    786169856            0.902 ns        0.901 ns    770703360
BM_Sort_uint32_QuickSortAdversary_16                                 0.513 ns        0.513 ns   1000079360            0.511 ns        0.511 ns   1000079360
BM_Sort_uint32_QuickSortAdversary_64                                  11.0 ns         11.0 ns     63963136             11.2 ns         11.2 ns     62390272
BM_Sort_uint32_QuickSortAdversary_256                                 15.2 ns         15.2 ns     46137344             15.6 ns         15.6 ns     45350912
BM_Sort_uint32_QuickSortAdversary_1024                                22.0 ns         22.0 ns     31981568             21.7 ns         21.7 ns     32505856
BM_Sort_uint32_QuickSortAdversary_16384                               34.6 ns         34.6 ns     20447232             34.7 ns         34.7 ns     20447232
BM_Sort_uint32_QuickSortAdversary_262144                              48.0 ns         48.0 ns     14942208             47.5 ns         47.5 ns     14942208
BM_Sort_uint64_Random_1                                               2.53 ns         2.53 ns    274202624             2.53 ns         2.53 ns    278134784
BM_Sort_uint64_Random_4                                              0.929 ns        0.929 ns    757334016            0.944 ns        0.944 ns    749469696
BM_Sort_uint64_Random_16                                              7.28 ns         7.28 ns     96206848             7.27 ns         7.27 ns     96206848
BM_Sort_uint64_Random_64                                              10.8 ns         10.8 ns     65011712             11.2 ns         11.2 ns     62652416
BM_Sort_uint64_Random_256                                             13.0 ns         13.0 ns     54001664             13.5 ns         13.5 ns     52166656
BM_Sort_uint64_Random_1024                                            14.5 ns         14.5 ns     47972352             15.0 ns         15.0 ns     46923776
BM_Sort_uint64_Random_16384                                           17.1 ns         17.1 ns     41156608             17.5 ns         17.5 ns     40108032
BM_Sort_uint64_Random_262144                                          19.8 ns         19.8 ns     34865152             20.4 ns         20.4 ns     34340864
BM_Sort_uint64_Ascending_1                                            2.54 ns         2.54 ns    277086208             2.52 ns         2.52 ns    277348352
BM_Sort_uint64_Ascending_4                                           0.913 ns        0.913 ns    768081920            0.920 ns        0.919 ns    763101184
BM_Sort_uint64_Ascending_16                                          0.510 ns        0.510 ns   1000079360            0.512 ns        0.512 ns   1000079360
BM_Sort_uint64_Ascending_64                                           1.06 ns         1.06 ns    663486464             1.06 ns         1.06 ns    655622144
BM_Sort_uint64_Ascending_256                                          1.11 ns         1.11 ns    621543424             1.10 ns         1.10 ns    638058496
BM_Sort_uint64_Ascending_1024                                         1.01 ns         1.01 ns    700973056            0.998 ns        0.998 ns    701235200
BM_Sort_uint64_Ascending_16384                                       0.948 ns        0.948 ns    738983936            0.945 ns        0.945 ns    743702528
BM_Sort_uint64_Ascending_262144                                      0.939 ns        0.939 ns    746061824            0.937 ns        0.937 ns    747372544
BM_Sort_uint64_Descending_1                                           2.54 ns         2.54 ns    277086208             2.52 ns         2.52 ns    277348352
BM_Sort_uint64_Descending_4                                          0.921 ns        0.921 ns    766246912            0.931 ns        0.931 ns    738721792
BM_Sort_uint64_Descending_16                                          3.45 ns         3.45 ns    202899456             3.46 ns         3.46 ns    200802304
BM_Sort_uint64_Descending_64                                          2.04 ns         2.04 ns    341049344             2.07 ns         2.07 ns    338427904
BM_Sort_uint64_Descending_256                                         1.82 ns         1.82 ns    385089536             1.83 ns         1.83 ns    384303104
BM_Sort_uint64_Descending_1024                                        1.85 ns         1.85 ns    378798080             1.86 ns         1.86 ns    378535936
BM_Sort_uint64_Descending_16384                                       1.69 ns         1.69 ns    413925376             1.69 ns         1.69 ns    412090368
BM_Sort_uint64_Descending_262144                                      1.69 ns         1.69 ns    411041792             1.68 ns         1.68 ns    416022528
BM_Sort_uint64_SingleElement_1                                        2.53 ns         2.53 ns    276561920             2.53 ns         2.53 ns    276824064
BM_Sort_uint64_SingleElement_4                                       0.910 ns        0.910 ns    768081920            0.924 ns        0.924 ns    757596160
BM_Sort_uint64_SingleElement_16                                      0.510 ns        0.510 ns   1000079360            0.512 ns        0.512 ns   1000079360
BM_Sort_uint64_SingleElement_64                                       1.28 ns         1.28 ns    547094528             1.28 ns         1.28 ns    547618816
BM_Sort_uint64_SingleElement_256                                      1.35 ns         1.35 ns    520617984             1.34 ns         1.34 ns    521928704
BM_Sort_uint64_SingleElement_1024                                     1.28 ns         1.28 ns    549453824             1.86 ns         1.42 ns    541589504
BM_Sort_uint64_SingleElement_16384                                    1.25 ns         1.25 ns    559415296             1.25 ns         1.25 ns    556007424
BM_Sort_uint64_SingleElement_262144                                   1.25 ns         1.25 ns    560201728             1.25 ns         1.25 ns    560463872
BM_Sort_uint64_PipeOrgan_1                                            2.54 ns         2.54 ns    276299776             2.52 ns         2.52 ns    274726912
BM_Sort_uint64_PipeOrgan_4                                           0.911 ns        0.911 ns    760217600            0.929 ns        0.929 ns    746586112
BM_Sort_uint64_PipeOrgan_16                                           1.45 ns         1.45 ns    484179968             1.41 ns         1.41 ns    496500736
BM_Sort_uint64_PipeOrgan_64                                           2.53 ns         2.53 ns    276824064             2.51 ns         2.51 ns    278396928
BM_Sort_uint64_PipeOrgan_256                                          2.00 ns         2.00 ns    351535104             1.98 ns         1.98 ns    354418688
BM_Sort_uint64_PipeOrgan_1024                                         3.44 ns         3.44 ns    201326592             3.53 ns         3.53 ns    197132288
BM_Sort_uint64_PipeOrgan_16384                                        4.28 ns         4.28 ns    164364288             4.35 ns         4.35 ns    160694272
BM_Sort_uint64_PipeOrgan_262144                                       4.90 ns         4.90 ns    142606336             4.99 ns         4.99 ns    140509184
BM_Sort_uint64_QuickSortAdversary_1                                   2.53 ns         2.53 ns    276824064             2.53 ns         2.53 ns    275775488
BM_Sort_uint64_QuickSortAdversary_4                                  0.912 ns        0.912 ns    759955456            0.920 ns        0.920 ns    760479744
BM_Sort_uint64_QuickSortAdversary_16                                 0.511 ns        0.511 ns   1000079360            0.515 ns        0.515 ns   1000079360
BM_Sort_uint64_QuickSortAdversary_64                                  11.2 ns         11.2 ns     62652416             11.2 ns         11.2 ns     62652416
BM_Sort_uint64_QuickSortAdversary_256                                 15.2 ns         15.2 ns     46661632             15.4 ns         15.4 ns     45875200
BM_Sort_uint64_QuickSortAdversary_1024                                21.7 ns         21.7 ns     32505856             21.8 ns         21.8 ns     32243712
BM_Sort_uint64_QuickSortAdversary_16384                               34.6 ns         34.6 ns     20447232             35.0 ns         35.0 ns     20185088
BM_Sort_uint64_QuickSortAdversary_262144                              47.6 ns         47.6 ns     14942208             49.4 ns         49.4 ns     14417920
BM_Sort_pair<uint32, uint32>_Random_1                                 2.31 ns         2.31 ns    304873472             2.31 ns         2.30 ns    302252032
BM_Sort_pair<uint32, uint32>_Random_4                                 4.74 ns         4.74 ns    147587072             4.72 ns         4.72 ns    147587072
BM_Sort_pair<uint32, uint32>_Random_16                                10.4 ns         10.4 ns     67895296             10.2 ns         10.2 ns     69468160
BM_Sort_pair<uint32, uint32>_Random_64                                18.6 ns         18.6 ns     38010880             18.8 ns         18.8 ns     37224448
BM_Sort_pair<uint32, uint32>_Random_256                               25.7 ns         25.7 ns     27262976             26.0 ns         26.0 ns     27000832
BM_Sort_pair<uint32, uint32>_Random_1024                              32.8 ns         32.8 ns     21233664             33.3 ns         33.3 ns     21233664
BM_Sort_pair<uint32, uint32>_Random_16384                             46.9 ns         46.9 ns     15204352             47.1 ns         47.1 ns     14942208
BM_Sort_pair<uint32, uint32>_Random_262144                            59.2 ns         59.2 ns     12058624             61.1 ns         61.1 ns     11796480
BM_Sort_pair<uint32, uint32>_Ascending_1                              2.30 ns         2.30 ns    303562752             2.29 ns         2.29 ns    299892736
BM_Sort_pair<uint32, uint32>_Ascending_4                              1.12 ns         1.12 ns    624164864             1.12 ns         1.12 ns    628097024
BM_Sort_pair<uint32, uint32>_Ascending_16                            0.908 ns        0.908 ns    768081920            0.913 ns        0.913 ns    770965504
BM_Sort_pair<uint32, uint32>_Ascending_64                             1.30 ns         1.30 ns    538443776             1.30 ns         1.30 ns    538443776
BM_Sort_pair<uint32, uint32>_Ascending_256                            1.38 ns         1.38 ns    509870080             1.39 ns         1.39 ns    501219328
BM_Sort_pair<uint32, uint32>_Ascending_1024                           1.26 ns         1.26 ns    554958848             1.26 ns         1.26 ns    553385984
BM_Sort_pair<uint32, uint32>_Ascending_16384                          1.16 ns         1.16 ns    602144768             1.16 ns         1.16 ns    603455488
BM_Sort_pair<uint32, uint32>_Ascending_262144                         1.16 ns         1.16 ns    605290496             1.16 ns         1.16 ns    602931200
BM_Sort_pair<uint32, uint32>_Descending_1                             2.30 ns         2.30 ns    305135616             2.29 ns         2.29 ns    305659904
BM_Sort_pair<uint32, uint32>_Descending_4                             1.67 ns         1.67 ns    419692544             1.68 ns         1.68 ns    421789696
BM_Sort_pair<uint32, uint32>_Descending_16                            5.33 ns         5.33 ns    131596288             5.55 ns         5.55 ns    125566976
BM_Sort_pair<uint32, uint32>_Descending_64                            2.81 ns         2.81 ns    249036800             2.79 ns         2.79 ns    250871808
BM_Sort_pair<uint32, uint32>_Descending_256                           2.49 ns         2.49 ns    281804800             2.51 ns         2.51 ns    278659072
BM_Sort_pair<uint32, uint32>_Descending_1024                          2.39 ns         2.39 ns    292028416             2.38 ns         2.38 ns    293601280
BM_Sort_pair<uint32, uint32>_Descending_16384                         2.26 ns         2.26 ns    309592064             2.26 ns         2.26 ns    310116352
BM_Sort_pair<uint32, uint32>_Descending_262144                        2.27 ns         2.27 ns    308281344             2.29 ns         2.29 ns    308805632
BM_Sort_pair<uint32, uint32>_SingleElement_1                          2.30 ns         2.30 ns    305922048             2.29 ns         2.29 ns    305659904
BM_Sort_pair<uint32, uint32>_SingleElement_4                          1.21 ns         1.21 ns    575930368             1.21 ns         1.21 ns    580124672
BM_Sort_pair<uint32, uint32>_SingleElement_16                        0.925 ns        0.925 ns    755761152            0.930 ns        0.929 ns    757858304
BM_Sort_pair<uint32, uint32>_SingleElement_64                         1.79 ns         1.79 ns    390332416             1.79 ns         1.79 ns    389021696
BM_Sort_pair<uint32, uint32>_SingleElement_256                        1.86 ns         1.86 ns    382205952             1.84 ns         1.84 ns    382730240
BM_Sort_pair<uint32, uint32>_SingleElement_1024                       1.76 ns         1.76 ns    398983168             1.76 ns         1.76 ns    399769600
BM_Sort_pair<uint32, uint32>_SingleElement_16384                      1.70 ns         1.70 ns    410779648             1.72 ns         1.72 ns    409206784
BM_Sort_pair<uint32, uint32>_SingleElement_262144                     1.71 ns         1.71 ns    409206784             1.72 ns         1.72 ns    408158208
BM_Sort_pair<uint32, uint32>_PipeOrgan_1                              2.31 ns         2.31 ns    305135616             2.29 ns         2.29 ns    305659904
BM_Sort_pair<uint32, uint32>_PipeOrgan_4                              1.38 ns         1.38 ns    507510784             1.42 ns         1.42 ns    494403584
BM_Sort_pair<uint32, uint32>_PipeOrgan_16                             2.34 ns         2.34 ns    297795584             2.03 ns         2.03 ns    344457216
BM_Sort_pair<uint32, uint32>_PipeOrgan_64                             3.33 ns         3.33 ns    212074496             3.53 ns         3.53 ns    198705152
BM_Sort_pair<uint32, uint32>_PipeOrgan_256                            2.60 ns         2.60 ns    270794752             2.76 ns         2.76 ns    253231104
BM_Sort_pair<uint32, uint32>_PipeOrgan_1024                           4.82 ns         4.82 ns    145227776             4.95 ns         4.95 ns    141295616
BM_Sort_pair<uint32, uint32>_PipeOrgan_16384                          5.77 ns         5.77 ns    121634816             5.94 ns         5.94 ns    118751232
BM_Sort_pair<uint32, uint32>_PipeOrgan_262144                         6.54 ns         6.54 ns    106954752             6.68 ns         6.68 ns    104333312
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_1                     2.30 ns         2.30 ns    304349184             2.29 ns         2.29 ns    304349184
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_4                     1.34 ns         1.34 ns    524025856             1.35 ns         1.35 ns    511180800
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_16                    2.59 ns         2.59 ns    269221888             2.68 ns         2.68 ns    262406144
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_64                    6.19 ns         6.19 ns    112984064             6.34 ns         6.34 ns    110624768
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_256                   7.50 ns         7.50 ns     93323264             7.57 ns         7.57 ns     92798976
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_1024                  9.38 ns         9.38 ns     73138176             9.42 ns         9.42 ns     74448896
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_16384                 16.7 ns         16.7 ns     42729472             16.6 ns         16.6 ns     42205184
BM_Sort_pair<uint32, uint32>_QuickSortAdversary_262144                22.7 ns         22.7 ns     30670848             23.0 ns         23.0 ns     30670848
BM_Sort_tuple<uint32, uint64, uint32>_Random_1                        2.50 ns         2.50 ns    282066944             2.48 ns         2.48 ns    282329088
BM_Sort_tuple<uint32, uint64, uint32>_Random_4                        5.39 ns         5.39 ns    129761280             5.37 ns         5.37 ns    129761280
BM_Sort_tuple<uint32, uint64, uint32>_Random_16                       12.1 ns         12.1 ns     58195968             12.0 ns         12.0 ns     57933824
BM_Sort_tuple<uint32, uint64, uint32>_Random_64                       21.1 ns         21.1 ns     33030144             21.7 ns         21.7 ns     31981568
BM_Sort_tuple<uint32, uint64, uint32>_Random_256                      28.5 ns         28.5 ns     24641536             29.4 ns         29.4 ns     23855104
BM_Sort_tuple<uint32, uint64, uint32>_Random_1024                     35.6 ns         35.6 ns     19660800             36.5 ns         36.5 ns     19136512
BM_Sort_tuple<uint32, uint64, uint32>_Random_16384                    49.9 ns         49.9 ns     14155776             50.7 ns         50.7 ns     13893632
BM_Sort_tuple<uint32, uint64, uint32>_Random_262144                   63.0 ns         63.0 ns     11272192             66.0 ns         66.0 ns     11010048
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_1                     2.48 ns         2.48 ns    282066944             2.48 ns         2.48 ns    282329088
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_4                     1.36 ns         1.35 ns    520093696             1.37 ns         1.37 ns    517734400
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_16                    1.32 ns         1.32 ns    522715136             1.32 ns         1.32 ns    527433728
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_64                    1.68 ns         1.68 ns    419430400             1.73 ns         1.72 ns    405798912
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_256                   1.82 ns         1.82 ns    386138112             1.88 ns         1.88 ns    373030912
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_1024                  1.69 ns         1.69 ns    411303936             1.72 ns         1.72 ns    405536768
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_16384                 1.60 ns         1.60 ns    440401920             1.58 ns         1.58 ns    443809792
BM_Sort_tuple<uint32, uint64, uint32>_Ascending_262144                1.59 ns         1.59 ns    444596224             1.60 ns         1.60 ns    442761216
BM_Sort_tuple<uint32, uint64, uint32>_Descending_1                    2.48 ns         2.48 ns    281804800             2.49 ns         2.49 ns    276824064
BM_Sort_tuple<uint32, uint64, uint32>_Descending_4                    2.13 ns         2.12 ns    331087872             2.15 ns         2.15 ns    331612160
BM_Sort_tuple<uint32, uint64, uint32>_Descending_16                   7.23 ns         7.23 ns     96993280             7.23 ns         7.23 ns     96206848
BM_Sort_tuple<uint32, uint64, uint32>_Descending_64                   3.15 ns         3.15 ns    221249536             3.46 ns         3.45 ns    200540160
BM_Sort_tuple<uint32, uint64, uint32>_Descending_256                  2.78 ns         2.78 ns    251920384             2.91 ns         2.91 ns    240123904
BM_Sort_tuple<uint32, uint64, uint32>_Descending_1024                 2.63 ns         2.63 ns    267911168             2.73 ns         2.73 ns    255328256
BM_Sort_tuple<uint32, uint64, uint32>_Descending_16384                2.54 ns         2.54 ns    276824064             2.64 ns         2.64 ns    265551872
BM_Sort_tuple<uint32, uint64, uint32>_Descending_262144               2.55 ns         2.55 ns    272367616             2.65 ns         2.65 ns    263979008
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_1                 2.50 ns         2.50 ns    281542656             2.48 ns         2.48 ns    281542656
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_4                 1.56 ns         1.56 ns    443023360             1.57 ns         1.57 ns    445382656
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_16                1.27 ns         1.27 ns    551026688             1.27 ns         1.27 ns    550240256
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_64                2.45 ns         2.45 ns    285212672             2.46 ns         2.46 ns    283639808
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_256               2.64 ns         2.64 ns    264765440             2.67 ns         2.67 ns    264765440
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_1024              2.45 ns         2.45 ns    284950528             2.44 ns         2.44 ns    287047680
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_16384             2.38 ns         2.38 ns    293863424             2.40 ns         2.40 ns    296747008
BM_Sort_tuple<uint32, uint64, uint32>_SingleElement_262144            2.39 ns         2.39 ns    293863424             2.43 ns         2.43 ns    284164096
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_1                     2.48 ns         2.48 ns    282329088             2.48 ns         2.48 ns    279707648
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_4                     1.96 ns         1.96 ns    358612992             1.86 ns         1.86 ns    379322368
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_16                    2.86 ns         2.86 ns    243793920             2.87 ns         2.87 ns    242483200
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_64                    3.83 ns         3.83 ns    183762944             3.98 ns         3.98 ns    175112192
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_256                   3.04 ns         3.04 ns    230162432             3.20 ns         3.20 ns    219938816
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_1024                  5.63 ns         5.63 ns    124518400             5.87 ns         5.87 ns    120061952
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_16384                 6.77 ns         6.77 ns    103809024             6.97 ns         6.97 ns    100663296
BM_Sort_tuple<uint32, uint64, uint32>_PipeOrgan_262144                7.79 ns         7.79 ns     89915392             7.97 ns         7.97 ns     87031808
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_1            2.56 ns         2.55 ns    266338304             2.50 ns         2.50 ns    282066944
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_4            2.09 ns         2.09 ns    336855040             2.04 ns         1.98 ns    355729408
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_16           3.88 ns         3.88 ns    180355072             3.79 ns         3.79 ns    177733632
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_64           7.12 ns         7.12 ns     95682560             7.98 ns         7.98 ns     86769664
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_256          8.81 ns         8.81 ns     79691776             9.48 ns         9.48 ns     73400320
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_1024         10.3 ns         10.3 ns     68419584             11.1 ns         11.1 ns     63438848
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_16384        27.9 ns         27.9 ns     25165824             28.8 ns         28.8 ns     24379392
BM_Sort_tuple<uint32, uint64, uint32>_QuickSortAdversary_262144       35.9 ns         35.9 ns     19660800             38.0 ns         37.9 ns     18612224
BM_Sort_string_Random_1                                               2.49 ns         2.49 ns    282329088             2.50 ns         2.50 ns    282591232
BM_Sort_string_Random_4                                               10.9 ns         10.9 ns     64487424             11.0 ns         11.0 ns     63438848
BM_Sort_string_Random_16                                              23.8 ns         23.8 ns     29622272             25.3 ns         25.2 ns     27525120
BM_Sort_string_Random_64                                              41.2 ns         41.2 ns     17039360             43.2 ns         43.1 ns     15728640
BM_Sort_string_Random_256                                             58.7 ns         58.7 ns     12058624             59.0 ns         59.0 ns     12058624
BM_Sort_string_Random_1024                                            74.6 ns         74.6 ns      9437184             75.2 ns         75.2 ns      9175040
BM_Sort_string_Random_16384                                            108 ns          108 ns      6553600              110 ns          110 ns      6553600
BM_Sort_string_Random_262144                                           159 ns          159 ns      4456448              170 ns          170 ns      4456448
BM_Sort_string_Ascending_1                                            2.49 ns         2.49 ns    282066944             2.52 ns         2.52 ns    266338304
BM_Sort_string_Ascending_4                                            2.85 ns         2.85 ns    241958912             3.17 ns         3.17 ns    226754560
BM_Sort_string_Ascending_16                                           2.93 ns         2.93 ns    241696768             3.17 ns         3.17 ns    217579520
BM_Sort_string_Ascending_64                                           6.04 ns         6.04 ns    116391936             6.29 ns         6.29 ns    109838336
BM_Sort_string_Ascending_256                                          6.88 ns         6.88 ns    102236160             6.87 ns         6.87 ns    101711872
BM_Sort_string_Ascending_1024                                         6.82 ns         6.82 ns    103022592             6.87 ns         6.87 ns    102236160
BM_Sort_string_Ascending_16384                                        7.02 ns         7.02 ns     99614720             7.26 ns         7.26 ns     95944704
BM_Sort_string_Ascending_262144                                       13.7 ns         13.7 ns     51118080             13.8 ns         13.8 ns     51118080
BM_Sort_string_Descending_1                                           2.49 ns         2.49 ns    281542656             2.51 ns         2.50 ns    280756224
BM_Sort_string_Descending_4                                           3.98 ns         3.98 ns    174587904             4.26 ns         4.26 ns    164888576
BM_Sort_string_Descending_16                                          20.2 ns         20.2 ns     34603008             20.6 ns         20.6 ns     34340864
BM_Sort_string_Descending_64                                          8.45 ns         8.45 ns     83623936             8.78 ns         8.77 ns     78118912
BM_Sort_string_Descending_256                                         9.91 ns         9.91 ns     70778880             9.96 ns         9.96 ns     70516736
BM_Sort_string_Descending_1024                                        9.76 ns         9.76 ns     71827456             9.84 ns         9.83 ns     71565312
BM_Sort_string_Descending_16384                                       9.84 ns         9.84 ns     71041024             9.99 ns         9.99 ns     69468160
BM_Sort_string_Descending_262144                                      21.4 ns         21.4 ns     32768000             22.4 ns         22.4 ns     32768000
BM_Sort_string_SingleElement_1                                        2.48 ns         2.48 ns    281542656             2.49 ns         2.49 ns    276824064
BM_Sort_string_SingleElement_4                                        4.10 ns         4.10 ns    171442176             4.02 ns         4.02 ns    176160768
BM_Sort_string_SingleElement_16                                       4.30 ns         4.30 ns    163053568             4.47 ns         4.47 ns    151519232
BM_Sort_string_SingleElement_64                                       8.75 ns         8.75 ns     79953920             8.91 ns         8.91 ns     78381056
BM_Sort_string_SingleElement_256                                      8.19 ns         8.19 ns     83886080             8.19 ns         8.19 ns     85196800
BM_Sort_string_SingleElement_1024                                     7.52 ns         7.52 ns     93585408             7.70 ns         7.70 ns     90963968
BM_Sort_string_SingleElement_16384                                    7.25 ns         7.25 ns     96731136             7.46 ns         7.46 ns     93847552
BM_Sort_string_SingleElement_262144                                   7.41 ns         7.41 ns     93061120             7.63 ns         7.63 ns     91750400
BM_Sort_string_PipeOrgan_1                                            2.49 ns         2.49 ns    281804800             2.49 ns         2.49 ns    281542656
BM_Sort_string_PipeOrgan_4                                            3.31 ns         3.31 ns    209715200             3.65 ns         3.65 ns    195821568
BM_Sort_string_PipeOrgan_16                                           6.99 ns         6.99 ns    100401152             7.20 ns         7.20 ns     97517568
BM_Sort_string_PipeOrgan_64                                           12.4 ns         12.4 ns     56885248             12.7 ns         12.7 ns     55312384
BM_Sort_string_PipeOrgan_256                                          11.3 ns         11.3 ns     62914560             11.2 ns         11.2 ns     62914560
BM_Sort_string_PipeOrgan_1024                                         19.0 ns         19.0 ns     36962304             19.0 ns         19.0 ns     36700160
BM_Sort_string_PipeOrgan_16384                                        23.7 ns         23.7 ns     29622272             23.8 ns         23.8 ns     29360128
BM_Sort_string_PipeOrgan_262144                                       38.8 ns         38.8 ns     18350080             39.8 ns         39.8 ns     17039360
BM_Sort_string_QuickSortAdversary_1                                   2.49 ns         2.49 ns    281804800             2.49 ns         2.49 ns    281804800
BM_Sort_string_QuickSortAdversary_4                                   10.9 ns         10.9 ns     63963136             11.0 ns         11.0 ns     62652416
BM_Sort_string_QuickSortAdversary_16                                  23.6 ns         23.6 ns     29884416             23.9 ns         23.9 ns     29622272
BM_Sort_string_QuickSortAdversary_64                                  41.3 ns         41.3 ns     17039360             42.0 ns         42.0 ns     16777216
BM_Sort_string_QuickSortAdversary_256                                 58.8 ns         58.8 ns     12058624             59.7 ns         59.7 ns     12058624
BM_Sort_string_QuickSortAdversary_1024                                74.3 ns         74.3 ns      9699328             76.7 ns         76.7 ns      9437184
BM_Sort_string_QuickSortAdversary_16384                                108 ns          108 ns      6553600              109 ns          109 ns      6553600
BM_Sort_string_QuickSortAdversary_262144                               159 ns          159 ns      4456448              160 ns          160 ns      4456448
BM_Sort_float_Random_1                                                2.86 ns         2.86 ns    245104640             2.54 ns         2.54 ns    276561920
BM_Sort_float_Random_4                                               0.951 ns        0.950 ns    738983936            0.924 ns        0.924 ns    752615424
BM_Sort_float_Random_16                                               9.04 ns         9.04 ns     77856768             8.82 ns         8.82 ns     79429632
BM_Sort_float_Random_64                                               13.4 ns         13.4 ns     52953088             13.1 ns         13.1 ns     53477376
BM_Sort_float_Random_256                                              16.0 ns         16.0 ns     44040192             15.7 ns         15.7 ns     44826624
BM_Sort_float_Random_1024                                             17.7 ns         17.7 ns     39583744             17.4 ns         17.4 ns     39845888
BM_Sort_float_Random_16384                                            20.5 ns         20.5 ns     33816576             20.3 ns         20.3 ns     34603008
BM_Sort_float_Random_262144                                           23.4 ns         23.4 ns     30146560             23.3 ns         23.3 ns     30146560
BM_Sort_float_Ascending_1                                             2.93 ns         2.93 ns    239599616             2.54 ns         2.54 ns    273940480
BM_Sort_float_Ascending_4                                            0.956 ns        0.956 ns    730071040            0.924 ns        0.924 ns    753401856
BM_Sort_float_Ascending_16                                           0.517 ns        0.517 ns   1000079360            0.511 ns        0.511 ns   1000079360
BM_Sort_float_Ascending_64                                            1.05 ns         1.05 ns    666632192             1.06 ns         1.06 ns    660078592
BM_Sort_float_Ascending_256                                           1.20 ns         1.20 ns    605814784             1.18 ns         1.18 ns    609484800
BM_Sort_float_Ascending_1024                                          1.03 ns         1.03 ns    678952960             1.04 ns         1.04 ns    676593664
BM_Sort_float_Ascending_16384                                        0.939 ns        0.939 ns    744751104            0.945 ns        0.945 ns    732168192
BM_Sort_float_Ascending_262144                                       0.935 ns        0.935 ns    748945408            0.942 ns        0.942 ns    746848256
BM_Sort_float_Descending_1                                            2.87 ns         2.87 ns    244842496             2.55 ns         2.55 ns    273678336
BM_Sort_float_Descending_4                                           0.923 ns        0.923 ns    758906880            0.931 ns        0.931 ns    759955456
BM_Sort_float_Descending_16                                           3.50 ns         3.50 ns    199491584             3.52 ns         3.52 ns    198180864
BM_Sort_float_Descending_64                                           1.97 ns         1.97 ns    355205120             2.03 ns         2.03 ns    341835776
BM_Sort_float_Descending_256                                          1.87 ns         1.87 ns    376963072             1.88 ns         1.88 ns    370409472
BM_Sort_float_Descending_1024                                         1.89 ns         1.89 ns    370409472             1.90 ns         1.90 ns    367001600
BM_Sort_float_Descending_16384                                        1.75 ns         1.75 ns    400556032             1.75 ns         1.75 ns    399769600
BM_Sort_float_Descending_262144                                       1.74 ns         1.74 ns    403701760             1.74 ns         1.74 ns    403439616
BM_Sort_float_SingleElement_1                                         2.88 ns         2.88 ns    244580352             2.54 ns         2.54 ns    276561920
BM_Sort_float_SingleElement_4                                        0.973 ns        0.970 ns    722993152            0.924 ns        0.924 ns    759169024
BM_Sort_float_SingleElement_16                                       0.536 ns        0.535 ns   1000079360            0.514 ns        0.514 ns   1000079360
BM_Sort_float_SingleElement_64                                        1.30 ns         1.30 ns    541327360             1.24 ns         1.24 ns    563347456
BM_Sort_float_SingleElement_256                                       1.44 ns         1.43 ns    487063552             1.37 ns         1.37 ns    509345792
BM_Sort_float_SingleElement_1024                                      1.36 ns         1.35 ns    522190848             1.31 ns         1.31 ns    534249472
BM_Sort_float_SingleElement_16384                                     1.34 ns         1.34 ns    522977280             1.29 ns         1.29 ns    542900224
BM_Sort_float_SingleElement_262144                                    1.34 ns         1.34 ns    522452992             1.29 ns         1.29 ns    536346624
BM_Sort_float_PipeOrgan_1                                             2.95 ns         2.94 ns    237240320             2.54 ns         2.54 ns    276824064
BM_Sort_float_PipeOrgan_4                                            0.957 ns        0.956 ns    729022464            0.926 ns        0.926 ns    761266176
BM_Sort_float_PipeOrgan_16                                            1.48 ns         1.47 ns    473432064             1.41 ns         1.41 ns    497811456
BM_Sort_float_PipeOrgan_64                                            2.67 ns         2.66 ns    267386880             2.53 ns         2.53 ns    275775488
BM_Sort_float_PipeOrgan_256                                           2.13 ns         2.13 ns    326369280             2.06 ns         2.06 ns    340262912
BM_Sort_float_PipeOrgan_1024                                          3.72 ns         3.71 ns    188743680             3.65 ns         3.65 ns    191102976
BM_Sort_float_PipeOrgan_16384                                         4.61 ns         4.61 ns    152043520             4.52 ns         4.52 ns    155451392
BM_Sort_float_PipeOrgan_262144                                        5.27 ns         5.27 ns    133431296             5.14 ns         5.14 ns    136314880
BM_Sort_float_QuickSortAdversary_1                                    2.95 ns         2.95 ns    236978176             2.52 ns         2.52 ns    276824064
BM_Sort_float_QuickSortAdversary_4                                   0.958 ns        0.958 ns    729808896            0.924 ns        0.924 ns    759955456
BM_Sort_float_QuickSortAdversary_16                                  0.529 ns        0.529 ns   1000079360            0.511 ns        0.511 ns   1000079360
BM_Sort_float_QuickSortAdversary_64                                   9.59 ns         9.58 ns     72876032             9.30 ns         9.30 ns     75497472
BM_Sort_float_QuickSortAdversary_256                                  14.8 ns         14.7 ns     48234496             14.1 ns         14.1 ns     49545216
BM_Sort_float_QuickSortAdversary_1024                                 19.5 ns         19.4 ns     36175872             18.9 ns         18.9 ns     38010880
BM_Sort_float_QuickSortAdversary_16384                                68.6 ns         68.6 ns     10223616             64.7 ns         64.7 ns     11010048
BM_Sort_float_QuickSortAdversary_262144                               69.5 ns         69.4 ns     10223616             65.9 ns         65.9 ns     10747904

I don't see anything concerning given the load-average difference on the machine.

ldionne updated this revision to Diff 519658.May 4 2023, 2:26 PM

Rebase and fix issues where we'd check against the incorrect bounds.

ldionne updated this revision to Diff 519821.May 5 2023, 5:43 AM

Fix unused variable warning

ldionne updated this revision to Diff 520336.May 8 2023, 5:53 AM

Fix tests

ldionne updated this revision to Diff 520378.May 8 2023, 8:01 AM

Revert incorrect bounds check change in last diff.

ldionne accepted this revision.May 9 2023, 6:05 AM

Merging based on previous reviews.

This revision is now accepted and ready to land.May 9 2023, 6:05 AM
alexfh added a subscriber: alexfh.May 15 2023, 3:50 PM

Hi Louis, we've started seeing compilation errors in code using custom iterators after this patch:

.../include/c++/v1/__algorithm/sort.h:681:14 no viable overloaded '='
    *__begin = _Ops::__iter_move(__pivot_pos);

One example that relies on an open-source library is here https://gcc.godbolt.org/z/be7xMhcn3, but we have other instances as well. Is this a intended effect of this patch? If so, can we have an escape hatch?

FWIW, the error doesn't depend on whether _LIBCPP_ENABLE_ASSERTIONS is enabled.

bgraur added a subscriber: bgraur.May 16 2023, 1:52 AM

Hi Louis, we've started seeing compilation errors in code using custom iterators after this patch:

.../include/c++/v1/__algorithm/sort.h:681:14 no viable overloaded '='
    *__begin = _Ops::__iter_move(__pivot_pos);

One example that relies on an open-source library is here https://gcc.godbolt.org/z/be7xMhcn3, but we have other instances as well. Is this a intended effect of this patch? If so, can we have an escape hatch?

FWIW, the error doesn't depend on whether _LIBCPP_ENABLE_ASSERTIONS is enabled.

Thanks for reducing to a godbolt! This happens because __begin was made const in the new code and we're assigning to the result of *__begin. This is required to be valid since operator* on iterators is supposed to be const (see https://en.cppreference.com/w/cpp/iterator/indirectly_readable). So the code that fails should be fixed.

That being said, I can make __begin non-const for the time being to give you time to fix up these places, but I'll mark it with a TODO to make const again in LLVM 18. How does that sound?

ldionne marked 3 inline comments as done.May 17 2023, 7:16 AM

See https://reviews.llvm.org/D150779 for the temporary workaround.

See https://reviews.llvm.org/D150779 for the temporary workaround.

@ldionne thanks for the workaround! However, it doesn't help even with the original reproducer in godbolt (https://gcc.godbolt.org/z/be7xMhcn3). We've done some progress cleaning up non-conforming iterators (mostly adding const to dereference and comparison operators), but I'd still find it useful to have a working workaround for this in case we're stuck with some of the fixes. I guess, other libc++ users would also appreciate a more controllable way to enable enforcement of iterator requirements.

See https://reviews.llvm.org/D150779 for the temporary workaround.

@ldionne thanks for the workaround! However, it doesn't help even with the original reproducer in godbolt (https://gcc.godbolt.org/z/be7xMhcn3). We've done some progress cleaning up non-conforming iterators (mostly adding const to dereference and comparison operators), but I'd still find it useful to have a working workaround for this in case we're stuck with some of the fixes. I guess, other libc++ users would also appreciate a more controllable way to enable enforcement of iterator requirements.

Ideally, it would be nice to put breaking changes in this area under a config option that could be enabled in order to get an early warning or prevent backsliding.

libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp