summaryrefslogtreecommitdiff
path: root/lib/fuzzing/fuzz_stable_sort.c
blob: 032a2a676fe8de3ab86b28bbc1e5349c5ad35945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
   Fuzzing for stable_sort
   Copyright © Catalyst IT

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "fuzzing/fuzzing.h"
#include "talloc.h"
#include "util/stable_sort.h"


int LLVMFuzzerInitialize(int *argc, char ***argv)
{
	return 0;
}


#define CMP_FN(type) static int cmp_ ## type (type *a, type *b) \
{\
	if (*a > *b) {\
		return 1;\
	}\
	if (*a < *b) {\
		return -1;\
	}\
	return 0;\
}

CMP_FN(uint8_t)
CMP_FN(uint16_t)
CMP_FN(uint32_t)
CMP_FN(uint64_t)

#define MAX_SIZE (1024 * 1024)

int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len)
{
	TALLOC_CTX *mem_ctx = NULL;
	samba_compare_fn_t fn;
	size_t s, i;
	uint8_t buf2[MAX_SIZE];

	if (len < 1 || len > MAX_SIZE) {
		return 0;
	}
	s = 1 << (buf[0] & 3);
	if (s == 1) {
		fn = (samba_compare_fn_t)cmp_uint8_t;
	} else if (s == 2) {
		fn = (samba_compare_fn_t)cmp_uint16_t;
	} else if (s == 4) {
		fn = (samba_compare_fn_t)cmp_uint32_t;
	} else {
		fn = (samba_compare_fn_t)cmp_uint64_t;
	}
	buf++;
	len--;
	len -= len & (s - 1);

	mem_ctx = talloc_new(NULL);
	memcpy(buf2, buf, len);

	stable_sort_talloc(mem_ctx, buf2, len / s, s, fn);

	talloc_free(mem_ctx);

	for (i = s; i < len; i += s) {
		int c = fn(&buf2[i - s], &buf2[i]);
		if (c > 0) {
			abort();
		}
	}

	return 0;
}