Coverage Report

Created: 2025-08-03 06:34

/src/openvswitch/tests/oss-fuzz/odp_target.c
Line
Count
Source (jump to first uncovered line)
1
#include <config.h>
2
#include "fuzzer.h"
3
#undef NDEBUG
4
#include "odp-util.h"
5
#include <stdio.h>
6
#include "openvswitch/dynamic-string.h"
7
#include "flow.h"
8
#include "openvswitch/match.h"
9
#include "openvswitch/ofpbuf.h"
10
#include "util.h"
11
#include "openvswitch/ofp-flow.h"
12
#include "openvswitch/vlog.h"
13
14
static int
15
parse_keys(bool wc_keys, const char *in)
16
34.3k
{
17
34.3k
    int exit_code = 0;
18
19
34.3k
    enum odp_key_fitness fitness;
20
34.3k
    struct ofpbuf odp_key;
21
34.3k
    struct ofpbuf odp_mask;
22
34.3k
    struct flow flow;
23
34.3k
    struct ds out;
24
34.3k
    int error;
25
26
    /* Convert string to OVS DP key. */
27
34.3k
    ofpbuf_init(&odp_key, 0);
28
34.3k
    ofpbuf_init(&odp_mask, 0);
29
34.3k
    error = odp_flow_from_string(in, NULL,
30
34.3k
                                 &odp_key, &odp_mask, NULL);
31
34.3k
    if (error) {
32
24.7k
        printf("odp_flow_from_string: error\n");
33
24.7k
        goto next;
34
24.7k
    }
35
36
9.59k
    if (!wc_keys) {
37
4.79k
        struct odp_flow_key_parms odp_parms = {
38
4.79k
            .flow = &flow,
39
4.79k
            .support = {
40
4.79k
                .recirc = true,
41
4.79k
                .ct_state = true,
42
4.79k
                .ct_zone = true,
43
4.79k
                .ct_mark = true,
44
4.79k
                .ct_label = true,
45
4.79k
                .max_vlan_headers = SIZE_MAX,
46
4.79k
            },
47
4.79k
        };
48
49
        /* Convert odp_key to flow. */
50
4.79k
        fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size,
51
4.79k
                                       &flow, NULL);
52
4.79k
        switch (fitness) {
53
2.28k
            case ODP_FIT_PERFECT:
54
2.28k
                break;
55
56
103
            case ODP_FIT_TOO_LITTLE:
57
103
                printf("ODP_FIT_TOO_LITTLE: ");
58
103
                break;
59
60
424
            case ODP_FIT_TOO_MUCH:
61
424
                printf("ODP_FIT_TOO_MUCH: ");
62
424
                break;
63
64
1.97k
            case ODP_FIT_ERROR:
65
1.97k
                printf("odp_flow_key_to_flow: error\n");
66
1.97k
                goto next;
67
4.79k
        }
68
        /* Convert cls_rule back to odp_key. */
69
2.81k
        ofpbuf_uninit(&odp_key);
70
2.81k
        ofpbuf_init(&odp_key, 0);
71
2.81k
        odp_flow_key_from_flow(&odp_parms, &odp_key);
72
73
2.81k
        if (odp_key.size > ODPUTIL_FLOW_KEY_BYTES) {
74
0
            printf ("too long: %"PRIu32" > %d\n",
75
0
                    odp_key.size, ODPUTIL_FLOW_KEY_BYTES);
76
0
            exit_code = 1;
77
0
        }
78
2.81k
    }
79
80
    /* Convert odp_key to string. */
81
7.61k
    ds_init(&out);
82
7.61k
    if (wc_keys) {
83
4.79k
        odp_flow_format(odp_key.data, odp_key.size,
84
4.79k
                        odp_mask.data, odp_mask.size, NULL, &out, false,
85
4.79k
                        false);
86
4.79k
    } else {
87
2.81k
        odp_flow_key_format(odp_key.data, odp_key.size, &out);
88
2.81k
    }
89
7.61k
    puts(ds_cstr(&out));
90
7.61k
    ds_destroy(&out);
91
92
34.3k
next:
93
34.3k
    ofpbuf_uninit(&odp_key);
94
34.3k
    ofpbuf_uninit(&odp_mask);
95
96
34.3k
    return exit_code;
97
7.61k
}
98
99
static int
100
parse_actions(const char *in)
101
17.1k
{
102
17.1k
    struct ofpbuf odp_actions;
103
17.1k
    struct ds out;
104
17.1k
    int error;
105
106
    /* Convert string to OVS DP actions. */
107
17.1k
    ofpbuf_init(&odp_actions, 0);
108
17.1k
    error = odp_actions_from_string(in, NULL, &odp_actions);
109
17.1k
    if (error) {
110
13.9k
        printf("odp_actions_from_string: error\n");
111
13.9k
        goto next;
112
13.9k
    }
113
114
    /* Convert odp_actions back to string. */
115
3.21k
    ds_init(&out);
116
3.21k
    format_odp_actions(&out, odp_actions.data, odp_actions.size, NULL);
117
3.21k
    puts(ds_cstr(&out));
118
3.21k
    ds_destroy(&out);
119
120
17.1k
next:
121
17.1k
    ofpbuf_uninit(&odp_actions);
122
17.1k
    return 0;
123
3.21k
}
124
125
int
126
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
127
17.2k
{
128
    /* Bail out if we cannot construct at least a 1 char string. */
129
17.2k
    const char *input = (const char *) data;
130
17.2k
    if (size < 2 || input[size - 1] != '\0' || strchr(input, '\n') ||
131
17.2k
        strlen(input) != size - 1) {
132
35
        return 0;
133
35
    }
134
135
    /* Disable logging to avoid write to disk. */
136
17.1k
    static bool isInit = false;
137
17.1k
    if (!isInit) {
138
1
        vlog_set_verbosity("off");
139
1
        isInit = true;
140
1
    }
141
142
    /* Parse keys and wc keys. */
143
17.1k
    parse_keys(false, input);
144
17.1k
    parse_keys(true, input);
145
146
    /* Parse actions. */
147
17.1k
    parse_actions(input);
148
149
17.1k
    return 0;
150
17.2k
}