Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/fuzz_setopt.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
// Direct fuzzing for numeric curl_easy_setopt paths. Each fixed-size input
8
// record selects one option from libcurl's runtime catalog and supplies one
9
// value. The target does not perform a transfer, so mutations can concentrate
10
// on option validation and state changes.
11
12
#include "fuzz_setopt.h"
13
14
#include <algorithm>
15
#include <cstring>
16
#include <limits>
17
#include <type_traits>
18
19
namespace setopt_fuzzer {
20
namespace {
21
22
struct CurlBootstrap {
23
2
  CurlBootstrap() { (void)curl_global_init(CURL_GLOBAL_ALL); }
24
0
  ~CurlBootstrap() { curl_global_cleanup(); }
25
};
26
27
CurlBootstrap kCurlBootstrap;
28
29
312
bool IsNumericType(curl_easytype type) {
30
312
  return type == CURLOT_LONG || type == CURLOT_VALUES || type == CURLOT_OFF_T;
31
312
}
32
33
27.4k
std::uint16_t ReadSelector(const std::uint8_t *record) {
34
27.4k
  return static_cast<std::uint16_t>(record[0]) |
35
27.4k
         static_cast<std::uint16_t>(record[1]) << 8;
36
27.4k
}
37
38
27.4k
std::uint64_t ReadPayload(const std::uint8_t *record) {
39
27.4k
  std::uint64_t value = 0;
40
246k
  for (std::size_t index = 0; index < sizeof(value); ++index)
41
219k
    value |= static_cast<std::uint64_t>(record[3 + index]) << (index * 8);
42
27.4k
  return value;
43
27.4k
}
44
45
16.2k
template <typename T> T FromBits(typename std::make_unsigned<T>::type bits) {
46
16.2k
  static_assert(std::is_integral<T>::value, "setopt value must be integral");
47
16.2k
  T value;
48
16.2k
  std::memcpy(&value, &bits, sizeof(value));
49
16.2k
  return value;
50
16.2k
}
51
52
27.4k
template <typename T> T ValueFor(const DecodedRecord &record) {
53
27.4k
  using Unsigned = typename std::make_unsigned<T>::type;
54
27.4k
  constexpr std::size_t kValueBits = std::numeric_limits<Unsigned>::digits;
55
27.4k
  const Unsigned payload = static_cast<Unsigned>(record.payload);
56
27.4k
  const Unsigned one_bit = static_cast<Unsigned>(1)
57
27.4k
                           << (record.payload % kValueBits);
58
59
27.4k
  switch (record.mode) {
60
9.54k
  case ValueMode::kRaw:
61
9.54k
    return FromBits<T>(payload);
62
3.07k
  case ValueMode::kZero:
63
3.07k
    return static_cast<T>(0);
64
2.18k
  case ValueMode::kOne:
65
2.18k
    return static_cast<T>(1);
66
1.85k
  case ValueMode::kMinusOne:
67
1.85k
    return static_cast<T>(-1);
68
1.94k
  case ValueMode::kNativeMinimum:
69
1.94k
    return std::numeric_limits<T>::min();
70
2.09k
  case ValueMode::kNativeMaximum:
71
2.09k
    return std::numeric_limits<T>::max();
72
3.03k
  case ValueMode::kOneBit:
73
3.03k
    return FromBits<T>(one_bit);
74
3.70k
  case ValueMode::kInvertedOneBit:
75
3.70k
    return FromBits<T>(static_cast<Unsigned>(~one_bit));
76
27.4k
  }
77
0
  return static_cast<T>(0);
78
27.4k
}
79
80
27.4k
void ApplyRecord(CURL *easy, const DecodedRecord &record) {
81
27.4k
  switch (record.option.type) {
82
20.4k
  case CURLOT_LONG:
83
25.5k
  case CURLOT_VALUES:
84
25.5k
    (void)curl_easy_setopt(easy, record.option.id, ValueFor<long>(record));
85
25.5k
    break;
86
1.90k
  case CURLOT_OFF_T:
87
1.90k
    (void)curl_easy_setopt(easy, record.option.id,
88
1.90k
                           ValueFor<curl_off_t>(record));
89
1.90k
    break;
90
0
  default:
91
0
    break;
92
27.4k
  }
93
27.4k
}
94
95
} // namespace
96
97
9.98k
const std::vector<EligibleOption> &EligibleOptions() {
98
9.98k
  static const std::vector<EligibleOption> options = [] {
99
1
    std::vector<EligibleOption> result;
100
1
    const struct curl_easyoption *option = nullptr;
101
328
    while ((option = curl_easy_option_next(option)) != nullptr) {
102
327
      if ((option->flags & CURLOT_FLAG_ALIAS) == 0 &&
103
312
          IsNumericType(option->type)) {
104
141
        result.push_back({option->id, option->type});
105
141
      }
106
327
    }
107
108
1
    std::sort(result.begin(), result.end(),
109
1.14k
              [](const EligibleOption &left, const EligibleOption &right) {
110
1.14k
                return left.id < right.id;
111
1.14k
              });
112
1
    result.erase(std::unique(result.begin(), result.end(),
113
1
                             [](const EligibleOption &left,
114
140
                                const EligibleOption &right) {
115
140
                               return left.id == right.id;
116
140
                             }),
117
1
                 result.end());
118
1
    return result;
119
1
  }();
120
9.98k
  return options;
121
9.98k
}
122
123
std::vector<DecodedRecord> DecodeRecords(const std::uint8_t *data,
124
9.98k
                                         std::size_t size) {
125
9.98k
  std::vector<DecodedRecord> records;
126
9.98k
  const std::vector<EligibleOption> &options = EligibleOptions();
127
9.98k
  if (data == nullptr || options.empty())
128
0
    return records;
129
130
9.98k
  const std::size_t count = std::min(size / kRecordSize, kMaxRecords);
131
9.98k
  records.reserve(count);
132
37.4k
  for (std::size_t index = 0; index < count; ++index) {
133
27.4k
    const std::uint8_t *record = data + index * kRecordSize;
134
27.4k
    const std::size_t option_index = ReadSelector(record) % options.size();
135
27.4k
    records.push_back({options[option_index],
136
27.4k
                       static_cast<ValueMode>(record[2] & 7U),
137
27.4k
                       ReadPayload(record)});
138
27.4k
  }
139
9.98k
  return records;
140
9.98k
}
141
142
9.98k
int TestOneInput(const std::uint8_t *data, std::size_t size) {
143
9.98k
  CURL *easy = curl_easy_init();
144
9.98k
  if (easy == nullptr)
145
0
    return 0;
146
147
9.98k
  for (const DecodedRecord &record : DecodeRecords(data, size))
148
27.4k
    ApplyRecord(easy, record);
149
150
9.98k
  curl_easy_cleanup(easy);
151
9.98k
  return 0;
152
9.98k
}
153
154
} // namespace setopt_fuzzer
155
156
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t *data,
157
435k
                                      std::size_t size) {
158
435k
  return setopt_fuzzer::TestOneInput(data, size);
159
435k
}