Coverage Report

Created: 2023-12-08 06:55

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