File: cmGeneratorExpressionFuzzer.cxx

package info (click to toggle)
cmake 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 158,704 kB
  • sloc: ansic: 406,077; cpp: 309,512; sh: 4,233; python: 3,696; yacc: 3,109; lex: 1,279; f90: 538; asm: 471; lisp: 375; java: 310; cs: 270; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 110; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22; sed: 2
file content (88 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download | duplicates (2)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */

/*
 * Fuzzer for CMake's Generator Expression parser
 *
 * Generator expressions ($<...>) are evaluated at build-system generation
 * time. This fuzzer targets the lexer and static parsing utilities that don't
 * require full cmake context.
 *
 * Coverage targets:
 * - Generator expression lexer (cmGeneratorExpressionLexer)
 * - Static parsing/preprocessing functions
 * - Nested expression handling
 * - Expression validation
 */

#include <cstddef>
#include <cstdint>
#include <map>
#include <string>
#include <vector>

#include "cmGeneratorExpression.h"
#include "cmGeneratorExpressionLexer.h"

// Limit input size - genex can be exponential in nested cases
static constexpr size_t kMaxInputSize = 16 * 1024; // 16KB

extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
{
  if (size == 0 || size > kMaxInputSize) {
    return 0;
  }

  std::string input(reinterpret_cast<char const*>(data), size);

  // Test the lexer directly
  {
    cmGeneratorExpressionLexer lexer;
    auto tokens = lexer.Tokenize(input);
    (void)tokens;
  }

  // Test static utility functions that don't need cmake context
  {
    // Find generator expressions
    auto pos = cmGeneratorExpression::Find(input);
    (void)pos;

    // Check if starts with genex
    bool starts = cmGeneratorExpression::StartsWithGeneratorExpression(input);
    (void)starts;

    // Validate as target name
    bool valid = cmGeneratorExpression::IsValidTargetName(input);
    (void)valid;

    // Strip empty list elements
    std::string stripped =
      cmGeneratorExpression::StripEmptyListElements(input);
    (void)stripped;

    // Split expressions
    std::vector<std::string> output;
    cmGeneratorExpression::Split(input, output);

    // Preprocess with different contexts
    std::string preprocessed1 = cmGeneratorExpression::Preprocess(
      input, cmGeneratorExpression::StripAllGeneratorExpressions);
    (void)preprocessed1;

    std::string preprocessed2 = cmGeneratorExpression::Preprocess(
      input, cmGeneratorExpression::BuildInterface);
    (void)preprocessed2;

    std::string preprocessed3 = cmGeneratorExpression::Preprocess(
      input, cmGeneratorExpression::InstallInterface);
    (void)preprocessed3;

    // Collect expressions
    std::map<std::string, std::vector<std::string>> collected;
    std::string collResult = cmGeneratorExpression::Collect(input, collected);
    (void)collResult;
  }

  return 0;
}