File: main.cpp

package info (click to toggle)
simdutf 7.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,244 kB
  • sloc: cpp: 60,074; ansic: 14,226; python: 3,364; sh: 321; makefile: 12
file content (72 lines) | stat: -rw-r--r-- 2,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <vector>

/*
 * this provides a way to run garbage data through the library also on platforms
 * that do not have libFuzzer. data can be taken from a fuzzing session
 * and feeding it to a program with this main function.
 */

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size);

void handle_file(std::filesystem::path file) {
  std::vector<char> data;

  const auto Nbytes = file_size(file);
  data.resize(Nbytes);

  std::ifstream filestream(file);
  filestream.read(data.data(), data.size());
  if (filestream.gcount() != Nbytes) {
    std::cerr << "failed reading from file " << file << '\n';
    return;
  }

  std::cout << "invoking fuzzer on data from file " << file << '\n';

  LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t*>(data.data()),
                         data.size());
}

void handle_directory(std::filesystem::path directory) {
  for (const auto& entry : std::filesystem::recursive_directory_iterator(
           directory,
           std::filesystem::directory_options::follow_directory_symlink)) {
    if (entry.is_regular_file()) {
      handle_file(entry.path());
    } else if (entry.is_symlink()) {
      auto resolved = read_symlink(entry);
      if (is_regular_file(resolved)) {
        handle_file(resolved);
      }
    }
  }
}

void handle_possible_file(std::filesystem::path possible_file) {
  if (std::filesystem::is_directory(possible_file)) {
    handle_directory(possible_file);
  } else if (is_regular_file(possible_file)) {
    handle_file(possible_file);
  } else if (is_symlink(possible_file)) {
    auto resolved = read_symlink(possible_file);
    handle_possible_file(resolved);
  } else {
    std::cerr << "not a directory, regular file or symlink: " << possible_file
              << '\n';
  }
}

void handle_arg(const char* path) {
  return handle_possible_file(std::filesystem::path(path));
}

int main(int argc, char* argv[]) {
  for (int i = 1; i < argc; ++i) {
    handle_arg(argv[i]);
  }
}