/src/CMake/Tests/Fuzzing/cmGccDepfileFuzzer.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | |
4 | | /* |
5 | | * Fuzzer for CMake's GCC dependency file parser |
6 | | * |
7 | | * GCC generates .d files with make-style dependencies. |
8 | | * This fuzzer tests parsing of these files. |
9 | | */ |
10 | | |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <cstdio> |
14 | | #include <string> |
15 | | |
16 | | #include <unistd.h> |
17 | | |
18 | | #include "cmGccDepfileReader.h" |
19 | | #include "cmSystemTools.h" |
20 | | |
21 | | static constexpr size_t kMaxInputSize = 256 * 1024; |
22 | | static std::string g_testDir; |
23 | | |
24 | | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) |
25 | 6 | { |
26 | 6 | (void)argc; |
27 | 6 | (void)argv; |
28 | | |
29 | 6 | char tmpl[] = "/tmp/cmake_fuzz_depfile_XXXXXX"; |
30 | 6 | char* dir = mkdtemp(tmpl); |
31 | 6 | if (dir) { |
32 | 6 | g_testDir = dir; |
33 | 6 | } else { |
34 | 0 | g_testDir = "/tmp/cmake_fuzz_depfile"; |
35 | 0 | cmSystemTools::MakeDirectory(g_testDir); |
36 | 0 | } |
37 | | |
38 | 6 | return 0; |
39 | 6 | } |
40 | | |
41 | | extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) |
42 | 5.67k | { |
43 | 5.67k | if (size == 0 || size > kMaxInputSize) { |
44 | 4 | return 0; |
45 | 4 | } |
46 | | |
47 | | // Write input to temp file |
48 | 5.67k | std::string depFile = g_testDir + "/test.d"; |
49 | 5.67k | { |
50 | 5.67k | FILE* fp = fopen(depFile.c_str(), "wb"); |
51 | 5.67k | if (!fp) |
52 | 0 | return 0; |
53 | 5.67k | fwrite(data, 1, size, fp); |
54 | 5.67k | fclose(fp); |
55 | 5.67k | } |
56 | | |
57 | | // Parse the depfile |
58 | 0 | auto result = cmReadGccDepfile(depFile.c_str()); |
59 | | |
60 | | // Examine results if parsing succeeded |
61 | 5.67k | if (result) { |
62 | 45.4k | for (auto const& entry : *result) { |
63 | 62.5k | for (auto const& rule : entry.rules) { |
64 | 62.5k | (void)rule; |
65 | 62.5k | } |
66 | 70.8k | for (auto const& path : entry.paths) { |
67 | 70.8k | (void)path; |
68 | 70.8k | } |
69 | 45.4k | } |
70 | 5.61k | } |
71 | | |
72 | | // Clean up |
73 | 5.67k | unlink(depFile.c_str()); |
74 | | |
75 | 5.67k | return 0; |
76 | 5.67k | } |