summaryrefslogtreecommitdiffhomepage
path: root/fuzzing/nxt_json_fuzz.c
blob: 3a9201d86001f01a4ada64ef78ed47bf06f5894f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <nxt_conf.h>
#include <nxt_router.h>

#define KMININPUTLENGTH 2
#define KMAXINPUTLENGTH 1024


extern int LLVMFuzzerInitialize(int *argc, char ***argv);
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);


extern char  **environ;


int
LLVMFuzzerInitialize(int *argc, char ***argv)
{
    if (nxt_lib_start("fuzzing", NULL, &environ) != NXT_OK) {
        return NXT_ERROR;
    }

    return 0;
}


int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    nxt_mp_t               *mp;
    nxt_str_t              input;
    nxt_thread_t           *thr;
    nxt_runtime_t          *rt;
    nxt_conf_value_t       *conf;
    nxt_conf_validation_t  vldt;

    if (size < KMININPUTLENGTH || size > KMAXINPUTLENGTH) {
        return 0;
    }

    thr = nxt_thread();

    mp = nxt_mp_create(1024, 128, 256, 32);
    if (mp == NULL) {
        return 0;
    }

    rt = nxt_mp_zget(mp, sizeof(nxt_runtime_t));
    if (rt == NULL) {
        goto failed;
    }

    rt->languages = nxt_array_create(mp, 1, sizeof(nxt_app_lang_module_t));
    if (rt->languages == NULL) {
        goto failed;
    }

    input.start = (u_char *)data;
    input.length = size;

    thr->runtime = rt;
    rt->mem_pool = mp;

    nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));

    conf = nxt_conf_json_parse_str(mp, &input);
    if (conf == NULL) {
        goto failed;
    }

    vldt.pool = nxt_mp_create(1024, 128, 256, 32);
    if (vldt.pool == NULL) {
        goto failed;
    }

    vldt.conf = conf;
    vldt.conf_pool = mp;
    vldt.ver = NXT_VERNUM;

    nxt_conf_validate(&vldt);
    nxt_mp_destroy(vldt.pool);

failed:

    nxt_mp_destroy(mp);

    return 0;
}