Coverage Report

Created: 2025-11-20 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/fuzz/fuzz_common_code.c
Line
Count
Source
1
2
#include "fuzz_common_code.h"
3
4
5
static int mem_alloc_state = 0;
6
7
__attribute__((no_sanitize("integer")))
8
static int fastrand ()
9
2.67G
{
10
2.67G
  if(!mem_alloc_state) return 1; /* No failures */
11
1.54G
  mem_alloc_state = (214013 * mem_alloc_state + 2531011);
12
1.54G
  return (mem_alloc_state >> 16) & 0x7FFF;
13
2.67G
}
14
15
248M
static void *malloc_wrapper(size_t size) {
16
248M
  return (fastrand () % 16) ? malloc (size) : NULL;
17
248M
}
18
2.57G
static void free_wrapper(void *freeable) {
19
2.57G
  free(freeable);
20
2.57G
}
21
2.42G
static void *calloc_wrapper(size_t nmemb, size_t size) {
22
2.42G
  return (fastrand () % 16) ? calloc (nmemb, size) : NULL;
23
2.42G
}
24
977k
static void *realloc_wrapper(void *ptr, size_t size) {
25
977k
  return (fastrand () % 16) ? realloc (ptr, size) : NULL;
26
977k
}
27
28
void fuzz_set_alloc_callbacks(void)
29
80.4k
{
30
80.4k
  ndpi_set_memory_alloction_functions(malloc_wrapper,
31
80.4k
                                      free_wrapper,
32
80.4k
                                      calloc_wrapper,
33
80.4k
                                      realloc_wrapper,
34
                                      /* Aligned allocations are used only by croaring,
35
                                         but no during fuzzing. So no point to set
36
                                         these two wrappers here */
37
80.4k
                                      NULL, NULL,
38
80.4k
                                      malloc_wrapper,
39
80.4k
                                      free_wrapper);
40
80.4k
}
41
void fuzz_set_alloc_seed(int seed)
42
80.4k
{
43
80.4k
  mem_alloc_state = seed;
44
80.4k
}
45
void fuzz_set_alloc_callbacks_and_seed(int seed)
46
80.4k
{
47
80.4k
  fuzz_set_alloc_callbacks();
48
80.4k
  fuzz_set_alloc_seed(seed);
49
80.4k
}
50
51
void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_mod,
52
                                struct ndpi_global_context *g_ctx)
53
6
{
54
6
  if(*ndpi_info_mod == NULL) {
55
6
    *ndpi_info_mod = ndpi_init_detection_module(g_ctx);
56
57
6
    ndpi_set_config_u64(*ndpi_info_mod, NULL, "log.level", 3);
58
6
    ndpi_set_config(*ndpi_info_mod, "all", "log", "enable");
59
60
6
    ndpi_load_domain_suffixes(*ndpi_info_mod, "public_suffix_list.dat");
61
6
    ndpi_load_categories_dir(*ndpi_info_mod, "./lists/");
62
6
    ndpi_load_protocols_file(*ndpi_info_mod, "protos.txt");
63
6
    ndpi_load_categories_file(*ndpi_info_mod, "categories.txt", NULL);
64
6
    ndpi_load_risk_domain_file(*ndpi_info_mod, "risky_domains.txt");
65
6
    ndpi_load_malicious_ja4_file(*ndpi_info_mod, "ja4_fingerprints.csv");
66
6
    ndpi_load_malicious_sha1_file(*ndpi_info_mod, "sha1_fingerprints.csv");
67
68
6
    ndpi_set_config(*ndpi_info_mod, NULL, "filename.config", "config.txt");
69
70
6
    ndpi_finalize_initialization(*ndpi_info_mod);
71
6
  }
72
6
}
73
74
FILE *buffer_to_file(const uint8_t *data, size_t size)
75
223k
{
76
223k
  return fmemopen((void *)data, size, "rw");
77
223k
}