Coverage Report

Created: 2025-08-29 06:30

/src/unit/fuzzing/nxt_json_fuzz.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) NGINX, Inc.
3
 */
4
5
#include <nxt_main.h>
6
#include <nxt_conf.h>
7
#include <nxt_router.h>
8
9
13.2k
#define KMININPUTLENGTH 2
10
6.62k
#define KMAXINPUTLENGTH 1024
11
12
13
extern int LLVMFuzzerInitialize(int *argc, char ***argv);
14
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
15
16
17
extern char  **environ;
18
19
20
int
21
LLVMFuzzerInitialize(int *argc, char ***argv)
22
2
{
23
2
    if (nxt_lib_start("fuzzing", NULL, &environ) != NXT_OK) {
24
0
        return NXT_ERROR;
25
0
    }
26
27
2
    return 0;
28
2
}
29
30
31
int
32
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
33
6.63k
{
34
6.63k
    nxt_mp_t                *mp;
35
6.63k
    nxt_str_t               input;
36
6.63k
    nxt_thread_t            *thr;
37
6.63k
    nxt_runtime_t           *rt;
38
6.63k
    nxt_conf_value_t        *conf;
39
6.63k
    nxt_conf_validation_t   vldt;
40
6.63k
    nxt_conf_json_pretty_t  pretty;
41
42
6.63k
    if (size < KMININPUTLENGTH || size > KMAXINPUTLENGTH) {
43
22
        return 0;
44
22
    }
45
46
6.60k
    thr = nxt_thread();
47
48
6.60k
    mp = nxt_mp_create(1024, 128, 256, 32);
49
6.60k
    if (mp == NULL) {
50
0
        return 0;
51
0
    }
52
53
6.60k
    rt = nxt_mp_zget(mp, sizeof(nxt_runtime_t));
54
6.60k
    if (rt == NULL) {
55
0
        goto failed;
56
0
    }
57
58
6.60k
    rt->languages = nxt_array_create(mp, 1, sizeof(nxt_app_lang_module_t));
59
6.60k
    if (rt->languages == NULL) {
60
0
        goto failed;
61
0
    }
62
63
6.60k
    input.start = (u_char *)data;
64
6.60k
    input.length = size;
65
66
6.60k
    thr->runtime = rt;
67
6.60k
    rt->mem_pool = mp;
68
69
6.60k
    nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
70
6.60k
    nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
71
72
6.60k
    conf = nxt_conf_json_parse_str(mp, &input);
73
6.60k
    if (conf == NULL) {
74
2.04k
        goto failed;
75
2.04k
    }
76
77
4.56k
    nxt_conf_json_length(conf, NULL);
78
4.56k
    nxt_conf_json_length(conf, &pretty);
79
80
4.56k
    vldt.pool = nxt_mp_create(1024, 128, 256, 32);
81
4.56k
    if (vldt.pool == NULL) {
82
0
        goto failed;
83
0
    }
84
85
4.56k
    vldt.conf = conf;
86
4.56k
    vldt.conf_pool = mp;
87
4.56k
    vldt.ver = NXT_VERNUM;
88
89
4.56k
    nxt_conf_validate(&vldt);
90
4.56k
    nxt_mp_destroy(vldt.pool);
91
92
6.60k
failed:
93
94
6.60k
    nxt_mp_destroy(mp);
95
96
6.60k
    return 0;
97
4.56k
}