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 | extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) { |
19 | std::vector<ByteWithPayload> input; |
20 | for (std::size_t i = 0; i < size; ++i) |
21 | input.push_back(x: ByteWithPayload(data[i], i)); |
22 | |
23 | std::vector<ByteWithPayload> working = input; |
24 | std::stable_sort(first: working.begin(), last: working.end(), comp: ByteWithPayload::key_less()); |
25 | |
26 | if (!std::is_sorted(first: working.begin(), last: working.end(), comp: ByteWithPayload::key_less())) |
27 | return 1; |
28 | |
29 | auto iter = working.begin(); |
30 | while (iter != working.end()) { |
31 | auto range = std::equal_range(first: iter, last: working.end(), val: *iter, comp: ByteWithPayload::key_less()); |
32 | if (!std::is_sorted(first: range.first, last: range.second, comp: ByteWithPayload::total_less())) |
33 | return 2; |
34 | iter = range.second; |
35 | } |
36 | if (!fast_is_permutation(first1: input.cbegin(), last1: input.cend(), first2: working.cbegin())) |
37 | return 99; |
38 | return 0; |
39 | } |
40 | |