Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | # Copyright 2022 Google LLC |
3 | | # |
4 | | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | # you may not use this file except in compliance with the License. |
6 | | # You may obtain a copy of the License at |
7 | | # |
8 | | # http://www.apache.org/licenses/LICENSE-2.0 |
9 | | # |
10 | | # Unless required by applicable law or agreed to in writing, software |
11 | | # distributed under the License is distributed on an "AS IS" BASIS, |
12 | | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | # See the License for the specific language governing permissions and |
14 | | # limitations under the License. |
15 | | # |
16 | | ############################################################################### |
17 | | */ |
18 | | |
19 | | #include "bzlib.h" |
20 | | #include <stdint.h> |
21 | | #include <stdlib.h> |
22 | | #include <assert.h> |
23 | | #include <string.h> |
24 | | #include <stddef.h> |
25 | | #include <unistd.h> |
26 | | #include <stdio.h> |
27 | | #include <stdbool.h> |
28 | | |
29 | 4.03k | static void fuzzer_write_data(FILE *file, const uint8_t *data, size_t size) { |
30 | 4.03k | int bzerr = 0; |
31 | 4.03k | int blockSize100k = 9; |
32 | 4.03k | int verbosity = 0; |
33 | 4.03k | int workFactor = 30; |
34 | 4.03k | uint nbytes_in_lo32, nbytes_in_hi32; |
35 | 4.03k | uint nbytes_out_lo32, nbytes_out_hi32; |
36 | | |
37 | 4.03k | BZFILE* bzf = BZ2_bzWriteOpen ( &bzerr, file, |
38 | 4.03k | blockSize100k, verbosity, workFactor ); |
39 | | |
40 | 4.03k | BZ2_bzWrite (&bzerr, bzf, (void*)data, size); |
41 | | |
42 | 4.03k | BZ2_bzWriteClose64 ( &bzerr, bzf, 0, |
43 | 4.03k | &nbytes_in_lo32, &nbytes_in_hi32, |
44 | 4.03k | &nbytes_out_lo32, &nbytes_out_hi32 ); |
45 | 4.03k | return; |
46 | 4.03k | } |
47 | | |
48 | 4.03k | static void fuzzer_read_data(char *filename) { |
49 | 4.03k | int bzerr = 0; |
50 | 4.03k | char obuf[BZ_MAX_UNUSED]; |
51 | | |
52 | 4.03k | BZFILE* bzf2 = BZ2_bzopen(filename,"rb"); |
53 | | |
54 | 87.4k | while (bzerr == BZ_OK) { |
55 | 83.3k | BZ2_bzRead ( &bzerr, bzf2, obuf, BZ_MAX_UNUSED); |
56 | 83.3k | } |
57 | | |
58 | 4.03k | BZ2_bzReadClose ( &bzerr, bzf2); |
59 | 4.03k | return; |
60 | 4.03k | } |
61 | | |
62 | | int |
63 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
64 | 7.26k | { |
65 | 7.26k | char* filename = strdup("/tmp/generate_temporary_file.XXXXXX"); |
66 | 7.26k | if (!filename) { |
67 | 0 | perror("Failed to allocate file name buffer."); |
68 | 0 | abort(); |
69 | 0 | } |
70 | 7.26k | const int file_descriptor = mkstemp(filename); |
71 | 7.26k | if (file_descriptor < 0) { |
72 | 0 | perror("Failed to make temporary file."); |
73 | 0 | abort(); |
74 | 0 | } |
75 | 7.26k | FILE* file = fdopen(file_descriptor, "wb"); |
76 | 7.26k | if (!file) { |
77 | 0 | perror("Failed to open file descriptor."); |
78 | 0 | close(file_descriptor); |
79 | 0 | abort(); |
80 | 0 | } |
81 | | |
82 | 7.26k | fuzzer_write_data(file, data, size); |
83 | | |
84 | 7.26k | fuzzer_read_data(filename); |
85 | | |
86 | 7.26k | BZ2_bzlibVersion(); |
87 | | |
88 | 7.26k | BZ2_bzflush(file); |
89 | 7.26k | fclose(file); |
90 | | |
91 | 7.26k | if (unlink(filename) != 0) { |
92 | 0 | perror("WARNING: Failed to delete temporary file."); |
93 | 0 | } |
94 | 7.26k | free(filename); |
95 | 7.26k | return 0; |
96 | 7.26k | } |