Coverage Report

Created: 2025-11-11 06:45

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
69.0k
{
10
69.0k
  if(!mem_alloc_state) return 1; /* No failures */
11
69.0k
  mem_alloc_state = (214013 * mem_alloc_state + 2531011);
12
69.0k
  return (mem_alloc_state >> 16) & 0x7FFF;
13
69.0k
}
14
15
0
static void *malloc_wrapper(size_t size) {
16
0
  return (fastrand () % 16) ? malloc (size) : NULL;
17
0
}
18
5.20k
static void free_wrapper(void *freeable) {
19
5.20k
  free(freeable);
20
5.20k
}
21
5.23k
static void *calloc_wrapper(size_t nmemb, size_t size) {
22
5.23k
  return (fastrand () % 16) ? calloc (nmemb, size) : NULL;
23
5.23k
}
24
63.8k
static void *realloc_wrapper(void *ptr, size_t size) {
25
63.8k
  return (fastrand () % 16) ? realloc (ptr, size) : NULL;
26
63.8k
}
27
28
void fuzz_set_alloc_callbacks(void)
29
3.44k
{
30
3.44k
  ndpi_set_memory_alloction_functions(malloc_wrapper,
31
3.44k
                                      free_wrapper,
32
3.44k
                                      calloc_wrapper,
33
3.44k
                                      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
3.44k
                                      NULL, NULL,
38
                                      /* No interested in flow allocator, because it
39
                                         is used only by the application */
40
3.44k
                                      NULL, NULL);
41
3.44k
}
42
void fuzz_set_alloc_seed(int seed)
43
3.44k
{
44
3.44k
  mem_alloc_state = seed;
45
3.44k
}
46
void fuzz_set_alloc_callbacks_and_seed(int seed)
47
3.44k
{
48
3.44k
  fuzz_set_alloc_callbacks();
49
3.44k
  fuzz_set_alloc_seed(seed);
50
3.44k
}
51
52
void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_mod,
53
                                struct ndpi_global_context *g_ctx)
54
0
{
55
0
  if(*ndpi_info_mod == NULL) {
56
0
    *ndpi_info_mod = ndpi_init_detection_module(g_ctx);
57
58
0
    ndpi_set_config_u64(*ndpi_info_mod, NULL, "log.level", 3);
59
0
    ndpi_set_config(*ndpi_info_mod, "all", "log", "enable");
60
61
0
    ndpi_load_domain_suffixes(*ndpi_info_mod, "public_suffix_list.dat");
62
0
    ndpi_load_categories_dir(*ndpi_info_mod, "./lists/");
63
0
    ndpi_load_protocols_file(*ndpi_info_mod, "protos.txt");
64
0
    ndpi_load_categories_file(*ndpi_info_mod, "categories.txt", NULL);
65
0
    ndpi_load_risk_domain_file(*ndpi_info_mod, "risky_domains.txt");
66
0
    ndpi_load_malicious_ja4_file(*ndpi_info_mod, "ja4_fingerprints.csv");
67
0
    ndpi_load_malicious_sha1_file(*ndpi_info_mod, "sha1_fingerprints.csv");
68
69
0
    ndpi_set_config(*ndpi_info_mod, NULL, "filename.config", "config.txt");
70
71
0
    ndpi_finalize_initialization(*ndpi_info_mod);
72
0
  }
73
0
}
74
75
FILE *buffer_to_file(const uint8_t *data, size_t size)
76
0
{
77
0
  return fmemopen((void *)data, size, "rw");
78
0
}