Coverage Report

Created: 2026-11-12 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/oss-fuzz-fuzzers/libtheora/fuzzer.cpp
Line
Count
Source
1
/* libtheora decoder fuzzer
2
 * 2019 Guido Vranken
3
 */
4
5
#include <cstddef>
6
#include <cstdint>
7
#include <fuzzing/datasource/datasource.hpp>
8
#include <fuzzing/memory.hpp>
9
#include <theora/theoradec.h>
10
11
6.44k
#define THEORA_NUM_HEADER_PACKETS 3
12
13
class TheoraDecoder {
14
    private:
15
        fuzzing::datasource::Datasource& ds;
16
17
        th_setup_info* tsi = nullptr;
18
        th_dec_ctx* ctx = nullptr;
19
        th_info ti;
20
        th_comment tc;
21
22
        bool initialize(void);
23
        void processComments(void) const;
24
        bool decodePacket(void);
25
        void writeImage(const th_ycbcr_buffer& image) const;
26
    public:
27
        TheoraDecoder(fuzzing::datasource::Datasource& ds);
28
        ~TheoraDecoder(void);
29
        void Run(void);
30
};
31
32
TheoraDecoder::TheoraDecoder(fuzzing::datasource::Datasource& ds) :
33
2.14k
    ds(ds)
34
2.14k
{ }
35
36
2.14k
TheoraDecoder::~TheoraDecoder(void) {
37
2.14k
    /* noret */ th_info_clear(&ti);
38
2.14k
    /* noret */ th_comment_clear(&tc);
39
40
2.14k
    if ( ctx != nullptr ) {
41
1.08k
        th_decode_free(ctx);
42
1.08k
    }
43
44
2.14k
    if ( tsi != nullptr ) {
45
39
        th_setup_free(tsi);
46
39
    }
47
2.14k
}
48
49
2.14k
bool TheoraDecoder::initialize(void) {
50
2.14k
    /* noret */ th_info_init(&ti);
51
2.14k
    /* noret */ th_comment_init(&tc);
52
53
6.44k
    for (int i = 0; i < THEORA_NUM_HEADER_PACKETS; i++) {
54
5.31k
        ogg_packet op = { 0 };
55
56
5.31k
        std::vector<uint8_t> packet;
57
        /* Fill packet */
58
5.31k
        {
59
5.31k
            try {
60
5.31k
                packet = ds.GetData(0);
61
5.31k
            } catch ( ... ) {
62
95
                return false;
63
95
            }
64
65
5.22k
            op.packet = packet.data();
66
5.22k
            op.bytes = packet.size();
67
5.22k
            op.b_o_s = 1;
68
5.22k
        }
69
70
5.22k
        if (th_decode_headerin(&ti, &tc, &tsi, &op) < 0) {
71
926
            return false;
72
926
        }
73
5.22k
    }
74
75
1.12k
    /* noret */ processComments();
76
77
    /* Limit picture resolution to prevent OOMs */
78
1.12k
    if ( ti.frame_width > 1024 || ti.frame_height > 1024 ) {
79
32
        return false;
80
32
    }
81
82
1.09k
    ctx = th_decode_alloc(&ti, tsi);
83
1.09k
    if ( ctx == nullptr ) {
84
7
        return false;
85
7
    }
86
87
1.08k
    /* noret */ th_setup_free(tsi);
88
1.08k
    tsi = nullptr;
89
90
1.08k
    return true;
91
1.09k
}
92
93
1.12k
void TheoraDecoder::processComments(void) const {
94
1.35k
    for (int i = 0; i < tc.comments; i++) {
95
230
        if (tc.user_comments[i]) {
96
230
            const int len = tc.comment_lengths[i];
97
230
            fuzzing::memory::memory_test(tc.user_comments[i], len);
98
230
        }
99
230
    }
100
1.12k
}
101
102
1.28k
bool TheoraDecoder::decodePacket(void) {
103
1.28k
    int err;
104
1.28k
    ogg_packet op = { 0 };
105
106
1.28k
    std::vector<uint8_t> packet;
107
108
    /* Fill packet */
109
1.28k
    {
110
1.28k
        memset(&op, 0, sizeof(op));
111
1.28k
        packet = ds.GetData(0);
112
1.28k
        op.packet = packet.data();
113
1.28k
        op.bytes = packet.size();
114
1.28k
        op.granulepos = -1;
115
        /* TODO op.packetno */
116
1.28k
    }
117
118
    /* Decode */
119
1.28k
    {
120
1.28k
        ogg_int64_t granulepos;
121
1.28k
        err = th_decode_packetin(ctx, &op, &granulepos);
122
1.28k
        if (err < 0) {
123
4
            return false;
124
4
        }
125
126
        /* Verify that granulepos has been set */
127
1.28k
        fuzzing::memory::memory_test(granulepos);
128
1.28k
    }
129
130
    /* Write image data */
131
1.28k
    if (err != TH_DUPFRAME) {
132
1.14k
        th_ycbcr_buffer ycbcrbuf;
133
1.14k
        err = th_decode_ycbcr_out(ctx, ycbcrbuf);
134
1.14k
        if (err != 0) {
135
0
            return false;
136
0
        }
137
138
1.14k
        /* noret */ writeImage(ycbcrbuf);
139
1.14k
    }
140
141
1.28k
    return true;
142
1.28k
}
143
144
1.14k
void TheoraDecoder::writeImage(const th_ycbcr_buffer& ycbcrbuf) const {
145
    /* Modelled after examples/player_example.c */
146
147
1.14k
    const th_pixel_fmt px_fmt = ti.pixel_fmt;
148
1.14k
    const int y_offset = (ti.pic_x&~1) + ycbcrbuf[0].stride * (ti.pic_y&~1);
149
1.14k
    const int w = (ti.pic_x+ti.pic_width+1&~1)-(ti.pic_x&~1);
150
1.14k
    const int h = (ti.pic_y+ti.pic_height+1&~1)-(ti.pic_y&~1);
151
152
153
1.14k
    if ( px_fmt == TH_PF_422) {
154
296
        const int uv_offset = (ti.pic_x/2) + (ycbcrbuf[1].stride) * (ti.pic_y);
155
156
19.0k
        for(int i = 0; i < h; i++) {
157
18.7k
            {
158
18.7k
                const uint8_t* in_y = ycbcrbuf[0].data+y_offset+ycbcrbuf[0].stride*i;
159
18.7k
                fuzzing::memory::memory_test(in_y, w);
160
18.7k
            }
161
162
18.7k
            {
163
18.7k
                const uint8_t* in_u = ycbcrbuf[1].data+uv_offset+ycbcrbuf[1].stride*i;
164
18.7k
                const uint8_t* in_v = ycbcrbuf[2].data+uv_offset+ycbcrbuf[2].stride*i;
165
18.7k
                fuzzing::memory::memory_test(in_u, w >> 1);
166
18.7k
                fuzzing::memory::memory_test(in_v, w >> 1);
167
18.7k
            }
168
18.7k
        }
169
847
    } else if (px_fmt==TH_PF_444){
170
32.5k
        for(int i = 0; i < h; i++) {
171
32.2k
            fuzzing::memory::memory_test(ycbcrbuf[0].data+y_offset+ycbcrbuf[0].stride*i, w);
172
32.2k
            fuzzing::memory::memory_test(ycbcrbuf[1].data+y_offset+ycbcrbuf[1].stride*i, w);
173
32.2k
            fuzzing::memory::memory_test(ycbcrbuf[2].data+y_offset+ycbcrbuf[2].stride*i, w);
174
32.2k
        }
175
597
    } else {
176
597
        const int uv_offset = (ti.pic_x/2) + (ycbcrbuf[1].stride) * (ti.pic_y/2);
177
178
33.6k
        for(int i = 0; i < h; i++) {
179
33.0k
            fuzzing::memory::memory_test(ycbcrbuf[0].data+y_offset+ycbcrbuf[0].stride*i, w);
180
33.0k
        }
181
182
17.1k
        for(int i = 0; i < h/2; i++){
183
16.5k
            fuzzing::memory::memory_test(ycbcrbuf[2].data+uv_offset+ycbcrbuf[2].stride*i, w/2);
184
16.5k
            fuzzing::memory::memory_test(ycbcrbuf[1].data+uv_offset+ycbcrbuf[1].stride*i, w/2);
185
16.5k
        }
186
597
    }
187
1.14k
}
188
189
2.14k
void TheoraDecoder::Run(void) {
190
2.14k
    if ( initialize() == false ) {
191
1.06k
        return;
192
1.06k
    }
193
194
1.08k
    try {
195
1.08k
        size_t numDecoded = 0;
196
2.36k
        while ( ++numDecoded < 10 && ds.Get<bool>() == true ) {
197
1.28k
            if ( decodePacket() == false ) {
198
4
                break;
199
4
            }
200
1.28k
        }
201
1.08k
    } catch ( ... ) { }
202
1.08k
}
203
204
2
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
205
2
    return 0;
206
2
}
207
208
2.14k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
209
2.14k
    fuzzing::datasource::Datasource ds(data, size);
210
2.14k
    TheoraDecoder decoder(ds);
211
212
2.14k
    decoder.Run();
213
214
2.14k
    return 0;
215
2.14k
}