Coverage Report

Created: 2026-12-01 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d_fuzzer_nosimd.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Janne Grunau
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include <errno.h>
31
#include <stddef.h>
32
#include <stdint.h>
33
#include <string.h>
34
#include <stdlib.h>
35
36
#include <dav1d/dav1d.h>
37
#include "src/cpu.h"
38
#include "dav1d_fuzzer.h"
39
40
#ifdef DAV1D_ALLOC_FAIL
41
42
#include "alloc_fail.h"
43
44
static unsigned djb_xor(const uint8_t * c, size_t len) {
45
    unsigned hash = 5381;
46
    for(size_t i = 0; i < len; i++)
47
        hash = hash * 33 ^ c[i];
48
    return hash;
49
}
50
#endif
51
52
284k
static unsigned r32le(const uint8_t *const p) {
53
284k
    return ((uint32_t)p[3] << 24U) | (p[2] << 16U) | (p[1] << 8U) | p[0];
54
284k
}
55
56
41.9k
#define DAV1D_FUZZ_MAX_SIZE 4096 * 4096
57
58
// search for "--cpumask xxx" in argv and remove both parameters
59
6
int LLVMFuzzerInitialize(int *argc, char ***argv) {
60
6
    dav1d_set_cpu_flags_mask(0);
61
6
    int i = 1;
62
33
    for (; i < *argc; i++) {
63
27
        if (!strcmp((*argv)[i], "--cpumask")) {
64
0
            const char * cpumask = (*argv)[i+1];
65
0
            if (cpumask) {
66
0
                char *end;
67
0
                unsigned res;
68
0
                if (!strncmp(cpumask, "0x", 2)) {
69
0
                    cpumask += 2;
70
0
                    res = (unsigned) strtoul(cpumask, &end, 16);
71
0
                } else {
72
0
                    res = (unsigned) strtoul(cpumask, &end, 0);
73
0
                }
74
0
                if (end != cpumask && !end[0]) {
75
0
                    dav1d_set_cpu_flags_mask(res);
76
0
                }
77
0
            }
78
0
            break;
79
0
        }
80
27
    }
81
82
6
    for (; i < *argc - 2; i++) {
83
0
        (*argv)[i] = (*argv)[i + 2];
84
0
    }
85
86
6
    *argc = i;
87
88
6
    return 0;
89
6
}
90
91
92
// expects ivf input
93
94
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
95
41.9k
{
96
41.9k
    Dav1dSettings settings = { 0 };
97
41.9k
    Dav1dContext * ctx = NULL;
98
41.9k
    Dav1dPicture pic;
99
41.9k
    const uint8_t *ptr = data;
100
41.9k
    int have_seq_hdr = 0;
101
41.9k
    int err;
102
103
41.9k
    dav1d_version();
104
105
41.9k
    if (size < 32) goto end;
106
#ifdef DAV1D_ALLOC_FAIL
107
    unsigned h = djb_xor(ptr, 32);
108
    unsigned seed = h;
109
    unsigned probability = h > (RAND_MAX >> 5) ? RAND_MAX >> 5 : h;
110
    int max_frame_delay = (h & 0xf) + 1;
111
    int n_threads = ((h >> 4) & 0x7) + 1;
112
    if (max_frame_delay > 5) max_frame_delay = 1;
113
    if (n_threads > 3) n_threads = 1;
114
#endif
115
41.9k
    ptr += 32; // skip ivf header
116
117
41.9k
    dav1d_default_settings(&settings);
118
119
#ifdef DAV1D_MT_FUZZING
120
    settings.max_frame_delay = settings.n_threads = 4;
121
#elif defined(DAV1D_ALLOC_FAIL)
122
    settings.max_frame_delay = max_frame_delay;
123
    settings.n_threads = n_threads;
124
    dav1d_setup_alloc_fail(seed, probability);
125
#else
126
41.9k
    settings.max_frame_delay = settings.n_threads = 1;
127
41.9k
#endif
128
41.9k
#if defined(DAV1D_FUZZ_MAX_SIZE)
129
41.9k
    settings.frame_size_limit = DAV1D_FUZZ_MAX_SIZE;
130
41.9k
#endif
131
132
41.9k
    err = dav1d_open(&ctx, &settings);
133
41.9k
    if (err < 0) goto end;
134
135
701k
    while (ptr <= data + size - 12) {
136
663k
        Dav1dData buf;
137
663k
        uint8_t *p;
138
139
663k
        size_t frame_size = r32le(ptr);
140
663k
        ptr += 12;
141
142
663k
        if (frame_size > size || ptr > data + size - frame_size)
143
3.90k
            break;
144
145
659k
        if (!frame_size) continue;
146
147
518k
        if (!have_seq_hdr) {
148
53.7k
            Dav1dSequenceHeader seq;
149
53.7k
            int err = dav1d_parse_sequence_header(&seq, ptr, frame_size);
150
            // skip frames until we see a sequence header
151
53.7k
            if  (err != 0) {
152
13.0k
                ptr += frame_size;
153
13.0k
                continue;
154
13.0k
            }
155
40.7k
            have_seq_hdr = 1;
156
40.7k
        }
157
158
        // copy frame data to a new buffer to catch reads past the end of input
159
505k
        p = dav1d_data_create(&buf, frame_size);
160
505k
        if (!p) goto cleanup;
161
505k
        memcpy(p, ptr, frame_size);
162
505k
        ptr += frame_size;
163
164
522k
        do {
165
522k
            if ((err = dav1d_send_data(ctx, &buf)) < 0) {
166
203k
                if (err != DAV1D_ERR(EAGAIN))
167
182k
                    break;
168
203k
            }
169
340k
            memset(&pic, 0, sizeof(pic));
170
340k
            err = dav1d_get_picture(ctx, &pic);
171
340k
            if (err == 0) {
172
105k
                dav1d_picture_unref(&pic);
173
234k
            } else if (err != DAV1D_ERR(EAGAIN)) {
174
150k
                break;
175
150k
            }
176
340k
        } while (buf.sz > 0);
177
178
505k
        if (buf.sz > 0)
179
186k
            dav1d_data_unref(&buf);
180
505k
    }
181
182
41.9k
    memset(&pic, 0, sizeof(pic));
183
41.9k
    if ((err = dav1d_get_picture(ctx, &pic)) == 0) {
184
        /* Test calling dav1d_picture_unref() after dav1d_close() */
185
28.2k
        do {
186
28.2k
            Dav1dPicture pic2 = { 0 };
187
28.2k
            if ((err = dav1d_get_picture(ctx, &pic2)) == 0)
188
16.2k
                dav1d_picture_unref(&pic2);
189
28.2k
        } while (err != DAV1D_ERR(EAGAIN));
190
191
9.30k
        dav1d_close(&ctx);
192
9.30k
        dav1d_picture_unref(&pic);
193
9.30k
        return 0;
194
9.30k
    }
195
196
32.6k
cleanup:
197
32.6k
    dav1d_close(&ctx);
198
32.6k
end:
199
32.6k
    return 0;
200
32.6k
}