Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/fuzzer_main.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief Shared execution for profile-split protobuf targets.
9
10
#include "proto_fuzzer/fuzzer_main.h"
11
12
#include <curl/curl.h>
13
14
#include <array>
15
#include <cstdio>
16
17
#include "proto_fuzzer/allocation_fault_injection.h"
18
#include "proto_fuzzer/curl_global_lifecycle.h"
19
#include "proto_fuzzer/scenario_runner.h"
20
#include "proto_fuzzer/target_policy.h"
21
22
#if defined(PROTO_FUZZER_OBSERVE_STATE)
23
/// Invoke this target's libFuzzer mutator from coverage-only corpus replay.
24
/// @param data Serialized profile message buffer to mutate in place.
25
/// @param size Number of input bytes currently stored in `data`.
26
/// @param max_size Capacity of `data` available for the mutated message.
27
/// @param seed Deterministic mutation seed.
28
/// @return Number of serialized bytes in the mutated message.
29
extern "C" std::size_t LLVMFuzzerCustomMutator(std::uint8_t* data, std::size_t size, std::size_t max_size,
30
                                               unsigned int seed);
31
32
/// Invoke this target's libFuzzer crossover from coverage-only corpus replay.
33
/// @param data1 Serialized first parent message.
34
/// @param size1 Number of bytes in `data1`.
35
/// @param data2 Serialized second parent message.
36
/// @param size2 Number of bytes in `data2`.
37
/// @param out Destination buffer for the child message.
38
/// @param max_out_size Capacity of `out`.
39
/// @param seed Deterministic crossover seed.
40
/// @return Number of serialized bytes written to `out`.
41
extern "C" std::size_t LLVMFuzzerCustomCrossOver(const std::uint8_t* data1, std::size_t size1,
42
                                                 const std::uint8_t* data2, std::size_t size2, std::uint8_t* out,
43
                                                 std::size_t max_out_size, unsigned int seed);
44
#endif
45
46
namespace {
47
48
// libFuzzer reuses one process, so the static object initializes curl once and
49
// releases its global resolver/TLS state only after the engine has stopped.
50
proto_fuzzer::CurlGlobalLifecycle kGlobalBootstrap;
51
52
#if defined(PROTO_FUZZER_OBSERVE_STATE)
53
/// Exercise this executable's public mutation ABI once. Corpus replay calls
54
/// only LLVMFuzzerTestOneInput, so otherwise the same-named entrypoint wrappers
55
/// and shared dispatchers remain uncovered even though libFuzzer uses them in
56
/// mutation runs. Empty protobuf inputs are valid messages; fixed seeds and
57
/// bounded buffers keep this coverage-only probe deterministic.
58
421k
void ProbeMutationEntrypoints() {
59
421k
  static const bool mutation_entrypoints_probed = [] {
60
30
    constexpr std::size_t kBufferSize = 1024;
61
30
    constexpr unsigned int kFirstSeed = 0x4355524cU;
62
30
    constexpr unsigned int kSecondSeed = 0x46555a5aU;
63
64
30
    std::array<std::uint8_t, kBufferSize> first = {};
65
30
    std::array<std::uint8_t, kBufferSize> second = {};
66
30
    std::array<std::uint8_t, kBufferSize> child = {};
67
30
    const std::size_t first_size = LLVMFuzzerCustomMutator(first.data(), 0, first.size(), kFirstSeed);
68
30
    const std::size_t second_size = LLVMFuzzerCustomMutator(second.data(), 0, second.size(), kSecondSeed);
69
30
    const std::size_t child_size = LLVMFuzzerCustomCrossOver(first.data(), first_size, second.data(), second_size,
70
30
                                                             child.data(), child.size(), kFirstSeed);
71
30
    const volatile std::size_t result_sizes[] = {first_size, second_size, child_size};
72
30
    (void)result_sizes;
73
30
    return true;
74
30
  }();
75
421k
  (void)mutation_entrypoints_probed;
76
421k
}
77
#endif
78
79
}  // namespace
80
81
namespace proto_fuzzer {
82
83
/// Mutate one serialized generated profile message.
84
/// @param data Serialized profile message buffer to mutate in place.
85
/// @param size Number of input bytes currently stored in `data`.
86
/// @param max_size Capacity of `data` available for the mutated message.
87
/// @param seed Deterministic mutation seed supplied by libFuzzer.
88
/// @param traits Generated profile binding for this executable.
89
/// @return Number of serialized bytes in the mutated message.
90
std::size_t ProtoFuzzerCustomMutator(std::uint8_t* data, std::size_t size, std::size_t max_size, unsigned int seed,
91
60
                                     const ProfileFuzzerTraits& traits) {
92
60
  allocation_fault_injection::Reset();
93
60
  if (traits.custom_mutator == nullptr) {
94
0
    return 0;
95
0
  }
96
60
  return traits.custom_mutator(data, size, max_size, seed);
97
60
}
98
99
/// Cross two serialized generated profile messages of the same concrete type.
100
/// @param data1 Serialized first parent message.
101
/// @param size1 Number of bytes in `data1`.
102
/// @param data2 Serialized second parent message.
103
/// @param size2 Number of bytes in `data2`.
104
/// @param out Destination buffer for the crossed message.
105
/// @param max_out_size Capacity of `out`.
106
/// @param seed Deterministic crossover seed supplied by libFuzzer.
107
/// @param traits Generated profile binding for this executable.
108
/// @return Number of serialized bytes written to `out`.
109
std::size_t ProtoFuzzerCustomCrossOver(const std::uint8_t* data1, std::size_t size1, const std::uint8_t* data2,
110
                                       std::size_t size2, std::uint8_t* out, std::size_t max_out_size,
111
30
                                       unsigned int seed, const ProfileFuzzerTraits& traits) {
112
30
  allocation_fault_injection::Reset();
113
30
  if (traits.custom_crossover == nullptr) {
114
0
    return 0;
115
0
  }
116
30
  return traits.custom_crossover(data1, size1, data2, size2, out, max_out_size, seed);
117
30
}
118
119
/// Load and execute one generated-profile input under its fixed target lane.
120
/// @param data Serialized generated-profile input bytes.
121
/// @param size Number of bytes available at `data`.
122
/// @param traits Generated profile binding selected by the entrypoint.
123
/// @return Always zero, as required by the libFuzzer entrypoint contract.
124
421k
int ProtoFuzzerTestOneInput(const std::uint8_t* data, std::size_t size, const ProfileFuzzerTraits& traits) {
125
421k
  allocation_fault_injection::Reset();
126
421k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
127
421k
  ProbeMutationEntrypoints();
128
421k
  if (traits.probe_mutation_helpers != nullptr) {
129
23.4k
    traits.probe_mutation_helpers();
130
23.4k
  }
131
421k
#endif
132
421k
  curl::fuzzer::proto::Scenario scenario;
133
421k
  if (traits.load_input == nullptr || !traits.load_input(data, size, &scenario)) {
134
5.14k
    return 0;
135
5.14k
  }
136
137
415k
  NormalizeScenarioForExecution(&scenario);
138
415k
  allocation_fault_injection::Configure(scenario.fail_allocations().data(),
139
415k
                                        static_cast<std::size_t>(scenario.fail_allocations_size()));
140
415k
  allocation_fault_injection::Arm();
141
415k
  proto_fuzzer::RunScenario(scenario, RunModeFor(traits.profile));
142
415k
  allocation_fault_injection::Reset();
143
415k
  if (traits.profile == TargetProfile::kResolver &&
144
14.2k
      scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_GLOBAL_LIFECYCLE) {
145
336
    curl_global_cleanup();
146
336
    if (!allocation_fault_injection::ReinitializeCurl()) {
147
0
      std::fputs("warning: libcurl reinitialization failed\n", stderr);
148
0
      (void)curl_global_init(CURL_GLOBAL_ALL);
149
0
    }
150
336
  }
151
415k
  return 0;
152
421k
}
153
154
}  // namespace proto_fuzzer