2 Fuzzing for stable_sort
3 Copyright © Catalyst IT
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"
22 #include "util/stable_sort.h"
25 int LLVMFuzzerInitialize(int *argc
, char ***argv
)
31 #define CMP_FN(type) static int cmp_ ## type (type *a, type *b) \
47 #define MAX_SIZE (1024 * 1024)
49 int LLVMFuzzerTestOneInput(const uint8_t *buf
, size_t len
)
51 TALLOC_CTX
*mem_ctx
= NULL
;
52 samba_compare_fn_t fn
;
54 uint8_t buf2
[MAX_SIZE
];
56 if (len
< 1 || len
> MAX_SIZE
) {
59 s
= 1 << (buf
[0] & 3);
61 fn
= (samba_compare_fn_t
)cmp_uint8_t
;
63 fn
= (samba_compare_fn_t
)cmp_uint16_t
;
65 fn
= (samba_compare_fn_t
)cmp_uint32_t
;
67 fn
= (samba_compare_fn_t
)cmp_uint64_t
;
73 mem_ctx
= talloc_new(NULL
);
74 memcpy(buf2
, buf
, len
);
76 stable_sort_talloc(mem_ctx
, buf2
, len
/ s
, s
, fn
);
80 for (i
= s
; i
< len
; i
+= s
) {
81 int c
= fn(&buf2
[i
- s
], &buf2
[i
]);