Coverage Report

Created: 2024-09-19 18:47

/root/bitcoin/src/test/fuzz/FuzzedDataProvider.h
Line
Count
Source (jump to first uncovered line)
1
//===- FuzzedDataProvider.h - Utility header for fuzz targets ---*- C++ -* ===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
// A single header library providing an utility class to break up an array of
9
// bytes. Whenever run on the same input, provides the same output, as long as
10
// its methods are called in the same order, with the same arguments.
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
14
#define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
15
16
#include <algorithm>
17
#include <array>
18
#include <climits>
19
#include <cstddef>
20
#include <cstdint>
21
#include <cstring>
22
#include <initializer_list>
23
#include <limits>
24
#include <string>
25
#include <type_traits>
26
#include <utility>
27
#include <vector>
28
29
// In addition to the comments below, the API is also briefly documented at
30
// https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider
31
class FuzzedDataProvider {
32
 public:
33
  // |data| is an array of length |size| that the FuzzedDataProvider wraps to
34
  // provide more granular access. |data| must outlive the FuzzedDataProvider.
35
  FuzzedDataProvider(const uint8_t *data, size_t size)
36
240
      : data_ptr_(data), remaining_bytes_(size) {}
37
  ~FuzzedDataProvider() = default;
38
39
  // See the implementation below (after the class definition) for more verbose
40
  // comments for each of the methods.
41
42
  // Methods returning std::vector of bytes. These are the most popular choice
43
  // when splitting fuzzing input into pieces, as every piece is put into a
44
  // separate buffer (i.e. ASan would catch any under-/overflow) and the memory
45
  // will be released automatically.
46
  template <typename T> std::vector<T> ConsumeBytes(size_t num_bytes);
47
  template <typename T>
48
  std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, T terminator = 0);
49
  template <typename T> std::vector<T> ConsumeRemainingBytes();
50
51
  // Methods returning strings. Use only when you need a std::string or a null
52
  // terminated C-string. Otherwise, prefer the methods returning std::vector.
53
  std::string ConsumeBytesAsString(size_t num_bytes);
54
  std::string ConsumeRandomLengthString(size_t max_length);
55
  std::string ConsumeRandomLengthString();
56
  std::string ConsumeRemainingBytesAsString();
57
58
  // Methods returning integer values.
59
  template <typename T> T ConsumeIntegral();
60
  template <typename T> T ConsumeIntegralInRange(T min, T max);
61
62
  // Methods returning floating point values.
63
  template <typename T> T ConsumeFloatingPoint();
64
  template <typename T> T ConsumeFloatingPointInRange(T min, T max);
65
66
  // 0 <= return value <= 1.
67
  template <typename T> T ConsumeProbability();
68
69
  bool ConsumeBool();
70
71
  // Returns a value chosen from the given enum.
72
  template <typename T> T ConsumeEnum();
73
74
  // Returns a value from the given array.
75
  template <typename T, size_t size> T PickValueInArray(const T (&array)[size]);
76
  template <typename T, size_t size>
77
  T PickValueInArray(const std::array<T, size> &array);
78
  template <typename T> T PickValueInArray(std::initializer_list<const T> list);
79
80
  // Writes data to the given destination and returns number of bytes written.
81
  size_t ConsumeData(void *destination, size_t num_bytes);
82
83
  // Reports the remaining bytes available for fuzzed input.
84
0
  size_t remaining_bytes() { return remaining_bytes_; }
85
86
 private:
87
  FuzzedDataProvider(const FuzzedDataProvider &) = delete;
88
  FuzzedDataProvider &operator=(const FuzzedDataProvider &) = delete;
89
90
  void CopyAndAdvance(void *destination, size_t num_bytes);
91
92
  void Advance(size_t num_bytes);
93
94
  template <typename T>
95
  std::vector<T> ConsumeBytes(size_t size, size_t num_bytes);
96
97
  template <typename TS, typename TU> TS ConvertUnsignedToSigned(TU value);
98
99
  const uint8_t *data_ptr_;
100
  size_t remaining_bytes_;
101
};
102
103
// Returns a std::vector containing |num_bytes| of input data. If fewer than
104
// |num_bytes| of data remain, returns a shorter std::vector containing all
105
// of the data that's left. Can be used with any byte sized type, such as
106
// char, unsigned char, uint8_t, etc.
107
template <typename T>
108
0
std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) {
109
0
  num_bytes = std::min(num_bytes, remaining_bytes_);
110
0
  return ConsumeBytes<T>(num_bytes, num_bytes);
111
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider12ConsumeBytesIhEESt6vectorIT_SaIS2_EEm
Unexecuted instantiation: _ZN18FuzzedDataProvider12ConsumeBytesISt4byteEESt6vectorIT_SaIS3_EEm
112
113
// Similar to |ConsumeBytes|, but also appends the terminator value at the end
114
// of the resulting vector. Useful, when a mutable null-terminated C-string is
115
// needed, for example. But that is a rare case. Better avoid it, if possible,
116
// and prefer using |ConsumeBytes| or |ConsumeBytesAsString| methods.
117
template <typename T>
118
std::vector<T> FuzzedDataProvider::ConsumeBytesWithTerminator(size_t num_bytes,
119
                                                              T terminator) {
120
  num_bytes = std::min(num_bytes, remaining_bytes_);
121
  std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes);
122
  result.back() = terminator;
123
  return result;
124
}
125
126
// Returns a std::vector containing all remaining bytes of the input data.
127
template <typename T>
128
0
std::vector<T> FuzzedDataProvider::ConsumeRemainingBytes() {
129
0
  return ConsumeBytes<T>(remaining_bytes_);
130
0
}
131
132
// Returns a std::string containing |num_bytes| of input data. Using this and
133
// |.c_str()| on the resulting string is the best way to get an immutable
134
// null-terminated C string. If fewer than |num_bytes| of data remain, returns
135
// a shorter std::string containing all of the data that's left.
136
0
inline std::string FuzzedDataProvider::ConsumeBytesAsString(size_t num_bytes) {
137
0
  static_assert(sizeof(std::string::value_type) == sizeof(uint8_t),
138
0
                "ConsumeBytesAsString cannot convert the data to a string.");
139
140
0
  num_bytes = std::min(num_bytes, remaining_bytes_);
141
0
  std::string result(
142
0
      reinterpret_cast<const std::string::value_type *>(data_ptr_), num_bytes);
143
0
  Advance(num_bytes);
144
0
  return result;
145
0
}
146
147
// Returns a std::string of length from 0 to |max_length|. When it runs out of
148
// input data, returns what remains of the input. Designed to be more stable
149
// with respect to a fuzzer inserting characters than just picking a random
150
// length and then consuming that many bytes with |ConsumeBytes|.
151
inline std::string
152
0
FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) {
153
  // Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\"
154
  // followed by anything else to the end of the string. As a result of this
155
  // logic, a fuzzer can insert characters into the string, and the string
156
  // will be lengthened to include those new characters, resulting in a more
157
  // stable fuzzer than picking the length of a string independently from
158
  // picking its contents.
159
0
  std::string result;
160
161
  // Reserve the anticipated capacity to prevent several reallocations.
162
0
  result.reserve(std::min(max_length, remaining_bytes_));
163
0
  for (size_t i = 0; i < max_length && remaining_bytes_ != 0; ++i) {
164
0
    char next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
165
0
    Advance(1);
166
0
    if (next == '\\' && remaining_bytes_ != 0) {
167
0
      next = ConvertUnsignedToSigned<char>(data_ptr_[0]);
168
0
      Advance(1);
169
0
      if (next != '\\')
170
0
        break;
171
0
    }
172
0
    result += next;
173
0
  }
174
175
0
  result.shrink_to_fit();
176
0
  return result;
177
0
}
178
179
// Returns a std::string of length from 0 to |remaining_bytes_|.
180
0
inline std::string FuzzedDataProvider::ConsumeRandomLengthString() {
181
0
  return ConsumeRandomLengthString(remaining_bytes_);
182
0
}
183
184
// Returns a std::string containing all remaining bytes of the input data.
185
// Prefer using |ConsumeRemainingBytes| unless you actually need a std::string
186
// object.
187
0
inline std::string FuzzedDataProvider::ConsumeRemainingBytesAsString() {
188
0
  return ConsumeBytesAsString(remaining_bytes_);
189
0
}
190
191
// Returns a number in the range [Type's min, Type's max]. The value might
192
// not be uniformly distributed in the given range. If there's no input data
193
// left, always returns |min|.
194
103k
template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
195
103k
  return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
196
103k
                                std::numeric_limits<T>::max());
197
103k
}
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralIlEET_v
_ZN18FuzzedDataProvider15ConsumeIntegralImEET_v
Line
Count
Source
194
3.42k
template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
195
3.42k
  return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
196
3.42k
                                std::numeric_limits<T>::max());
197
3.42k
}
_ZN18FuzzedDataProvider15ConsumeIntegralIiEET_v
Line
Count
Source
194
45.1k
template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
195
45.1k
  return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
196
45.1k
                                std::numeric_limits<T>::max());
197
45.1k
}
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralIjEET_v
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralIsEET_v
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralItEET_v
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralIcEET_v
_ZN18FuzzedDataProvider15ConsumeIntegralIhEET_v
Line
Count
Source
194
55.0k
template <typename T> T FuzzedDataProvider::ConsumeIntegral() {
195
55.0k
  return ConsumeIntegralInRange(std::numeric_limits<T>::min(),
196
55.0k
                                std::numeric_limits<T>::max());
197
55.0k
}
Unexecuted instantiation: _ZN18FuzzedDataProvider15ConsumeIntegralIaEET_v
198
199
// Returns a number in the range [min, max] by consuming bytes from the
200
// input data. The value might not be uniformly distributed in the given
201
// range. If there's no input data left, always returns |min|. |min| must
202
// be less than or equal to |max|.
203
template <typename T>
204
170k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
170k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
170k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
170k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
170k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
170k
  uint64_t result = 0;
214
170k
  size_t offset = 0;
215
216
631k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
631k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
460k
    --remaining_bytes_;
225
460k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
460k
    offset += CHAR_BIT;
227
460k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
170k
  if (range != std::numeric_limits<decltype(range)>::max())
231
167k
    result = result % (range + 1);
232
233
170k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
170k
}
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIlEET_S1_S1_
Line
Count
Source
204
45.1k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
45.1k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
45.1k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
45.1k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
45.1k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
45.1k
  uint64_t result = 0;
214
45.1k
  size_t offset = 0;
215
216
222k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
222k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
177k
    --remaining_bytes_;
225
177k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
177k
    offset += CHAR_BIT;
227
177k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
45.1k
  if (range != std::numeric_limits<decltype(range)>::max())
231
45.1k
    result = result % (range + 1);
232
233
45.1k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
45.1k
}
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeImEET_S1_S1_
Line
Count
Source
204
22.9k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
22.9k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
22.9k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
22.9k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
22.9k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
22.9k
  uint64_t result = 0;
214
22.9k
  size_t offset = 0;
215
216
69.6k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
69.6k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
46.6k
    --remaining_bytes_;
225
46.6k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
46.6k
    offset += CHAR_BIT;
227
46.6k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
22.9k
  if (range != std::numeric_limits<decltype(range)>::max())
231
19.5k
    result = result % (range + 1);
232
233
22.9k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
22.9k
}
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIiEET_S1_S1_
Line
Count
Source
204
45.1k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
45.1k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
45.1k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
45.1k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
45.1k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
45.1k
  uint64_t result = 0;
214
45.1k
  size_t offset = 0;
215
216
222k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
222k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
177k
    --remaining_bytes_;
225
177k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
177k
    offset += CHAR_BIT;
227
177k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
45.1k
  if (range != std::numeric_limits<decltype(range)>::max())
231
45.1k
    result = result % (range + 1);
232
233
45.1k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
45.1k
}
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIjEET_S1_S1_
Line
Count
Source
204
2.20k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
2.20k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
2.20k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
2.20k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
2.20k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
2.20k
  uint64_t result = 0;
214
2.20k
  size_t offset = 0;
215
216
7.90k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
7.90k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
5.69k
    --remaining_bytes_;
225
5.69k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
5.69k
    offset += CHAR_BIT;
227
5.69k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
2.20k
  if (range != std::numeric_limits<decltype(range)>::max())
231
2.20k
    result = result % (range + 1);
232
233
2.20k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
2.20k
}
Unexecuted instantiation: _ZN18FuzzedDataProvider22ConsumeIntegralInRangeIsEET_S1_S1_
Unexecuted instantiation: _ZN18FuzzedDataProvider22ConsumeIntegralInRangeItEET_S1_S1_
Unexecuted instantiation: _ZN18FuzzedDataProvider22ConsumeIntegralInRangeIcEET_S1_S1_
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIhEET_S1_S1_
Line
Count
Source
204
55.0k
T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
205
55.0k
  static_assert(std::is_integral<T>::value, "An integral type is required.");
206
55.0k
  static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type.");
207
208
55.0k
  if (min > max)
209
0
    abort();
210
211
  // Use the biggest type possible to hold the range and the result.
212
55.0k
  uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
213
55.0k
  uint64_t result = 0;
214
55.0k
  size_t offset = 0;
215
216
109k
  while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 &&
217
109k
         remaining_bytes_ != 0) {
218
    // Pull bytes off the end of the seed data. Experimentally, this seems to
219
    // allow the fuzzer to more easily explore the input space. This makes
220
    // sense, since it works by modifying inputs that caused new code to run,
221
    // and this data is often used to encode length of data read by
222
    // |ConsumeBytes|. Separating out read lengths makes it easier modify the
223
    // contents of the data that is actually read.
224
54.1k
    --remaining_bytes_;
225
54.1k
    result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_];
226
54.1k
    offset += CHAR_BIT;
227
54.1k
  }
228
229
  // Avoid division by 0, in case |range + 1| results in overflow.
230
55.0k
  if (range != std::numeric_limits<decltype(range)>::max())
231
55.0k
    result = result % (range + 1);
232
233
55.0k
  return static_cast<T>(static_cast<uint64_t>(min) + result);
234
55.0k
}
Unexecuted instantiation: _ZN18FuzzedDataProvider22ConsumeIntegralInRangeIaEET_S1_S1_
235
236
// Returns a floating point value in the range [Type's lowest, Type's max] by
237
// consuming bytes from the input data. If there's no input data left, always
238
// returns approximately 0.
239
0
template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() {
240
0
  return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(),
241
0
                                        std::numeric_limits<T>::max());
242
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider20ConsumeFloatingPointIdEET_v
Unexecuted instantiation: _ZN18FuzzedDataProvider20ConsumeFloatingPointIfEET_v
243
244
// Returns a floating point value in the given range by consuming bytes from
245
// the input data. If there's no input data left, returns |min|. Note that
246
// |min| must be less than or equal to |max|.
247
template <typename T>
248
0
T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) {
249
0
  if (min > max)
250
0
    abort();
251
252
0
  T range = .0;
253
0
  T result = min;
254
0
  constexpr T zero(.0);
255
0
  if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) {
256
    // The diff |max - min| would overflow the given floating point type. Use
257
    // the half of the diff as the range and consume a bool to decide whether
258
    // the result is in the first of the second part of the diff.
259
0
    range = (max / 2.0) - (min / 2.0);
260
0
    if (ConsumeBool()) {
261
0
      result += range;
262
0
    }
263
0
  } else {
264
0
    range = max - min;
265
0
  }
266
267
0
  return result + range * ConsumeProbability<T>();
268
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider27ConsumeFloatingPointInRangeIdEET_S1_S1_
Unexecuted instantiation: _ZN18FuzzedDataProvider27ConsumeFloatingPointInRangeIfEET_S1_S1_
269
270
// Returns a floating point number in the range [0.0, 1.0]. If there's no
271
// input data left, always returns 0.
272
0
template <typename T> T FuzzedDataProvider::ConsumeProbability() {
273
0
  static_assert(std::is_floating_point<T>::value,
274
0
                "A floating point type is required.");
275
276
  // Use different integral types for different floating point types in order
277
  // to provide better density of the resulting values.
278
0
  using IntegralType =
279
0
      typename std::conditional<(sizeof(T) <= sizeof(uint32_t)), uint32_t,
280
0
                                uint64_t>::type;
281
282
0
  T result = static_cast<T>(ConsumeIntegral<IntegralType>());
283
0
  result /= static_cast<T>(std::numeric_limits<IntegralType>::max());
284
0
  return result;
285
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider18ConsumeProbabilityIdEET_v
Unexecuted instantiation: _ZN18FuzzedDataProvider18ConsumeProbabilityIfEET_v
286
287
// Reads one byte and returns a bool, or false when no data remains.
288
55.0k
inline bool FuzzedDataProvider::ConsumeBool() {
289
55.0k
  return 1 & ConsumeIntegral<uint8_t>();
290
55.0k
}
291
292
// Returns an enum value. The enum must start at 0 and be contiguous. It must
293
// also contain |kMaxValue| aliased to its largest (inclusive) value. Such as:
294
// enum class Foo { SomeValue, OtherValue, kMaxValue = OtherValue };
295
template <typename T> T FuzzedDataProvider::ConsumeEnum() {
296
  static_assert(std::is_enum<T>::value, "|T| must be an enum type.");
297
  return static_cast<T>(
298
      ConsumeIntegralInRange<uint32_t>(0, static_cast<uint32_t>(T::kMaxValue)));
299
}
300
301
// Returns a copy of the value selected from the given fixed-size |array|.
302
template <typename T, size_t size>
303
0
T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) {
304
0
  static_assert(size > 0, "The array must be non empty.");
305
0
  return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
306
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI12ServiceFlagsLm7EEET_RAT0__KS2_
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI14ConnectionTypeLm6EEET_RAT0__KS2_
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI18NetPermissionFlagsLm10EEET_RAT0__KS2_
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIN4node16TransactionErrorELm5EEET_RAT0__KS3_
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELm749EEET_RAT0__KS7_
307
308
template <typename T, size_t size>
309
0
T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) {
310
0
  static_assert(size > 0, "The array must be non empty.");
311
0
  return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
312
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI7NetworkLm7EEET_RKSt5arrayIS2_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI18FeeEstimateHorizonLm3EEET_RKSt5arrayIS2_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI10OutputTypeLm4EEET_RKSt5arrayIS2_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm18EEET_RKSt5arrayIS1_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm10EEET_RKSt5arrayIS1_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm8EEET_RKSt5arrayIS1_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm4EEET_RKSt5arrayIS1_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm3EEET_RKSt5arrayIS1_XT0_EE
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiLm2EEET_RKSt5arrayIS1_XT0_EE
313
314
template <typename T>
315
0
T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) {
316
  // TODO(Dor1s): switch to static_assert once C++14 is allowed.
317
0
  if (!list.size())
318
0
    abort();
319
320
0
  return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
321
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI10bloomflagsEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI11BlockStatusEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI19ConnectionDirectionEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI9FeeReasonEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIdEET_St16initializer_listIKS1_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI13SigningResultEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI21BlockValidationResultEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI10SigVersionEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayI15OptionsCategoryEET_St16initializer_listIKS2_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIiEET_St16initializer_listIKS1_E
Unexecuted instantiation: _ZN18FuzzedDataProvider16PickValueInArrayIjEET_St16initializer_listIKS1_E
322
323
// Writes |num_bytes| of input data to the given destination pointer. If there
324
// is not enough data left, writes all remaining bytes. Return value is the
325
// number of bytes written.
326
// In general, it's better to avoid using this function, but it may be useful
327
// in cases when it's necessary to fill a certain buffer or object with
328
// fuzzing data.
329
inline size_t FuzzedDataProvider::ConsumeData(void *destination,
330
0
                                              size_t num_bytes) {
331
0
  num_bytes = std::min(num_bytes, remaining_bytes_);
332
0
  CopyAndAdvance(destination, num_bytes);
333
0
  return num_bytes;
334
0
}
335
336
// Private methods.
337
inline void FuzzedDataProvider::CopyAndAdvance(void *destination,
338
0
                                               size_t num_bytes) {
339
0
  std::memcpy(destination, data_ptr_, num_bytes);
340
0
  Advance(num_bytes);
341
0
}
342
343
0
inline void FuzzedDataProvider::Advance(size_t num_bytes) {
344
0
  if (num_bytes > remaining_bytes_)
345
0
    abort();
346
347
0
  data_ptr_ += num_bytes;
348
0
  remaining_bytes_ -= num_bytes;
349
0
}
350
351
template <typename T>
352
0
std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) {
353
0
  static_assert(sizeof(T) == sizeof(uint8_t), "Incompatible data type.");
354
355
  // The point of using the size-based constructor below is to increase the
356
  // odds of having a vector object with capacity being equal to the length.
357
  // That part is always implementation specific, but at least both libc++ and
358
  // libstdc++ allocate the requested number of bytes in that constructor,
359
  // which seems to be a natural choice for other implementations as well.
360
  // To increase the odds even more, we also call |shrink_to_fit| below.
361
0
  std::vector<T> result(size);
362
0
  if (size == 0) {
363
0
    if (num_bytes != 0)
364
0
      abort();
365
0
    return result;
366
0
  }
367
368
0
  CopyAndAdvance(result.data(), num_bytes);
369
370
  // Even though |shrink_to_fit| is also implementation specific, we expect it
371
  // to provide an additional assurance in case vector's constructor allocated
372
  // a buffer which is larger than the actual amount of data we put inside it.
373
0
  result.shrink_to_fit();
374
0
  return result;
375
0
}
Unexecuted instantiation: _ZN18FuzzedDataProvider12ConsumeBytesIhEESt6vectorIT_SaIS2_EEmm
Unexecuted instantiation: _ZN18FuzzedDataProvider12ConsumeBytesISt4byteEESt6vectorIT_SaIS3_EEmm
376
377
template <typename TS, typename TU>
378
0
TS FuzzedDataProvider::ConvertUnsignedToSigned(TU value) {
379
0
  static_assert(sizeof(TS) == sizeof(TU), "Incompatible data types.");
380
0
  static_assert(!std::numeric_limits<TU>::is_signed,
381
0
                "Source type must be unsigned.");
382
383
  // TODO(Dor1s): change to `if constexpr` once C++17 becomes mainstream.
384
0
  if (std::numeric_limits<TS>::is_modulo)
385
0
    return static_cast<TS>(value);
386
387
  // Avoid using implementation-defined unsigned to signed conversions.
388
  // To learn more, see https://stackoverflow.com/questions/13150449.
389
0
  if (value <= std::numeric_limits<TS>::max()) {
390
0
    return static_cast<TS>(value);
391
0
  } else {
392
0
    constexpr auto TS_min = std::numeric_limits<TS>::min();
393
0
    return TS_min + static_cast<TS>(value - TS_min);
394
0
  }
395
0
}
396
397
#endif // LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_