Coverage Report

Created: 2023-06-07 06:35

/src/fuzz/ext2fs_read_bitmap_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <stddef.h>
16
#include <stdint.h>
17
#include <unistd.h>
18
#include <assert.h>
19
#include <sys/syscall.h>
20
#include <linux/memfd.h>
21
#include <fuzzer/FuzzedDataProvider.h>
22
23
#include "ext2fs/ext2fs.h"
24
25
270
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26
27
270
  enum FuzzerType {
28
270
    ext2fsReadBlockBitmap,
29
270
    ext2fsReadInodeBitmap,
30
270
    kMaxValue = ext2fsReadInodeBitmap
31
270
  };
32
33
270
  FuzzedDataProvider stream(data, size);
34
270
  const FuzzerType f = stream.ConsumeEnum<FuzzerType>();
35
270
  (void) stream.ConsumeIntegral<int>();
36
37
270
  static const char* fname = "ext2_test_file";
38
39
  // Write our data to a temp file.
40
270
  int fd = syscall(SYS_memfd_create, fname, 0);
41
270
  std::vector<char> buffer = stream.ConsumeRemainingBytes<char>();
42
270
  write(fd, buffer.data(), buffer.size());
43
44
270
  std::string fspath("/proc/self/fd/" + std::to_string(fd));
45
46
270
  ext2_filsys fs;
47
270
  errcode_t retval = ext2fs_open(
48
270
      fspath.c_str(),
49
270
      EXT2_FLAG_IGNORE_CSUM_ERRORS, 0, 0,
50
270
      unix_io_manager,
51
270
      &fs);
52
53
270
  if (!retval) {
54
220
    switch (f) {
55
190
      case ext2fsReadBlockBitmap: {
56
190
        ext2fs_read_block_bitmap(fs);
57
190
        break;
58
0
      }
59
30
      case ext2fsReadInodeBitmap: {
60
30
        ext2fs_read_inode_bitmap(fs);
61
30
        break;
62
0
      }
63
0
      default: {
64
0
        assert(false);
65
0
      }
66
220
    }
67
220
    ext2fs_close(fs);
68
220
  }
69
270
  close(fd);
70
71
270
  return 0;
72
270
}