summaryrefslogtreecommitdiffhomepage
path: root/fuzz/mvsfmt.c
blob: 01dd886a1fe0c33246934a2eb0236837b87cdfde (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * @file mvsfmt.c
 * @brief libFuzzer target for the MVS decoders (src/mvsfmt.c).
 *
 * The first byte chooses the decoder; the rest is the buffer it gets:
 * a CSI work area, a PDS directory block, TIOT entries, a filter key,
 * the work area a fake IGGCSI00 returns, or a name field.  Built and
 * run by `make fuzz` (clang, ASan, UBSan).
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src/mvsfmt.h"

/** @brief CSIRESUM in src/mvsfmt.c: the byte that asks for a resume. */
#define CSI_RESUM 149

/** @brief Callback: touch every byte of the name so ASan sees it. */
static void sink(const char *name, char type, void *arg) {
    size_t *total = arg;
    *total += strlen(name) + (size_t)(unsigned char)type;
}

/* ---- a fake IGGCSI00, so csi_search's resume loop can be fuzzed ------ */

/** @brief Bytes the fake call fills the work area with. */
static const unsigned char *fake_src;
/** @brief Their number. */
static size_t fake_len;

/** @brief One fake IGGCSI00 call: hand back the fuzz bytes and maybe resume. */
static int fake_call(csi_area *a, void *ctx) {
    (void)ctx;
    memset(a->work, 0, a->worklen);
    memcpy(a->work, fake_src, fake_len < a->worklen ? fake_len : a->worklen);
    /* A blank byte here means "no more"; anything else resumes. */
    a->field[CSI_RESUM] = fake_len && (fake_src[0] & 1) ? 0xe8 : 0x40;
    return fake_len && (fake_src[0] & 2) ? 4 : 0;
}

/** @brief Search with a work area of `worklen` the fake call fills. */
static void fuzz_search(const unsigned char *buf, size_t size,
                        uint32_t worklen) {
    unsigned char reason[4] = {0};
    unsigned char field[CSI_FIELD_LEN];
    char *filter = malloc(size + 1);
    unsigned char *work = malloc(worklen);
    if (!filter || !work) {
        free(filter);
        free(work);
        return;
    }
    memcpy(filter, buf, size);
    filter[size] = '\0';
    fake_src = buf;
    fake_len = size;
    csi_area a = {reason, field, work, worklen};
    size_t total = 0;
    (void)csi_search(filter, &a, fake_call, NULL, sink, &total);
    free(work);
    free(filter);
}

/** @brief Fuzzer entry point. */
int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);

int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
    if (size < 1)
        return 0;
    unsigned char which = data[0] % 6;
    data++;
    size--;
    /* An exact-size heap copy, so a read past the end is caught. */
    unsigned char *buf = malloc(size ? size : 1);
    if (!buf)
        return 0;
    memcpy(buf, data, size);
    size_t total = 0;
    bool end = false;
    switch (which) {
    case 0:
        (void)csi_parse(buf, size, sink, &total);
        break;
    case 1:
        if (size >= PDS_DIRBLK)
            (void)pds_parse_block(buf, sink, &total, &end);
        break;
    case 2:
        (void)tiot_parse(buf, size, sink, &total);
        break;
    case 3: {
        char *key = malloc(size + 1);
        if (key) {
            memcpy(key, buf, size);
            key[size] = '\0';
            if (csi_filter_ok(key)) {
                unsigned char field[CSI_FIELD_LEN];
                csi_build(field, key);
            }
            (void)mvs_dsn_ok(key);
            free(key);
        }
        break;
    }
    case 4:
        /* The smallest work area csi_search accepts, and a larger one. */
        fuzz_search(buf, size, CSI_WORK_MIN);
        fuzz_search(buf, size, CSI_WORK_MIN + 3);
        break;
    case 5: {
        /* A name field and both directions of the byte map, each in a
         * buffer exactly the size the function is allowed to touch. */
        char *dst = malloc(size + 1);
        if (dst) {
            mvs_name(buf, size, dst);
            total += strlen(dst);
            free(dst);
        }
        mvs_from_ebcdic((char *)buf, size);
        mvs_to_ebcdic((char *)buf, size);
        break;
    }
    }
    free(buf);
    return 0;
}