Coverage Report

Created: 2022-10-19 06:37

/src/bzip2_filename.c
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
extern BZFILE* BZ2_bzWriteOpen(
30
  int*  bzerror,
31
  FILE* f,
32
  int   blockSize100k,
33
  int   verbosity,
34
  int   workFactor );
35
36
extern BZFILE* BZ2_bzReadOpen(
37
  int*  bzerror,
38
  FILE* f,
39
  int   verbosity,
40
  int   small,
41
  void* unused,
42
  int   nUnused );
43
44
2.47k
static void fuzzer_write_data(FILE *file, const uint8_t *data, size_t size) {
45
2.47k
  int    bzerr;
46
2.47k
  int    blockSize100k = 9;
47
2.47k
  int    verbosity     = 0;
48
2.47k
  int    workFactor    = 30;
49
2.47k
  uint   nbytes_in_lo32, nbytes_in_hi32;
50
2.47k
  uint   nbytes_out_lo32, nbytes_out_hi32;
51
52
2.47k
  BZFILE* bzf = BZ2_bzWriteOpen ( &bzerr, file,
53
2.47k
                           blockSize100k, verbosity, workFactor );
54
55
2.47k
  BZ2_bzWrite (&bzerr, bzf, (void*)data, size);
56
57
2.47k
  BZ2_bzWriteClose64 ( &bzerr, bzf, 0,
58
2.47k
                        &nbytes_in_lo32, &nbytes_in_hi32,
59
2.47k
                        &nbytes_out_lo32, &nbytes_out_hi32 );
60
2.47k
}
61
62
2.47k
static void fuzzer_read_data(FILE *file) {
63
2.47k
  int    bzerr;
64
2.47k
  int    verbosity = 0;
65
2.47k
  char   obuf[BZ_MAX_UNUSED];
66
2.47k
  char   unused[BZ_MAX_UNUSED];
67
2.47k
  int    nUnused = 0;
68
2.47k
  bool   smallMode = 0;
69
70
2.47k
  BZFILE* bzf2 = BZ2_bzReadOpen (&bzerr, file, verbosity, (int)smallMode, unused, nUnused);
71
72
4.94k
  while (bzerr == BZ_OK) {
73
2.47k
      BZ2_bzRead ( &bzerr, bzf2, obuf, BZ_MAX_UNUSED);
74
2.47k
  }
75
76
2.47k
  BZ2_bzReadClose ( &bzerr, bzf2);
77
2.47k
}
78
79
int
80
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
81
2.47k
{
82
2.47k
  char* filename = strdup("/tmp/generate_temporary_file.XXXXXX");
83
2.47k
  if (!filename) {
84
0
    perror("Failed to allocate file name buffer.");
85
0
    abort();
86
0
  }
87
2.47k
  const int file_descriptor = mkstemp(filename);
88
2.47k
  if (file_descriptor < 0) {
89
0
    perror("Failed to make temporary file.");
90
0
    abort();
91
0
  }
92
2.47k
  FILE* file = fdopen(file_descriptor, "wb");
93
2.47k
  if (!file) {
94
0
    perror("Failed to open file descriptor.");
95
0
    close(file_descriptor);
96
0
    abort();
97
0
  }
98
  
99
2.47k
  fuzzer_write_data(file, data, size);
100
101
2.47k
  fuzzer_read_data(file);
102
103
2.47k
  fclose(file);
104
105
2.47k
  if (unlink(filename) != 0) {
106
0
    perror("WARNING: Failed to delete temporary file.");
107
0
  }
108
2.47k
  free(filename);
109
2.47k
  return 0;
110
2.47k
}