/src/ndpi/fuzz/fuzz_alg_isolationforest.cpp
Line | Count | Source |
1 | | #include "ndpi_api.h" |
2 | | |
3 | | #include <stdint.h> |
4 | | #include <stdio.h> |
5 | | #include "fuzzer/FuzzedDataProvider.h" |
6 | | |
7 | 311 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
8 | 311 | FuzzedDataProvider fuzzed_data(data, size); |
9 | 311 | u_int32_t n_normal, n_attacks, n_features, i, j; |
10 | 311 | double **d; |
11 | 311 | void *f; |
12 | | |
13 | | /* isolationforest code doesn't handle allocation failures */ |
14 | | |
15 | | /* Data set */ |
16 | 311 | n_features = fuzzed_data.ConsumeIntegralInRange<u_int32_t>(0, 8); |
17 | 311 | n_normal = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
18 | 311 | n_attacks = fuzzed_data.ConsumeIntegral<u_int8_t>(); |
19 | | |
20 | 311 | d = (double **)ndpi_malloc(sizeof(double *) * (n_normal + n_attacks)); |
21 | | |
22 | 21.7k | for(i = 0; i < n_normal; i++) { |
23 | 21.4k | u_int32_t l = sizeof(double) * n_features; |
24 | 21.4k | double *row = (double *)ndpi_malloc(l); |
25 | | |
26 | 21.4k | d[i] = row; |
27 | 97.8k | for(j = 0; j < n_features; j++) |
28 | 76.4k | row[j] = fuzzed_data.ConsumeFloatingPoint<double>(); |
29 | 21.4k | } |
30 | 27.9k | for(i = 0; i < n_attacks; i++) { |
31 | 27.6k | u_int32_t l = sizeof(double) * n_features; |
32 | 27.6k | double *row = (double *)ndpi_malloc(l); |
33 | | |
34 | 27.6k | d[n_normal + i] = row; |
35 | 121k | for(j = 0; j < n_features; j++) |
36 | 94.0k | row[j] = fuzzed_data.ConsumeFloatingPoint<double>(); |
37 | 27.6k | } |
38 | | |
39 | 311 | f = ndpi_alloc_iforest(d, n_normal, n_features); |
40 | 49.3k | for(i = 0; i < n_normal + n_attacks; i++) { |
41 | 49.0k | ndpi_iforest_score(f, d[i]); |
42 | 49.0k | } |
43 | | |
44 | 311 | ndpi_free_iforest(f); |
45 | | |
46 | 49.3k | for(i = 0; i < n_normal + n_attacks; i++) |
47 | 49.0k | ndpi_free(d[i]); |
48 | 311 | ndpi_free(d); |
49 | | |
50 | 311 | return 0; |
51 | 311 | } |