Coverage Report

Created: 2026-04-04 07:03

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
#include <assert.h>
5
6
7
static int mem_alloc_state = 0;
8
9
__attribute__((no_sanitize("integer")))
10
static int fastrand ()
11
0
{
12
0
  if(!mem_alloc_state) return 1; /* No failures */
13
0
  mem_alloc_state = (214013 * mem_alloc_state + 2531011);
14
0
  return (mem_alloc_state >> 16) & 0x7FFF;
15
0
}
16
17
0
static void *malloc_wrapper(size_t size) {
18
0
  return (fastrand () % 16) ? malloc (size) : NULL;
19
0
}
20
0
static void free_wrapper(void *freeable) {
21
0
  free(freeable);
22
0
}
23
0
static void *calloc_wrapper(size_t nmemb, size_t size) {
24
0
  return (fastrand () % 16) ? calloc (nmemb, size) : NULL;
25
0
}
26
0
static void *realloc_wrapper(void *ptr, size_t size) {
27
0
  return (fastrand () % 16) ? realloc (ptr, size) : NULL;
28
0
}
29
30
void fuzz_set_alloc_callbacks(void)
31
0
{
32
0
  ndpi_set_memory_alloction_functions(malloc_wrapper,
33
0
                                      free_wrapper,
34
0
                                      calloc_wrapper,
35
0
                                      realloc_wrapper,
36
                                      /* Aligned allocations are used only by croaring,
37
                                         but no during fuzzing. So no point to set
38
                                         these two wrappers here */
39
0
                                      NULL, NULL,
40
0
                                      malloc_wrapper,
41
0
                                      free_wrapper);
42
0
}
43
void fuzz_set_alloc_seed(int seed)
44
0
{
45
0
  mem_alloc_state = seed;
46
0
}
47
void fuzz_set_alloc_callbacks_and_seed(int seed)
48
0
{
49
0
  fuzz_set_alloc_callbacks();
50
0
  fuzz_set_alloc_seed(seed);
51
0
}
52
53
void fuzz_init_detection_module(struct ndpi_detection_module_struct **ndpi_info_mod,
54
                                struct ndpi_global_context *g_ctx,
55
                                const char *path)
56
1
{
57
1
  char name[256];
58
59
1
  if(*ndpi_info_mod == NULL) {
60
1
    *ndpi_info_mod = ndpi_init_detection_module(g_ctx);
61
62
1
    assert(ndpi_set_config_u64(*ndpi_info_mod, NULL, "log.level", 3) == 0);
63
1
    assert(ndpi_set_config(*ndpi_info_mod, "all", "log", "enable") == 0);
64
65
1
    sprintf(name, "%s/public_suffix_list.dat", path);
66
1
    assert(ndpi_load_domain_suffixes(*ndpi_info_mod, name) >= 0);
67
1
    sprintf(name, "%s/lists/", path);
68
1
    assert(ndpi_load_categories_dir(*ndpi_info_mod, name) >= 0);
69
1
    sprintf(name, "%s/lists/protocols/", path);
70
1
    assert(ndpi_load_protocols_dir(*ndpi_info_mod, name) >= 0);
71
1
    sprintf(name, "%s/protos.txt", path);
72
1
    assert(ndpi_load_protocols_file(*ndpi_info_mod, name) >= 0);
73
1
    sprintf(name, "%s/categories.txt", path);
74
1
    assert(ndpi_load_categories_file(*ndpi_info_mod, name, NULL) >= 0);
75
1
    sprintf(name, "%s/risky_domains.txt", path);
76
1
    assert(ndpi_load_risk_domain_file(*ndpi_info_mod, name) >= 0);
77
1
    sprintf(name, "%s/ja4_fingerprints.csv", path);
78
1
    assert(ndpi_load_malicious_ja4_file(*ndpi_info_mod, name) >= 0);
79
1
    sprintf(name, "%s/tcp_fingerprints.csv", path);
80
1
    assert(ndpi_load_tcp_fingerprint_file(*ndpi_info_mod, name) >= 0);
81
1
    sprintf(name, "%s/sha1_fingerprints.csv", path);
82
1
    assert(ndpi_load_malicious_sha1_file(*ndpi_info_mod, name) >= 0);
83
1
    sprintf(name, "%s/config_only_classification.txt", path);
84
1
    assert(ndpi_set_config(*ndpi_info_mod, NULL, "filename.config", name) >= 0);
85
86
1
    ndpi_finalize_initialization(*ndpi_info_mod);
87
1
  }
88
1
}
89
90
FILE *buffer_to_file(const uint8_t *data, size_t size)
91
0
{
92
0
  return fmemopen((void *)data, size, "rw");
93
0
}