aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_ds_libcache.cpp
blob: f4b30092f3957bc35020b39c87bffe6d03787bf4 (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
#include "ndpi_api.h"
#include "../src/lib/third_party/include/libcache.h"
#include "fuzz_common_code.h"

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

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  FuzzedDataProvider fuzzed_data(data, size);
  u_int16_t i, rc, num_iteration, data_len, is_added = 0;
  std::vector<u_int8_t>value_added;
  cache_t c;

  /* 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);

  c = cache_new(fuzzed_data.ConsumeIntegral<u_int8_t>());

  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
  for (i = 0; i < num_iteration; i++) {

    data_len = fuzzed_data.ConsumeIntegralInRange(0, 127);
    std::vector<u_int8_t>data = fuzzed_data.ConsumeBytes<u_int8_t>(data_len);

    rc = cache_add(c, data.data(), data.size());
    /* Keep one random entry really added */
    if (rc == CACHE_NO_ERROR && is_added == 0 && fuzzed_data.ConsumeBool()) {
      value_added = data;
      is_added = 1;
    }
  }

  /* "Random" search */
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
  for (i = 0; i < num_iteration; i++) {
    data_len = fuzzed_data.ConsumeIntegralInRange(0, 127);
    std::vector<u_int8_t>data = fuzzed_data.ConsumeBytes<u_int8_t>(data_len);

    cache_contains(c, data.data(), data.size());
  }
  /* Search of an added entry */
  if (is_added) {
    cache_contains(c, value_added.data(), value_added.size());
  }

  /* "Random" remove */
  num_iteration = fuzzed_data.ConsumeIntegral<u_int8_t>();
  for (i = 0; i < num_iteration; i++) {
    data_len = fuzzed_data.ConsumeIntegralInRange(0, 127);
    std::vector<u_int8_t>data = fuzzed_data.ConsumeBytes<u_int8_t>(data_len);

    cache_remove(c, data.data(), data.size());
  }
  /* Remove of an added entry */
  if (is_added) {
    cache_remove(c, value_added.data(), value_added.size());
  }

  cache_free(c);

  return 0;
}