Skip to content

Commit 6c345a3

Browse files
committed
Move sort fuzz tests to new interface
1 parent 32e360c commit 6c345a3

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

libc/fuzzing/stdlib/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
add_libc_fuzzer(
2-
qsort_fuzz
2+
quick_sort_fuzz
33
SRCS
4-
qsort_fuzz.cpp
4+
quick_sort_fuzz.cpp
55
DEPENDS
6-
libc.src.stdlib.qsort
6+
libc.src.stdlib.qsort_util
77
)
88

99
add_libc_fuzzer(

libc/fuzzing/stdlib/heap_sort_fuzz.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,10 @@
1010
///
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "src/stdlib/heap_sort.h"
13+
#include "src/stdlib/qsort_util.h"
1414
#include <stdint.h>
1515

16-
static int int_compare(const void *l, const void *r) {
17-
int li = *reinterpret_cast<const int *>(l);
18-
int ri = *reinterpret_cast<const int *>(r);
19-
if (li == ri)
20-
return 0;
21-
if (li > ri)
22-
return 1;
23-
return -1;
24-
}
25-
2616
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
27-
2817
const size_t array_size = size / sizeof(int);
2918
if (array_size == 0)
3019
return 0;
@@ -34,14 +23,22 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
3423
for (size_t i = 0; i < array_size; ++i)
3524
array[i] = data_as_int[i];
3625

37-
auto arr = LIBC_NAMESPACE::internal::Array(
38-
reinterpret_cast<uint8_t *>(array), array_size, sizeof(int), int_compare);
26+
const auto is_less = [](const void *a_ptr,
27+
const void *b_ptr) noexcept -> bool {
28+
const int &a = *static_cast<const int *>(a_ptr);
29+
const int &b = *static_cast<const int *>(b_ptr);
30+
31+
return a < b;
32+
};
3933

40-
LIBC_NAMESPACE::internal::heap_sort(arr);
34+
constexpr bool USE_QUICKSORT = false;
35+
LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>(
36+
array, array_size, sizeof(int), is_less);
4137

42-
for (size_t i = 0; i < array_size - 1; ++i)
38+
for (size_t i = 0; i < array_size - 1; ++i) {
4339
if (array[i] > array[i + 1])
4440
__builtin_trap();
41+
}
4542

4643
delete[] array;
4744
return 0;

libc/fuzzing/stdlib/qsort_fuzz.cpp renamed to libc/fuzzing/stdlib/quick_sort_fuzz.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
//===-- qsort_fuzz.cpp ----------------------------------------------------===//
1+
//===-- quick_sort_fuzz.cpp------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
///
9-
/// Fuzzing test for llvm-libc qsort implementation.
9+
/// Fuzzing test for llvm-libc quick_sort implementation.
1010
///
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "src/stdlib/qsort.h"
13+
#include "src/stdlib/qsort_util.h"
1414
#include <stdint.h>
1515

16-
static int int_compare(const void *l, const void *r) {
17-
int li = *reinterpret_cast<const int *>(l);
18-
int ri = *reinterpret_cast<const int *>(r);
19-
if (li == ri)
20-
return 0;
21-
else if (li > ri)
22-
return 1;
23-
else
24-
return -1;
25-
}
26-
2716
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
2817
const size_t array_size = size / sizeof(int);
2918
if (array_size == 0)
@@ -34,7 +23,17 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
3423
for (size_t i = 0; i < array_size; ++i)
3524
array[i] = data_as_int[i];
3625

37-
LIBC_NAMESPACE::qsort(array, array_size, sizeof(int), int_compare);
26+
const auto is_less = [](const void *a_ptr,
27+
const void *b_ptr) noexcept -> bool {
28+
const int &a = *static_cast<const int *>(a_ptr);
29+
const int &b = *static_cast<const int *>(b_ptr);
30+
31+
return a < b;
32+
};
33+
34+
constexpr bool USE_QUICKSORT = true;
35+
LIBC_NAMESPACE::internal::unstable_sort_impl<USE_QUICKSORT>(
36+
array, array_size, sizeof(int), is_less);
3837

3938
for (size_t i = 0; i < array_size - 1; ++i) {
4039
if (array[i] > array[i + 1])

0 commit comments

Comments
 (0)