Coverage Report

Created: 2025-11-11 06:46

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