xref: /aosp_15_r20/external/libdav1d/tests/libfuzzer/main.c (revision c09093415860a1c2373dacd84c4fde00c507cdfd)
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 <inttypes.h>
32 #include <limits.h>
33 #include <stddef.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 
39 #include "dav1d_fuzzer.h"
40 
41 // expects ivf input
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[]) {
44     int ret = -1;
45     FILE *f = NULL;
46     int64_t fsize;
47     const char *filename = NULL;
48     uint8_t *data = NULL;
49     size_t size = 0;
50 
51     if (LLVMFuzzerInitialize(&argc, &argv)) {
52         return 1;
53     }
54 
55     if (argc != 2) {
56         fprintf(stdout, "Usage:\n%s fuzzing_testcase.ivf\n", argv[0]);
57         return -1;
58     }
59     filename = argv[1];
60 
61     if (!(f = fopen(filename, "rb"))) {
62         fprintf(stderr, "failed to open %s: %s\n", filename, strerror(errno));
63         goto error;
64     }
65 
66     if (fseeko(f, 0, SEEK_END) == -1) {
67         fprintf(stderr, "fseek(%s, 0, SEEK_END) failed: %s\n", filename,
68                 strerror(errno));
69         goto error;
70     }
71     if ((fsize = ftello(f)) == -1) {
72         fprintf(stderr, "ftell(%s) failed: %s\n", filename, strerror(errno));
73         goto error;
74     }
75     rewind(f);
76 
77     if (fsize < 0 || fsize > INT_MAX) {
78         fprintf(stderr, "%s is too large: %"PRId64"\n", filename, fsize);
79         goto error;
80     }
81     size = (size_t)fsize;
82 
83     if (!(data = malloc(size))) {
84         fprintf(stderr, "failed to allocate: %zu bytes\n", size);
85         goto error;
86     }
87 
88     if (fread(data, size, 1, f) == size) {
89         fprintf(stderr, "failed to read %zu bytes from %s: %s\n", size,
90                 filename, strerror(errno));
91         goto error;
92     }
93 
94     ret = LLVMFuzzerTestOneInput(data, size);
95 
96 error:
97     free(data);
98     if (f) fclose(f);
99     return ret;
100 }
101