2 Fuzzing for stable_sort
3 Copyright © Catalyst IT 2024
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "fuzzing/fuzzing.h"
21 #include "util/stable_sort.h"
24 int LLVMFuzzerInitialize(int *argc
, char ***argv
)
30 * This function tries to never be a proper comparison function,
31 * whatever the value of ctx.
33 * If ctx is an odd number, it will change on every comparison,
34 * otherwise it will consistently use the same bad comparison
37 static int cmp_int8(int8_t *_a
, int8_t *_b
, int8_t *ctx
)
44 /* aim for sustained chaos. */
47 c
^= (c
>> 5) + (c
<< 3);
50 switch((c
>> 1) & 7) {
72 #define MAX_SIZE (1024 * 1024)
74 int LLVMFuzzerTestOneInput(const uint8_t *buf
, size_t len
)
77 int8_t buf2
[MAX_SIZE
];
81 if (len
< 3 || len
> MAX_SIZE
) {
84 context
= (int8_t)buf
[0];
88 memcpy(buf2
, buf
, len
);
90 stable_sort_r(buf2
, aux
, len
, 1,
91 (samba_compare_with_context_fn_t
)cmp_int8
,