Coverage Report

Created: 2023-06-07 06:35

/src/fuzz/ext2fs_image_read_write_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
475
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26
27
475
  enum FuzzerType {
28
475
    ext2fsImageBitmapRead,
29
475
    ext2fsImageInodeRead,
30
475
    ext2fsImageSuperRead,
31
475
    ext2fsImageBitmapWrite,
32
475
    ext2fsImageInodeWrite,
33
475
    ext2fsImageSuperWrite,
34
475
    kMaxValue = ext2fsImageSuperWrite
35
475
  };
36
37
475
  FuzzedDataProvider stream(data, size);
38
475
  const FuzzerType f = stream.ConsumeEnum<FuzzerType>();
39
475
  (void) stream.ConsumeIntegral<int>();
40
  // Keep this here to not spoil the corpus or reproducers
41
42
475
  static const char* fname = "ext2_test_file";
43
44
  // Write our data to a temp file.
45
475
  int fd = syscall(SYS_memfd_create, fname, 0);
46
475
  std::vector<char> buffer = stream.ConsumeRemainingBytes<char>();
47
475
  write(fd, buffer.data(), buffer.size());
48
49
475
  std::string fspath("/proc/self/fd/" + std::to_string(fd));
50
51
475
  ext2_filsys fs;
52
475
  errcode_t retval = ext2fs_open(
53
475
      fspath.c_str(),
54
475
      EXT2_FLAG_IGNORE_CSUM_ERRORS, 0, 0,
55
475
      unix_io_manager,
56
475
      &fs);
57
58
475
  if (!retval) {
59
401
    switch (f) {
60
99
      case ext2fsImageBitmapRead: {
61
99
        ext2fs_image_bitmap_read(fs, fd, 0);
62
99
        break;
63
0
      }
64
9
      case ext2fsImageInodeRead: {
65
9
        ext2fs_image_inode_read(fs, fd, 0);
66
9
        break;
67
0
      }
68
1
      case ext2fsImageSuperRead: {
69
1
        ext2fs_image_super_read(fs, fd, 0);
70
1
        break;
71
0
      }
72
123
      case ext2fsImageBitmapWrite: {
73
123
        ext2fs_image_bitmap_write(fs, fd, 0);
74
123
        break;
75
0
      }
76
162
      case ext2fsImageInodeWrite: {
77
162
        ext2fs_image_inode_write(fs, fd, 0);
78
162
        break;
79
0
      }
80
7
      case ext2fsImageSuperWrite: {
81
7
        ext2fs_image_super_write(fs, fd, 0);
82
7
        break;
83
0
      }
84
0
      default: {
85
0
        assert(false);
86
0
      }
87
401
    }
88
401
    ext2fs_close(fs);
89
401
  }
90
475
  close(fd);
91
92
475
  return 0;
93
475
}