1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03, c++11 |
10 | |
11 | #include <algorithm> |
12 | #include <cstddef> |
13 | #include <cstdint> |
14 | #include <vector> |
15 | |
16 | #include "fuzz.h" |
17 | |
18 | // Use the first element as a position into the data |
19 | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { |
20 | if (size <= 1) |
21 | return 0; |
22 | const std::size_t sort_point = data[0] % size; |
23 | std::vector<std::uint8_t> working(data + 1, data + size); |
24 | const auto sort_iter = working.begin() + sort_point; |
25 | std::partial_sort(first: working.begin(), middle: sort_iter, last: working.end()); |
26 | |
27 | if (sort_iter != working.end()) { |
28 | const std::uint8_t nth = *std::min_element(first: sort_iter, last: working.end()); |
29 | if (!std::all_of(first: working.begin(), last: sort_iter, pred: [=](std::uint8_t v) { return v <= nth; })) |
30 | return 1; |
31 | if (!std::all_of(first: sort_iter, last: working.end(), pred: [=](std::uint8_t v) { return v >= nth; })) |
32 | return 2; |
33 | } |
34 | if (!std::is_sorted(first: working.begin(), last: sort_iter)) |
35 | return 3; |
36 | if (!fast_is_permutation(first1: data + 1, last1: data + size, first2: working.cbegin())) |
37 | return 99; |
38 | |
39 | return 0; |
40 | } |
41 | |