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) + ((uint8_t)c
<< 3);
50 switch((c
>> 1) & 7) {
72 #define MAX_SIZE (1024 * 1024)
74 int LLVMFuzzerTestOneInput(const uint8_t *input
, size_t len
)
76 const int8_t *buf
= (const int8_t *)input
;
77 int8_t buf2
[MAX_SIZE
];
81 if (len
< 3 || len
> MAX_SIZE
) {
84 context
= (int8_t)buf
[0];
88 memcpy(buf2
, buf
, len
);
89 stable_sort_r(buf2
, aux
, len
- 1, 1,
90 (samba_compare_with_context_fn_t
)cmp_int8
,
94 * We sorted all but the last element, which should remain unchanged.
95 * buf2[-1] should also be unchanged, but the sanitizers will catch
98 if (buf2
[len
- 1] != buf
[len
- 1]) {