/src/ndpi/fuzz/fuzz_alg_ranking.cpp
Line | Count | Source |
1 | | #include "ndpi_api.h" |
2 | | #include "fuzz_common_code.h" |
3 | | |
4 | | #include <unistd.h> |
5 | | #include <stdint.h> |
6 | | #include "fuzzer/FuzzedDataProvider.h" |
7 | | |
8 | 176 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
9 | 176 | FuzzedDataProvider fuzzed_data(data, size); |
10 | 176 | u_int16_t i, j; |
11 | 176 | ndpi_ranking rank; |
12 | 176 | u_int16_t max_num_entries, num_epochs; |
13 | 176 | u_int32_t now, prev_epoch; |
14 | 176 | ndpi_ranking_epoch_entry *entries; |
15 | 176 | ndpi_ranking_change *curr_ranking, *prev_ranking; |
16 | 176 | char path[64] = {0}; |
17 | | |
18 | | /* To allow memory allocation failures */ |
19 | 176 | fuzz_set_alloc_callbacks_and_seed(size); |
20 | | |
21 | 176 | max_num_entries = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
22 | 176 | num_epochs = fuzzed_data.ConsumeIntegralInRange(1, 255); |
23 | | |
24 | 176 | ndpi_init_ranking(&rank, max_num_entries, num_epochs); |
25 | | |
26 | 176 | now = 0; |
27 | | |
28 | | /* No ndpi_malloc; we don't want failures here */ |
29 | 176 | entries = (ndpi_ranking_epoch_entry *)malloc(sizeof(ndpi_ranking_epoch_entry) * max_num_entries); |
30 | 176 | curr_ranking = (ndpi_ranking_change *)malloc(sizeof(ndpi_ranking_change) * max_num_entries); |
31 | 176 | prev_ranking = (ndpi_ranking_change *)malloc(sizeof(ndpi_ranking_change) * max_num_entries); |
32 | | |
33 | 15.5k | for (j = 0; j < max_num_entries; j++) { |
34 | 2.65M | for(i = 0; i < max_num_entries; i++) { |
35 | 2.64M | entries[i].item_unique_id = i; |
36 | 2.64M | entries[i].value = fuzzed_data.ConsumeIntegral<u_int64_t>(); |
37 | 2.64M | } |
38 | | |
39 | 15.3k | ndpi_ranking_add_epoch(&rank, now, entries, max_num_entries, |
40 | 15.3k | curr_ranking, prev_ranking, |
41 | 15.3k | &prev_epoch); |
42 | 15.3k | now++; |
43 | 15.3k | } |
44 | | |
45 | 176 | ndpi_print_ranking(&rank); |
46 | | |
47 | 176 | snprintf(path, sizeof(path), "/tmp/ranking.%u.test", (unsigned int)getpid()); |
48 | 176 | if(ndpi_serialize_ranking(&rank, path)) { |
49 | 176 | ndpi_term_ranking(&rank); |
50 | 176 | ndpi_deserialize_ranking(&rank, path); |
51 | 176 | unlink(path); |
52 | 176 | } |
53 | | |
54 | 176 | ndpi_term_ranking(&rank); |
55 | | /* No ndpi_free! */ |
56 | 176 | free(entries); |
57 | 176 | free(curr_ranking); |
58 | 176 | free(prev_ranking); |
59 | | |
60 | 176 | return 0; |
61 | 176 | } |