aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_alg_bins.cpp
blob: 7bda2778b352d90603ba1ac76fb65406a8303a48 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "ndpi_api.h"
#include "fuzz_common_code.h"

#include <stdint.h>
#include <stdio.h>
#include "fuzzer/FuzzedDataProvider.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  FuzzedDataProvider fuzzed_data(data, size);
  u_int16_t j, i, num_iteration;
  struct ndpi_bin *b, *b_cloned, *bins, b2;
  u_int16_t num_bins, num_cluster_ids, num_element, num_allocated_bins, rc;
  enum ndpi_bin_family family;
  u_int16_t *cluster_ids;
  char buf[128];

  /* Just to have some data */
  if(fuzzed_data.remaining_bytes() < 2048)
    return -1;

  /* To allow memory allocation failures */
  fuzz_set_alloc_callbacks_and_seed(size);

  b = (struct ndpi_bin *)ndpi_malloc(sizeof(struct ndpi_bin));

  num_bins = fuzzed_data.ConsumeIntegral<u_int16_t>();
  family = fuzzed_data.ConsumeEnum<enum ndpi_bin_family>();

  ndpi_init_bin(b, family, num_bins);
  ndpi_init_bin(&b2, family, num_bins * 2);

  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
  for (i = 0; i < num_iteration; i++) {
    ndpi_inc_bin(b, fuzzed_data.ConsumeIntegral<u_int16_t>(),
                 fuzzed_data.ConsumeIntegral<u_int64_t>());
    ndpi_inc_bin(&b2, fuzzed_data.ConsumeIntegral<u_int16_t>(),
                 fuzzed_data.ConsumeIntegral<u_int64_t>());
  }

  ndpi_bin_similarity(b, &b2, fuzzed_data.ConsumeBool(),
                      fuzzed_data.ConsumeFloatingPointInRange<float>(0, 1));

  b_cloned = ndpi_clone_bin(b);

  ndpi_bin_similarity(b, b_cloned, fuzzed_data.ConsumeBool(),
                      fuzzed_data.ConsumeFloatingPointInRange<float>(0, 1));

  for (i = 0; i < num_iteration; i++)
    ndpi_get_bin_value(b, fuzzed_data.ConsumeIntegral<u_int16_t>());

  ndpi_reset_bin(b);

  for (i = 0; i < num_iteration; i++)
    ndpi_get_bin_value(b, fuzzed_data.ConsumeIntegral<u_int16_t>());

  for (i = 0; i < num_iteration; i++)
    ndpi_set_bin(b_cloned, fuzzed_data.ConsumeIntegral<u_int16_t>(),
                 fuzzed_data.ConsumeIntegral<u_int64_t>());

  ndpi_bin_similarity(b, b_cloned, fuzzed_data.ConsumeBool(),
                      fuzzed_data.ConsumeFloatingPointInRange<float>(0, 1));

  ndpi_normalize_bin(b);
  ndpi_normalize_bin(b_cloned);

  ndpi_print_bin(b, fuzzed_data.ConsumeBool(), buf, sizeof(buf));

  ndpi_free_bin(b);
  ndpi_free(b);
  ndpi_free_bin(&b2);
  ndpi_free_bin(b_cloned);
  ndpi_free(b_cloned);

  /* Cluster */

  num_bins = fuzzed_data.ConsumeIntegral<u_int8_t>();
  num_element = fuzzed_data.ConsumeIntegral<u_int8_t>();
  num_cluster_ids = fuzzed_data.ConsumeIntegral<u_int16_t>();
  bins = (struct ndpi_bin *)ndpi_malloc(sizeof(struct ndpi_bin) * num_bins);
  cluster_ids = (u_int16_t *)ndpi_malloc(sizeof(u_int16_t) * num_bins);

  num_allocated_bins = 0;
  if (bins && cluster_ids) {
    for (i = 0; i < num_bins; i++) {
      rc = ndpi_init_bin(&bins[num_allocated_bins], ndpi_bin_family64 /* Use 64 bit to avoid overlaps */,
                         num_element);
      if (rc != 0) {
        continue;
      }
      num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
      for (j = 0; j < num_iteration; j++) {
        ndpi_set_bin(&bins[num_allocated_bins],
                     fuzzed_data.ConsumeIntegralInRange(0, num_element + 1),
                     fuzzed_data.ConsumeIntegral<u_int64_t>());
      }
      num_allocated_bins++;
    }
    ndpi_cluster_bins(bins, num_allocated_bins, num_cluster_ids, cluster_ids, NULL);
  }

  ndpi_free(cluster_ids);
  if (bins)
    for (i = 0; i < num_allocated_bins; i++)
      ndpi_free_bin(&bins[i]);
  ndpi_free(bins);

  return 0;
}