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