[libcxx-commits] [libcxx] [libc++][hardening] Enable comparator checks for safe mode too (PR #66458)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 14 20:25:10 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx
            
<details>
<summary>Changes</summary>
The comparator checks are currently only enabled in debug mode, but were previously part of the assertions mode, which has recently been added back as safe mode.

We would like to have these assertions enabled without enabling full debug mode, and I assume this now being in debug mode only was just an oversight in the hardening mode shuffling.

--
Full diff: https://github.com/llvm/llvm-project/pull/66458.diff

8 Files Affected:

- (modified) libcxx/include/__algorithm/comp_ref_type.h (+2-2) 
- (modified) libcxx/include/__algorithm/three_way_comp_ref_type.h (+2-2) 
- (modified) libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp (+1-1) 
- (modified) libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp (+2-1) 
- (modified) libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp (+1-1) 
- (modified) libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp (+1-1) 
- (modified) libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp (+1-1) 
- (modified) libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp (+1-1) 


<pre>
diff --git a/libcxx/include/__algorithm/comp_ref_type.h b/libcxx/include/__algorithm/comp_ref_type.h
index d16bd0f5310003f..3a422d65c58f0ba 100644
--- a/libcxx/include/__algorithm/comp_ref_type.h
+++ b/libcxx/include/__algorithm/comp_ref_type.h
@@ -63,9 +63,9 @@ struct __debug_less
     void __do_compare_assert(long, _LHS &amp;, _RHS &amp;) {}
 };
 
-// Pass the comparator by lvalue reference. Or in debug mode, using a
+// Pass the comparator by lvalue reference. Or in debug/safe mode, using a
 // debugging wrapper that stores a reference.
-#if _LIBCPP_ENABLE_DEBUG_MODE
+#if _LIBCPP_ENABLE_DEBUG_MODE || _LIBCPP_ENABLE_SAFE_MODE
 template &lt;class _Comp&gt;
 using __comp_ref_type = __debug_less&lt;_Comp&gt;;
 #else
diff --git a/libcxx/include/__algorithm/three_way_comp_ref_type.h b/libcxx/include/__algorithm/three_way_comp_ref_type.h
index 7731c0fd791d809..3df16e1515a36ae 100644
--- a/libcxx/include/__algorithm/three_way_comp_ref_type.h
+++ b/libcxx/include/__algorithm/three_way_comp_ref_type.h
@@ -56,9 +56,9 @@ struct __debug_three_way_comp {
   }
 };
 
-// Pass the comparator by lvalue reference. Or in debug mode, using a
+// Pass the comparator by lvalue reference. Or in debug/safe mode, using a
 // debugging wrapper that stores a reference.
-#  if _LIBCPP_ENABLE_DEBUG_MODE
+#  if _LIBCPP_ENABLE_DEBUG_MODE || _LIBCPP_ENABLE_SAFE_MODE
 template &lt;class _Comp&gt;
 using __three_way_comp_ref_type = __debug_three_way_comp&lt;_Comp&gt;;
 #  else
diff --git a/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
index a8032d032a4671b..f2b65d5f57cef73 100644
--- a/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/make.heap/complexity.pass.cpp
@@ -64,7 +64,7 @@ int main(int, char**)
   std::make_heap(v.begin(), v.end());
   assert(stats.copied == 0);
   assert(stats.moved == 153&#x27;486);
-#if !_LIBCPP_ENABLE_DEBUG_MODE
+#if !_LIBCPP_ENABLE_DEBUG_MODE &amp;&amp; !_LIBCPP_ENABLE_SAFE_MODE
   assert(stats.compared == 188&#x27;285);
 #endif
 
diff --git a/libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp b/libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp
index e5e417fe7bda2d4..03c3d4d07ec87ca 100644
--- a/libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/alg.sorting/assert.sort.invalid_comparator.pass.cpp
@@ -10,13 +10,14 @@
 
 // REQUIRES: has-unix-headers
 // UNSUPPORTED: c++03, c++11, c++14, c++17
-// UNSUPPORTED: !libcpp-hardening-mode=debug
+// UNSUPPORTED: !libcpp-hardening-mode=debug &amp;&amp; !libcpp-hardening-mode=safe
 // XFAIL: availability-verbose_abort-missing
 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG_STRICT_WEAK_ORDERING_CHECK
 // When the debug mode is enabled, this test fails because we actually catch on the fly that the comparator is not
 // a strict-weak ordering before we catch that we&#x27;d dereference out-of-bounds inside std::sort, which leads to different
 // errors than the ones tested below.
 // XFAIL: libcpp-hardening-mode=debug
+// XFAIL: libcpp-hardening-mode=safe
 
 // This test uses a specific combination of an invalid comparator and sequence of values to
 // ensure that our sorting functions do not go out-of-bounds and satisfy strict weak ordering in that case.
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
index 47b4f3bb19329f5..e8a8411a92ec8de 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
@@ -68,7 +68,7 @@ int main(int, char**) {
     std::sort_heap(first, last);
     LIBCPP_ASSERT(stats.copied == 0);
     LIBCPP_ASSERT(stats.moved &lt;= 2 * n + n * logn);
-#if !_LIBCPP_ENABLE_DEBUG_MODE
+#if !_LIBCPP_ENABLE_DEBUG_MODE &amp;&amp; !_LIBCPP_ENABLE_SAFE_MODE
     LIBCPP_ASSERT(stats.compared &lt;= n * logn);
     (void)debug_comparisons;
 #else
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp
index 5723ed0d3db25e4..c8ff4671b213de2 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp
@@ -262,7 +262,7 @@ void test_complexity() {
     std::ranges::sort_heap(first, last, &amp;MyInt::Comp);
     LIBCPP_ASSERT(stats.copied == 0);
     LIBCPP_ASSERT(stats.moved &lt;= 2 * n + n * logn);
-#if !_LIBCPP_ENABLE_DEBUG_MODE
+#if !_LIBCPP_ENABLE_DEBUG_MODE &amp;&amp; !_LIBCPP_ENABLE_SAFE_MODE
     LIBCPP_ASSERT(stats.compared &lt;= n * logn);
     (void)debug_comparisons;
 #else
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
index c82ad2623ee8b50..5be82e0ca0edd4f 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
@@ -79,7 +79,7 @@ test_one(unsigned N, unsigned M)
         assert(ia[0] == static_cast&lt;int&gt;(N)-1);
         assert(ia[N-1] == 0);
         assert(std::is_sorted(ia, ia+N, std::greater&lt;value_type&gt;()));
-#if !_LIBCPP_ENABLE_DEBUG_MODE
+#if !_LIBCPP_ENABLE_DEBUG_MODE &amp;&amp; !_LIBCPP_ENABLE_SAFE_MODE
         assert(pred.count() &lt;= (N-1));
 #endif
     }
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp
index dd413cfbaa2a92d..eb281c0899496dc 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp
@@ -156,7 +156,7 @@ constexpr void test_comparator_invocation_count() {
   // The comparator is invoked only `min(left.size(), right.size())` times
   test_lexicographical_compare&lt;const int*, const int*&gt;(
       std::array{0, 1, 2}, std::array{0, 1, 2, 3}, compare_last_digit_counting, std::strong_ordering::less);
-#if !_LIBCPP_ENABLE_DEBUG_MODE
+#if !_LIBCPP_ENABLE_DEBUG_MODE &amp;&amp; !_LIBCPP_ENABLE_SAFE_MODE
   assert(compare_invocation_count &lt;= 3);
 #else
   assert(compare_invocation_count &lt;= 6);
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66458


More information about the libcxx-commits mailing list