/root/bitcoin/src/test/fuzz/FuzzedDataProvider.h
Line | Count | Source |
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 <cstdlib> |
22 | | #include <cstring> |
23 | | #include <initializer_list> |
24 | | #include <limits> |
25 | | #include <string> |
26 | | #include <type_traits> |
27 | | #include <utility> |
28 | | #include <vector> |
29 | | |
30 | | // In addition to the comments below, the API is also briefly documented at |
31 | | // https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider |
32 | | class FuzzedDataProvider { |
33 | | public: |
34 | | // |data| is an array of length |size| that the FuzzedDataProvider wraps to |
35 | | // provide more granular access. |data| must outlive the FuzzedDataProvider. |
36 | | FuzzedDataProvider(const uint8_t *data, size_t size) |
37 | 247k | : data_ptr_(data), remaining_bytes_(size) {} |
38 | | ~FuzzedDataProvider() = default; |
39 | | |
40 | | // See the implementation below (after the class definition) for more verbose |
41 | | // comments for each of the methods. |
42 | | |
43 | | // Methods returning std::vector of bytes. These are the most popular choice |
44 | | // when splitting fuzzing input into pieces, as every piece is put into a |
45 | | // separate buffer (i.e. ASan would catch any under-/overflow) and the memory |
46 | | // will be released automatically. |
47 | | template <typename T> std::vector<T> ConsumeBytes(size_t num_bytes); |
48 | | template <typename T> |
49 | | std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, T terminator = 0); |
50 | | template <typename T> std::vector<T> ConsumeRemainingBytes(); |
51 | | |
52 | | // Methods returning strings. Use only when you need a std::string or a null |
53 | | // terminated C-string. Otherwise, prefer the methods returning std::vector. |
54 | | std::string ConsumeBytesAsString(size_t num_bytes); |
55 | | std::string ConsumeRandomLengthString(size_t max_length); |
56 | | std::string ConsumeRandomLengthString(); |
57 | | std::string ConsumeRemainingBytesAsString(); |
58 | | |
59 | | // Methods returning integer values. |
60 | | template <typename T> T ConsumeIntegral(); |
61 | | template <typename T> T ConsumeIntegralInRange(T min, T max); |
62 | | |
63 | | // Methods returning floating point values. |
64 | | template <typename T> T ConsumeFloatingPoint(); |
65 | | template <typename T> T ConsumeFloatingPointInRange(T min, T max); |
66 | | |
67 | | // 0 <= return value <= 1. |
68 | | template <typename T> T ConsumeProbability(); |
69 | | |
70 | | bool ConsumeBool(); |
71 | | |
72 | | // Returns a value chosen from the given enum. |
73 | | template <typename T> T ConsumeEnum(); |
74 | | |
75 | | // Returns a value from the given array. |
76 | | template <typename T, size_t size> T PickValueInArray(const T (&array)[size]); |
77 | | template <typename T, size_t size> |
78 | | T PickValueInArray(const std::array<T, size> &array); |
79 | | template <typename T> T PickValueInArray(std::initializer_list<const T> list); |
80 | | |
81 | | // Writes data to the given destination and returns number of bytes written. |
82 | | size_t ConsumeData(void *destination, size_t num_bytes); |
83 | | |
84 | | // Reports the remaining bytes available for fuzzed input. |
85 | 34.1M | size_t remaining_bytes() { return remaining_bytes_; } |
86 | | |
87 | | private: |
88 | | FuzzedDataProvider(const FuzzedDataProvider &) = delete; |
89 | | FuzzedDataProvider &operator=(const FuzzedDataProvider &) = delete; |
90 | | |
91 | | void CopyAndAdvance(void *destination, size_t num_bytes); |
92 | | |
93 | | void Advance(size_t num_bytes); |
94 | | |
95 | | template <typename T> |
96 | | std::vector<T> ConsumeBytes(size_t size, size_t num_bytes); |
97 | | |
98 | | template <typename TS, typename TU> TS ConvertUnsignedToSigned(TU value); |
99 | | |
100 | | const uint8_t *data_ptr_; |
101 | | size_t remaining_bytes_; |
102 | | }; |
103 | | |
104 | | // Returns a std::vector containing |num_bytes| of input data. If fewer than |
105 | | // |num_bytes| of data remain, returns a shorter std::vector containing all |
106 | | // of the data that's left. Can be used with any byte sized type, such as |
107 | | // char, unsigned char, uint8_t, etc. |
108 | | template <typename T> |
109 | 10.2M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) { |
110 | 10.2M | num_bytes = std::min(num_bytes, remaining_bytes_); |
111 | 10.2M | return ConsumeBytes<T>(num_bytes, num_bytes); |
112 | 10.2M | } _ZN18FuzzedDataProvider12ConsumeBytesIhEESt6vectorIT_SaIS2_EEm Line | Count | Source | 109 | 9.17M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) { | 110 | 9.17M | num_bytes = std::min(num_bytes, remaining_bytes_); | 111 | 9.17M | return ConsumeBytes<T>(num_bytes, num_bytes); | 112 | 9.17M | } |
_ZN18FuzzedDataProvider12ConsumeBytesISt4byteEESt6vectorIT_SaIS3_EEm Line | Count | Source | 109 | 1.10M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t num_bytes) { | 110 | 1.10M | num_bytes = std::min(num_bytes, remaining_bytes_); | 111 | 1.10M | return ConsumeBytes<T>(num_bytes, num_bytes); | 112 | 1.10M | } |
|
113 | | |
114 | | // Similar to |ConsumeBytes|, but also appends the terminator value at the end |
115 | | // of the resulting vector. Useful, when a mutable null-terminated C-string is |
116 | | // needed, for example. But that is a rare case. Better avoid it, if possible, |
117 | | // and prefer using |ConsumeBytes| or |ConsumeBytesAsString| methods. |
118 | | template <typename T> |
119 | | std::vector<T> FuzzedDataProvider::ConsumeBytesWithTerminator(size_t num_bytes, |
120 | | T terminator) { |
121 | | num_bytes = std::min(num_bytes, remaining_bytes_); |
122 | | std::vector<T> result = ConsumeBytes<T>(num_bytes + 1, num_bytes); |
123 | | result.back() = terminator; |
124 | | return result; |
125 | | } |
126 | | |
127 | | // Returns a std::vector containing all remaining bytes of the input data. |
128 | | template <typename T> |
129 | 2.65k | std::vector<T> FuzzedDataProvider::ConsumeRemainingBytes() { |
130 | 2.65k | return ConsumeBytes<T>(remaining_bytes_); |
131 | 2.65k | } |
132 | | |
133 | | // Returns a std::string containing |num_bytes| of input data. Using this and |
134 | | // |.c_str()| on the resulting string is the best way to get an immutable |
135 | | // null-terminated C string. If fewer than |num_bytes| of data remain, returns |
136 | | // a shorter std::string containing all of the data that's left. |
137 | 428k | inline std::string FuzzedDataProvider::ConsumeBytesAsString(size_t num_bytes) { |
138 | 428k | static_assert(sizeof(std::string::value_type) == sizeof(uint8_t), |
139 | 428k | "ConsumeBytesAsString cannot convert the data to a string."); |
140 | | |
141 | 428k | num_bytes = std::min(num_bytes, remaining_bytes_); |
142 | 428k | std::string result( |
143 | 428k | reinterpret_cast<const std::string::value_type *>(data_ptr_), num_bytes); |
144 | 428k | Advance(num_bytes); |
145 | 428k | return result; |
146 | 428k | } |
147 | | |
148 | | // Returns a std::string of length from 0 to |max_length|. When it runs out of |
149 | | // input data, returns what remains of the input. Designed to be more stable |
150 | | // with respect to a fuzzer inserting characters than just picking a random |
151 | | // length and then consuming that many bytes with |ConsumeBytes|. |
152 | | inline std::string |
153 | 7.86M | FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) { |
154 | | // Reads bytes from the start of |data_ptr_|. Maps "\\" to "\", and maps "\" |
155 | | // followed by anything else to the end of the string. As a result of this |
156 | | // logic, a fuzzer can insert characters into the string, and the string |
157 | | // will be lengthened to include those new characters, resulting in a more |
158 | | // stable fuzzer than picking the length of a string independently from |
159 | | // picking its contents. |
160 | 7.86M | std::string result; |
161 | | |
162 | | // Reserve the anticipated capacity to prevent several reallocations. |
163 | 7.86M | result.reserve(std::min(max_length, remaining_bytes_)); |
164 | 1.72G | for (size_t i = 0; i < max_length && remaining_bytes_ != 0; ++i) { Branch (164:22): [True: 1.72G, False: 1.68M]
Branch (164:40): [True: 1.72G, False: 96.5k]
|
165 | 1.72G | char next = ConvertUnsignedToSigned<char>(data_ptr_[0]); |
166 | 1.72G | Advance(1); |
167 | 1.72G | if (next == '\\' && remaining_bytes_ != 0) { Branch (167:9): [True: 7.29M, False: 1.71G]
Branch (167:25): [True: 7.29M, False: 1.30k]
|
168 | 7.29M | next = ConvertUnsignedToSigned<char>(data_ptr_[0]); |
169 | 7.29M | Advance(1); |
170 | 7.29M | if (next != '\\') Branch (170:11): [True: 6.08M, False: 1.21M]
|
171 | 6.08M | break; |
172 | 7.29M | } |
173 | 1.71G | result += next; |
174 | 1.71G | } |
175 | | |
176 | 7.86M | result.shrink_to_fit(); |
177 | 7.86M | return result; |
178 | 7.86M | } |
179 | | |
180 | | // Returns a std::string of length from 0 to |remaining_bytes_|. |
181 | 3.65M | inline std::string FuzzedDataProvider::ConsumeRandomLengthString() { |
182 | 3.65M | return ConsumeRandomLengthString(remaining_bytes_); |
183 | 3.65M | } |
184 | | |
185 | | // Returns a std::string containing all remaining bytes of the input data. |
186 | | // Prefer using |ConsumeRemainingBytes| unless you actually need a std::string |
187 | | // object. |
188 | 192 | inline std::string FuzzedDataProvider::ConsumeRemainingBytesAsString() { |
189 | 192 | return ConsumeBytesAsString(remaining_bytes_); |
190 | 192 | } |
191 | | |
192 | | // Returns a number in the range [Type's min, Type's max]. The value might |
193 | | // not be uniformly distributed in the given range. If there's no input data |
194 | | // left, always returns |min|. |
195 | 235M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { |
196 | 235M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), |
197 | 235M | std::numeric_limits<T>::max()); |
198 | 235M | } _ZN18FuzzedDataProvider15ConsumeIntegralIlEET_v Line | Count | Source | 195 | 8.93M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 8.93M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 8.93M | std::numeric_limits<T>::max()); | 198 | 8.93M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralImEET_v Line | Count | Source | 195 | 4.73M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 4.73M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 4.73M | std::numeric_limits<T>::max()); | 198 | 4.73M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIiEET_v Line | Count | Source | 195 | 3.78M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 3.78M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 3.78M | std::numeric_limits<T>::max()); | 198 | 3.78M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIjEET_v Line | Count | Source | 195 | 23.8M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 23.8M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 23.8M | std::numeric_limits<T>::max()); | 198 | 23.8M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIsEET_v Line | Count | Source | 195 | 5.56k | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 5.56k | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 5.56k | std::numeric_limits<T>::max()); | 198 | 5.56k | } |
_ZN18FuzzedDataProvider15ConsumeIntegralItEET_v Line | Count | Source | 195 | 7.95M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 7.95M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 7.95M | std::numeric_limits<T>::max()); | 198 | 7.95M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIcEET_v Line | Count | Source | 195 | 73.3k | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 73.3k | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 73.3k | std::numeric_limits<T>::max()); | 198 | 73.3k | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIhEET_v Line | Count | Source | 195 | 185M | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 185M | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 185M | std::numeric_limits<T>::max()); | 198 | 185M | } |
_ZN18FuzzedDataProvider15ConsumeIntegralIaEET_v Line | Count | Source | 195 | 292k | template <typename T> T FuzzedDataProvider::ConsumeIntegral() { | 196 | 292k | return ConsumeIntegralInRange(std::numeric_limits<T>::min(), | 197 | 292k | std::numeric_limits<T>::max()); | 198 | 292k | } |
|
199 | | |
200 | | // Returns a number in the range [min, max] by consuming bytes from the |
201 | | // input data. The value might not be uniformly distributed in the given |
202 | | // range. If there's no input data left, always returns |min|. |min| must |
203 | | // be less than or equal to |max|. |
204 | | template <typename T> |
205 | 497M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { |
206 | 497M | static_assert(std::is_integral_v<T>, "An integral type is required."); |
207 | 497M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); |
208 | | |
209 | 497M | if (min > max) Branch (209:7): [True: 0, False: 28.5M]
Branch (209:7): [True: 0, False: 140M]
Branch (209:7): [True: 0, False: 17.5M]
Branch (209:7): [True: 0, False: 91.4M]
Branch (209:7): [True: 0, False: 5.56k]
Branch (209:7): [True: 0, False: 9.57M]
Branch (209:7): [True: 0, False: 75.9k]
Branch (209:7): [True: 0, False: 187M]
Branch (209:7): [True: 0, False: 292k]
Branch (209:7): [True: 0, False: 22.2M]
|
210 | 0 | abort(); |
211 | | |
212 | | // Use the biggest type possible to hold the range and the result. |
213 | 497M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); |
214 | 497M | uint64_t result = 0; |
215 | 497M | size_t offset = 0; |
216 | | |
217 | 1.28G | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 173M, False: 9.72M]
Branch (217:43): [True: 155M, False: 18.7M]
Branch (217:10): [True: 311M, False: 4.53M]
Branch (217:43): [True: 177M, False: 133M]
Branch (217:10): [True: 39.6M, False: 1.05M]
Branch (217:43): [True: 25.9M, False: 13.6M]
Branch (217:10): [True: 253M, False: 36.6M]
Branch (217:43): [True: 206M, False: 46.8M]
Branch (217:10): [True: 9.99k, False: 4.38k]
Branch (217:43): [True: 9.99k, False: 0]
Branch (217:10): [True: 16.4M, False: 6.85M]
Branch (217:43): [True: 16.4M, False: 1.30k]
Branch (217:10): [True: 75.9k, False: 72.0k]
Branch (217:43): [True: 75.9k, False: 0]
Branch (217:10): [True: 187M, False: 180M]
Branch (217:43): [True: 187M, False: 2.44k]
Branch (217:10): [True: 292k, False: 274k]
Branch (217:43): [True: 292k, False: 0]
Branch (217:10): [True: 59.4M, False: 0]
Branch (217:43): [True: 39.1M, False: 20.3M]
|
218 | 1.28G | remaining_bytes_ != 0) { Branch (218:10): [True: 154M, False: 116k]
Branch (218:10): [True: 175M, False: 2.59M]
Branch (218:10): [True: 23.1M, False: 2.83M]
Branch (218:10): [True: 198M, False: 7.99M]
Branch (218:10): [True: 8.81k, False: 1.18k]
Branch (218:10): [True: 13.7M, False: 2.72M]
Branch (218:10): [True: 72.0k, False: 3.84k]
Branch (218:10): [True: 180M, False: 7.15M]
Branch (218:10): [True: 274k, False: 17.5k]
Branch (218:10): [True: 37.1M, False: 1.94M]
|
219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to |
220 | | // allow the fuzzer to more easily explore the input space. This makes |
221 | | // sense, since it works by modifying inputs that caused new code to run, |
222 | | // and this data is often used to encode length of data read by |
223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the |
224 | | // contents of the data that is actually read. |
225 | 783M | --remaining_bytes_; |
226 | 783M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; |
227 | 783M | offset += CHAR_BIT; |
228 | 783M | } |
229 | | |
230 | | // Avoid division by 0, in case |range + 1| results in overflow. |
231 | 497M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 19.6M, False: 8.93M]
Branch (231:7): [True: 135M, False: 4.73M]
Branch (231:7): [True: 17.5M, False: 0]
Branch (231:7): [True: 91.4M, False: 0]
Branch (231:7): [True: 5.56k, False: 0]
Branch (231:7): [True: 9.57M, False: 0]
Branch (231:7): [True: 75.9k, False: 0]
Branch (231:7): [True: 187M, False: 0]
Branch (231:7): [True: 292k, False: 0]
Branch (231:7): [True: 22.2M, False: 0]
|
232 | 483M | result = result % (range + 1); |
233 | | |
234 | 497M | return static_cast<T>(static_cast<uint64_t>(min) + result); |
235 | 497M | } _ZN18FuzzedDataProvider22ConsumeIntegralInRangeIlEET_S1_S1_ Line | Count | Source | 205 | 28.5M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 28.5M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 28.5M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 28.5M | if (min > max) Branch (209:7): [True: 0, False: 28.5M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 28.5M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 28.5M | uint64_t result = 0; | 215 | 28.5M | size_t offset = 0; | 216 | | | 217 | 183M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 173M, False: 9.72M]
Branch (217:43): [True: 155M, False: 18.7M]
| 218 | 183M | remaining_bytes_ != 0) { Branch (218:10): [True: 154M, False: 116k]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 154M | --remaining_bytes_; | 226 | 154M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 154M | offset += CHAR_BIT; | 228 | 154M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 28.5M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 19.6M, False: 8.93M]
| 232 | 19.6M | result = result % (range + 1); | 233 | | | 234 | 28.5M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 28.5M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeImEET_S1_S1_ Line | Count | Source | 205 | 140M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 140M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 140M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 140M | if (min > max) Branch (209:7): [True: 0, False: 140M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 140M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 140M | uint64_t result = 0; | 215 | 140M | size_t offset = 0; | 216 | | | 217 | 315M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 311M, False: 4.53M]
Branch (217:43): [True: 177M, False: 133M]
| 218 | 315M | remaining_bytes_ != 0) { Branch (218:10): [True: 175M, False: 2.59M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 175M | --remaining_bytes_; | 226 | 175M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 175M | offset += CHAR_BIT; | 228 | 175M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 140M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 135M, False: 4.73M]
| 232 | 135M | result = result % (range + 1); | 233 | | | 234 | 140M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 140M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIiEET_S1_S1_ Line | Count | Source | 205 | 17.5M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 17.5M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 17.5M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 17.5M | if (min > max) Branch (209:7): [True: 0, False: 17.5M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 17.5M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 17.5M | uint64_t result = 0; | 215 | 17.5M | size_t offset = 0; | 216 | | | 217 | 40.6M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 39.6M, False: 1.05M]
Branch (217:43): [True: 25.9M, False: 13.6M]
| 218 | 40.6M | remaining_bytes_ != 0) { Branch (218:10): [True: 23.1M, False: 2.83M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 23.1M | --remaining_bytes_; | 226 | 23.1M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 23.1M | offset += CHAR_BIT; | 228 | 23.1M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 17.5M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 17.5M, False: 0]
| 232 | 17.5M | result = result % (range + 1); | 233 | | | 234 | 17.5M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 17.5M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIjEET_S1_S1_ Line | Count | Source | 205 | 91.4M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 91.4M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 91.4M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 91.4M | if (min > max) Branch (209:7): [True: 0, False: 91.4M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 91.4M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 91.4M | uint64_t result = 0; | 215 | 91.4M | size_t offset = 0; | 216 | | | 217 | 290M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 253M, False: 36.6M]
Branch (217:43): [True: 206M, False: 46.8M]
| 218 | 290M | remaining_bytes_ != 0) { Branch (218:10): [True: 198M, False: 7.99M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 198M | --remaining_bytes_; | 226 | 198M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 198M | offset += CHAR_BIT; | 228 | 198M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 91.4M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 91.4M, False: 0]
| 232 | 91.4M | result = result % (range + 1); | 233 | | | 234 | 91.4M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 91.4M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIsEET_S1_S1_ Line | Count | Source | 205 | 5.56k | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 5.56k | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 5.56k | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 5.56k | if (min > max) Branch (209:7): [True: 0, False: 5.56k]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 5.56k | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 5.56k | uint64_t result = 0; | 215 | 5.56k | size_t offset = 0; | 216 | | | 217 | 14.3k | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 9.99k, False: 4.38k]
Branch (217:43): [True: 9.99k, False: 0]
| 218 | 14.3k | remaining_bytes_ != 0) { Branch (218:10): [True: 8.81k, False: 1.18k]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 8.81k | --remaining_bytes_; | 226 | 8.81k | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 8.81k | offset += CHAR_BIT; | 228 | 8.81k | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 5.56k | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 5.56k, False: 0]
| 232 | 5.56k | result = result % (range + 1); | 233 | | | 234 | 5.56k | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 5.56k | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeItEET_S1_S1_ Line | Count | Source | 205 | 9.57M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 9.57M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 9.57M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 9.57M | if (min > max) Branch (209:7): [True: 0, False: 9.57M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 9.57M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 9.57M | uint64_t result = 0; | 215 | 9.57M | size_t offset = 0; | 216 | | | 217 | 23.2M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 16.4M, False: 6.85M]
Branch (217:43): [True: 16.4M, False: 1.30k]
| 218 | 23.2M | remaining_bytes_ != 0) { Branch (218:10): [True: 13.7M, False: 2.72M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 13.7M | --remaining_bytes_; | 226 | 13.7M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 13.7M | offset += CHAR_BIT; | 228 | 13.7M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 9.57M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 9.57M, False: 0]
| 232 | 9.57M | result = result % (range + 1); | 233 | | | 234 | 9.57M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 9.57M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIcEET_S1_S1_ Line | Count | Source | 205 | 75.9k | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 75.9k | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 75.9k | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 75.9k | if (min > max) Branch (209:7): [True: 0, False: 75.9k]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 75.9k | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 75.9k | uint64_t result = 0; | 215 | 75.9k | size_t offset = 0; | 216 | | | 217 | 148k | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 75.9k, False: 72.0k]
Branch (217:43): [True: 75.9k, False: 0]
| 218 | 148k | remaining_bytes_ != 0) { Branch (218:10): [True: 72.0k, False: 3.84k]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 72.0k | --remaining_bytes_; | 226 | 72.0k | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 72.0k | offset += CHAR_BIT; | 228 | 72.0k | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 75.9k | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 75.9k, False: 0]
| 232 | 75.9k | result = result % (range + 1); | 233 | | | 234 | 75.9k | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 75.9k | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIhEET_S1_S1_ Line | Count | Source | 205 | 187M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 187M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 187M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 187M | if (min > max) Branch (209:7): [True: 0, False: 187M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 187M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 187M | uint64_t result = 0; | 215 | 187M | size_t offset = 0; | 216 | | | 217 | 367M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 187M, False: 180M]
Branch (217:43): [True: 187M, False: 2.44k]
| 218 | 367M | remaining_bytes_ != 0) { Branch (218:10): [True: 180M, False: 7.15M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 180M | --remaining_bytes_; | 226 | 180M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 180M | offset += CHAR_BIT; | 228 | 180M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 187M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 187M, False: 0]
| 232 | 187M | result = result % (range + 1); | 233 | | | 234 | 187M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 187M | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIaEET_S1_S1_ Line | Count | Source | 205 | 292k | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 292k | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 292k | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 292k | if (min > max) Branch (209:7): [True: 0, False: 292k]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 292k | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 292k | uint64_t result = 0; | 215 | 292k | size_t offset = 0; | 216 | | | 217 | 566k | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 292k, False: 274k]
Branch (217:43): [True: 292k, False: 0]
| 218 | 566k | remaining_bytes_ != 0) { Branch (218:10): [True: 274k, False: 17.5k]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 274k | --remaining_bytes_; | 226 | 274k | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 274k | offset += CHAR_BIT; | 228 | 274k | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 292k | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 292k, False: 0]
| 232 | 292k | result = result % (range + 1); | 233 | | | 234 | 292k | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 292k | } |
_ZN18FuzzedDataProvider22ConsumeIntegralInRangeIKmEET_S2_S2_ Line | Count | Source | 205 | 22.2M | T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) { | 206 | 22.2M | static_assert(std::is_integral_v<T>, "An integral type is required."); | 207 | 22.2M | static_assert(sizeof(T) <= sizeof(uint64_t), "Unsupported integral type."); | 208 | | | 209 | 22.2M | if (min > max) Branch (209:7): [True: 0, False: 22.2M]
| 210 | 0 | abort(); | 211 | | | 212 | | // Use the biggest type possible to hold the range and the result. | 213 | 22.2M | uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min); | 214 | 22.2M | uint64_t result = 0; | 215 | 22.2M | size_t offset = 0; | 216 | | | 217 | 59.4M | while (offset < sizeof(T) * CHAR_BIT && (range >> offset) > 0 && Branch (217:10): [True: 59.4M, False: 0]
Branch (217:43): [True: 39.1M, False: 20.3M]
| 218 | 59.4M | remaining_bytes_ != 0) { Branch (218:10): [True: 37.1M, False: 1.94M]
| 219 | | // Pull bytes off the end of the seed data. Experimentally, this seems to | 220 | | // allow the fuzzer to more easily explore the input space. This makes | 221 | | // sense, since it works by modifying inputs that caused new code to run, | 222 | | // and this data is often used to encode length of data read by | 223 | | // |ConsumeBytes|. Separating out read lengths makes it easier modify the | 224 | | // contents of the data that is actually read. | 225 | 37.1M | --remaining_bytes_; | 226 | 37.1M | result = (result << CHAR_BIT) | data_ptr_[remaining_bytes_]; | 227 | 37.1M | offset += CHAR_BIT; | 228 | 37.1M | } | 229 | | | 230 | | // Avoid division by 0, in case |range + 1| results in overflow. | 231 | 22.2M | if (range != std::numeric_limits<decltype(range)>::max()) Branch (231:7): [True: 22.2M, False: 0]
| 232 | 22.2M | result = result % (range + 1); | 233 | | | 234 | 22.2M | return static_cast<T>(static_cast<uint64_t>(min) + result); | 235 | 22.2M | } |
|
236 | | |
237 | | // Returns a floating point value in the range [Type's lowest, Type's max] by |
238 | | // consuming bytes from the input data. If there's no input data left, always |
239 | | // returns approximately 0. |
240 | 3.49k | template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() { |
241 | 3.49k | return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(), |
242 | 3.49k | std::numeric_limits<T>::max()); |
243 | 3.49k | } _ZN18FuzzedDataProvider20ConsumeFloatingPointIdEET_v Line | Count | Source | 240 | 3.11k | template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() { | 241 | 3.11k | return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(), | 242 | 3.11k | std::numeric_limits<T>::max()); | 243 | 3.11k | } |
_ZN18FuzzedDataProvider20ConsumeFloatingPointIfEET_v Line | Count | Source | 240 | 373 | template <typename T> T FuzzedDataProvider::ConsumeFloatingPoint() { | 241 | 373 | return ConsumeFloatingPointInRange<T>(std::numeric_limits<T>::lowest(), | 242 | 373 | std::numeric_limits<T>::max()); | 243 | 373 | } |
|
244 | | |
245 | | // Returns a floating point value in the given range by consuming bytes from |
246 | | // the input data. If there's no input data left, returns |min|. Note that |
247 | | // |min| must be less than or equal to |max|. |
248 | | template <typename T> |
249 | 3.49k | T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) { |
250 | 3.49k | if (min > max) Branch (250:7): [True: 0, False: 3.11k]
Branch (250:7): [True: 0, False: 373]
|
251 | 0 | abort(); |
252 | | |
253 | 3.49k | T range = .0; |
254 | 3.49k | T result = min; |
255 | 3.49k | constexpr T zero(.0); |
256 | 3.49k | if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) { Branch (256:7): [True: 3.11k, False: 0]
Branch (256:21): [True: 3.11k, False: 0]
Branch (256:35): [True: 3.11k, False: 0]
Branch (256:7): [True: 373, False: 0]
Branch (256:21): [True: 373, False: 0]
Branch (256:35): [True: 373, False: 0]
|
257 | | // The diff |max - min| would overflow the given floating point type. Use |
258 | | // the half of the diff as the range and consume a bool to decide whether |
259 | | // the result is in the first of the second part of the diff. |
260 | 3.49k | range = (max / 2.0) - (min / 2.0); |
261 | 3.49k | if (ConsumeBool()) { Branch (261:9): [True: 2.06k, False: 1.05k]
Branch (261:9): [True: 8, False: 365]
|
262 | 2.07k | result += range; |
263 | 2.07k | } |
264 | 3.49k | } else { |
265 | 0 | range = max - min; |
266 | 0 | } |
267 | | |
268 | 3.49k | return result + range * ConsumeProbability<T>(); |
269 | 3.49k | } _ZN18FuzzedDataProvider27ConsumeFloatingPointInRangeIdEET_S1_S1_ Line | Count | Source | 249 | 3.11k | T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) { | 250 | 3.11k | if (min > max) Branch (250:7): [True: 0, False: 3.11k]
| 251 | 0 | abort(); | 252 | | | 253 | 3.11k | T range = .0; | 254 | 3.11k | T result = min; | 255 | 3.11k | constexpr T zero(.0); | 256 | 3.11k | if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) { Branch (256:7): [True: 3.11k, False: 0]
Branch (256:21): [True: 3.11k, False: 0]
Branch (256:35): [True: 3.11k, False: 0]
| 257 | | // The diff |max - min| would overflow the given floating point type. Use | 258 | | // the half of the diff as the range and consume a bool to decide whether | 259 | | // the result is in the first of the second part of the diff. | 260 | 3.11k | range = (max / 2.0) - (min / 2.0); | 261 | 3.11k | if (ConsumeBool()) { Branch (261:9): [True: 2.06k, False: 1.05k]
| 262 | 2.06k | result += range; | 263 | 2.06k | } | 264 | 3.11k | } else { | 265 | 0 | range = max - min; | 266 | 0 | } | 267 | | | 268 | 3.11k | return result + range * ConsumeProbability<T>(); | 269 | 3.11k | } |
_ZN18FuzzedDataProvider27ConsumeFloatingPointInRangeIfEET_S1_S1_ Line | Count | Source | 249 | 373 | T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) { | 250 | 373 | if (min > max) Branch (250:7): [True: 0, False: 373]
| 251 | 0 | abort(); | 252 | | | 253 | 373 | T range = .0; | 254 | 373 | T result = min; | 255 | 373 | constexpr T zero(.0); | 256 | 373 | if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) { Branch (256:7): [True: 373, False: 0]
Branch (256:21): [True: 373, False: 0]
Branch (256:35): [True: 373, False: 0]
| 257 | | // The diff |max - min| would overflow the given floating point type. Use | 258 | | // the half of the diff as the range and consume a bool to decide whether | 259 | | // the result is in the first of the second part of the diff. | 260 | 373 | range = (max / 2.0) - (min / 2.0); | 261 | 373 | if (ConsumeBool()) { Branch (261:9): [True: 8, False: 365]
| 262 | 8 | result += range; | 263 | 8 | } | 264 | 373 | } else { | 265 | 0 | range = max - min; | 266 | 0 | } | 267 | | | 268 | 373 | return result + range * ConsumeProbability<T>(); | 269 | 373 | } |
|
270 | | |
271 | | // Returns a floating point number in the range [0.0, 1.0]. If there's no |
272 | | // input data left, always returns 0. |
273 | 3.49k | template <typename T> T FuzzedDataProvider::ConsumeProbability() { |
274 | 3.49k | static_assert(std::is_floating_point_v<T>, |
275 | 3.49k | "A floating point type is required."); |
276 | | |
277 | | // Use different integral types for different floating point types in order |
278 | | // to provide better density of the resulting values. |
279 | 3.49k | using IntegralType = |
280 | 3.49k | typename std::conditional_t<(sizeof(T) <= sizeof(uint32_t)), uint32_t, |
281 | 3.49k | uint64_t>; |
282 | | |
283 | 3.49k | T result = static_cast<T>(ConsumeIntegral<IntegralType>()); |
284 | 3.49k | result /= static_cast<T>(std::numeric_limits<IntegralType>::max()); |
285 | 3.49k | return result; |
286 | 3.49k | } _ZN18FuzzedDataProvider18ConsumeProbabilityIdEET_v Line | Count | Source | 273 | 3.11k | template <typename T> T FuzzedDataProvider::ConsumeProbability() { | 274 | 3.11k | static_assert(std::is_floating_point_v<T>, | 275 | 3.11k | "A floating point type is required."); | 276 | | | 277 | | // Use different integral types for different floating point types in order | 278 | | // to provide better density of the resulting values. | 279 | 3.11k | using IntegralType = | 280 | 3.11k | typename std::conditional_t<(sizeof(T) <= sizeof(uint32_t)), uint32_t, | 281 | 3.11k | uint64_t>; | 282 | | | 283 | 3.11k | T result = static_cast<T>(ConsumeIntegral<IntegralType>()); | 284 | 3.11k | result /= static_cast<T>(std::numeric_limits<IntegralType>::max()); | 285 | 3.11k | return result; | 286 | 3.11k | } |
_ZN18FuzzedDataProvider18ConsumeProbabilityIfEET_v Line | Count | Source | 273 | 373 | template <typename T> T FuzzedDataProvider::ConsumeProbability() { | 274 | 373 | static_assert(std::is_floating_point_v<T>, | 275 | 373 | "A floating point type is required."); | 276 | | | 277 | | // Use different integral types for different floating point types in order | 278 | | // to provide better density of the resulting values. | 279 | 373 | using IntegralType = | 280 | 373 | typename std::conditional_t<(sizeof(T) <= sizeof(uint32_t)), uint32_t, | 281 | 373 | uint64_t>; | 282 | | | 283 | 373 | T result = static_cast<T>(ConsumeIntegral<IntegralType>()); | 284 | 373 | result /= static_cast<T>(std::numeric_limits<IntegralType>::max()); | 285 | 373 | return result; | 286 | 373 | } |
|
287 | | |
288 | | // Reads one byte and returns a bool, or false when no data remains. |
289 | 181M | inline bool FuzzedDataProvider::ConsumeBool() { |
290 | 181M | return 1 & ConsumeIntegral<uint8_t>(); |
291 | 181M | } |
292 | | |
293 | | // Returns an enum value. The enum must start at 0 and be contiguous. It must |
294 | | // also contain |kMaxValue| aliased to its largest (inclusive) value. Such as: |
295 | | // enum class Foo { SomeValue, OtherValue, kMaxValue = OtherValue }; |
296 | | template <typename T> T FuzzedDataProvider::ConsumeEnum() { |
297 | | static_assert(std::is_enum_v<T>, "|T| must be an enum type."); |
298 | | return static_cast<T>( |
299 | | ConsumeIntegralInRange<uint32_t>(0, static_cast<uint32_t>(T::kMaxValue))); |
300 | | } |
301 | | |
302 | | // Returns a copy of the value selected from the given fixed-size |array|. |
303 | | template <typename T, size_t size> |
304 | 9.13M | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { |
305 | 9.13M | static_assert(size > 0, "The array must be non empty."); |
306 | 9.13M | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; |
307 | 9.13M | } _ZN18FuzzedDataProvider16PickValueInArrayI12ServiceFlagsLm7EEET_RAT0__KS2_ Line | Count | Source | 304 | 4.64M | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 4.64M | static_assert(size > 0, "The array must be non empty."); | 306 | 4.64M | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 4.64M | } |
_ZN18FuzzedDataProvider16PickValueInArrayI14ConnectionTypeLm7EEET_RAT0__KS2_ Line | Count | Source | 304 | 623k | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 623k | static_assert(size > 0, "The array must be non empty."); | 306 | 623k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 623k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI18NetPermissionFlagsLm10EEET_RAT0__KS2_ Line | Count | Source | 304 | 253k | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 253k | static_assert(size > 0, "The array must be non empty."); | 306 | 253k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 253k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIN4node16TransactionErrorELm5EEET_RAT0__KS3_ Line | Count | Source | 304 | 65 | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 65 | static_assert(size > 0, "The array must be non empty."); | 306 | 65 | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 65 | } |
_ZN18FuzzedDataProvider16PickValueInArrayINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEELm749EEET_RAT0__KS7_ Line | Count | Source | 304 | 65 | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 65 | static_assert(size > 0, "The array must be non empty."); | 306 | 65 | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 65 | } |
_ZN18FuzzedDataProvider16PickValueInArrayI8ByteUnitLm9EEET_RAT0__KS2_ Line | Count | Source | 304 | 1.59k | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 1.59k | static_assert(size > 0, "The array must be non empty."); | 306 | 1.59k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 1.59k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI9COutPointLm50EEET_RAT0__KS2_ Line | Count | Source | 304 | 2.51M | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 2.51M | static_assert(size > 0, "The array must be non empty."); | 306 | 2.51M | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 2.51M | } |
_ZN18FuzzedDataProvider16PickValueInArrayI18TxValidationResultLm11EEET_RAT0__KS2_ Line | Count | Source | 304 | 253k | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 253k | static_assert(size > 0, "The array must be non empty."); | 306 | 253k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 253k | } |
_ZN18FuzzedDataProvider16PickValueInArrayINSt6chrono8durationIlSt5ratioILl1ELl1000000EEEELm128EEET_RAT0__KS6_ Line | Count | Source | 304 | 841k | T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) { | 305 | 841k | static_assert(size > 0, "The array must be non empty."); | 306 | 841k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 307 | 841k | } |
|
308 | | |
309 | | template <typename T, size_t size> |
310 | 895k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { |
311 | 895k | static_assert(size > 0, "The array must be non empty."); |
312 | 895k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; |
313 | 895k | } _ZN18FuzzedDataProvider16PickValueInArrayI7NetworkLm7EEET_RKSt5arrayIS2_XT0_EE Line | Count | Source | 310 | 479k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 479k | static_assert(size > 0, "The array must be non empty."); | 312 | 479k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 479k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI18FeeEstimateHorizonLm3EEET_RKSt5arrayIS2_XT0_EE Line | Count | Source | 310 | 3.82k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 3.82k | static_assert(size > 0, "The array must be non empty."); | 312 | 3.82k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 3.82k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI10OutputTypeLm4EEET_RKSt5arrayIS2_XT0_EE Line | Count | Source | 310 | 246k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 246k | static_assert(size > 0, "The array must be non empty."); | 312 | 246k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 246k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm18EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 11.0k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 11.0k | static_assert(size > 0, "The array must be non empty."); | 312 | 11.0k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 11.0k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm10EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 2.11k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 2.11k | static_assert(size > 0, "The array must be non empty."); | 312 | 2.11k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 2.11k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm8EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 474 | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 474 | static_assert(size > 0, "The array must be non empty."); | 312 | 474 | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 474 | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm4EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 16.9k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 16.9k | static_assert(size > 0, "The array must be non empty."); | 312 | 16.9k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 16.9k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm3EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 12.4k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 12.4k | static_assert(size > 0, "The array must be non empty."); | 312 | 12.4k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 12.4k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiLm2EEET_RKSt5arrayIS1_XT0_EE Line | Count | Source | 310 | 123k | T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) { | 311 | 123k | static_assert(size > 0, "The array must be non empty."); | 312 | 123k | return array[ConsumeIntegralInRange<size_t>(0, size - 1)]; | 313 | 123k | } |
|
314 | | |
315 | | template <typename T> |
316 | 13.5M | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { |
317 | 13.5M | if (!list.size()) Branch (317:7): [True: 0, False: 1.31k]
Branch (317:7): [True: 0, False: 63.7k]
Branch (317:7): [True: 0, False: 13.3k]
Branch (317:7): [True: 0, False: 201k]
Branch (317:7): [True: 0, False: 189]
Branch (317:7): [True: 0, False: 183]
Branch (317:7): [True: 0, False: 183]
Branch (317:7): [True: 0, False: 8]
Branch (317:7): [True: 0, False: 2.64k]
Branch (317:7): [True: 0, False: 3.88k]
Branch (317:7): [True: 0, False: 39.7k]
Branch (317:7): [True: 0, False: 3.66M]
Branch (317:7): [True: 0, False: 9.57M]
|
318 | 0 | abort(); |
319 | | |
320 | 13.5M | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); |
321 | 13.5M | } _ZN18FuzzedDataProvider16PickValueInArrayI10bloomflagsEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 1.31k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 1.31k | if (!list.size()) Branch (317:7): [True: 0, False: 1.31k]
| 318 | 0 | abort(); | 319 | | | 320 | 1.31k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 1.31k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI11BlockStatusEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 63.7k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 63.7k | if (!list.size()) Branch (317:7): [True: 0, False: 63.7k]
| 318 | 0 | abort(); | 319 | | | 320 | 63.7k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 63.7k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI19ConnectionDirectionEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 13.3k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 13.3k | if (!list.size()) Branch (317:7): [True: 0, False: 13.3k]
| 318 | 0 | abort(); | 319 | | | 320 | 13.3k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 13.3k | } |
dbwrapper.cpp:_ZN18FuzzedDataProvider16PickValueInArrayIZ38dbwrapper_concurrent_reads_fuzz_targetSt4spanIKhLm18446744073709551615EEE6ReadOpEET_St16initializer_listIKS5_E Line | Count | Source | 316 | 201k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 201k | if (!list.size()) Branch (317:7): [True: 0, False: 201k]
| 318 | 0 | abort(); | 319 | | | 320 | 201k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 201k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI9FeeReasonEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 189 | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 189 | if (!list.size()) Branch (317:7): [True: 0, False: 189]
| 318 | 0 | abort(); | 319 | | | 320 | 189 | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 189 | } |
_ZN18FuzzedDataProvider16PickValueInArrayI25BlockPolicyEstimateReasonEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 183 | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 183 | if (!list.size()) Branch (317:7): [True: 0, False: 183]
| 318 | 0 | abort(); | 319 | | | 320 | 183 | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 183 | } |
_ZN18FuzzedDataProvider16PickValueInArrayI20FeeRateEstimatorTypeEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 183 | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 183 | if (!list.size()) Branch (317:7): [True: 0, False: 183]
| 318 | 0 | abort(); | 319 | | | 320 | 183 | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 183 | } |
_ZN18FuzzedDataProvider16PickValueInArrayIdEET_St16initializer_listIKS1_E Line | Count | Source | 316 | 8 | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 8 | if (!list.size()) Branch (317:7): [True: 0, False: 8]
| 318 | 0 | abort(); | 319 | | | 320 | 8 | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 8 | } |
_ZN18FuzzedDataProvider16PickValueInArrayI13SigningResultEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 2.64k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 2.64k | if (!list.size()) Branch (317:7): [True: 0, False: 2.64k]
| 318 | 0 | abort(); | 319 | | | 320 | 2.64k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 2.64k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI10SigVersionEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 3.88k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 3.88k | if (!list.size()) Branch (317:7): [True: 0, False: 3.88k]
| 318 | 0 | abort(); | 319 | | | 320 | 3.88k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 3.88k | } |
_ZN18FuzzedDataProvider16PickValueInArrayI15OptionsCategoryEET_St16initializer_listIKS2_E Line | Count | Source | 316 | 39.7k | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 39.7k | if (!list.size()) Branch (317:7): [True: 0, False: 39.7k]
| 318 | 0 | abort(); | 319 | | | 320 | 39.7k | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 39.7k | } |
_ZN18FuzzedDataProvider16PickValueInArrayIiEET_St16initializer_listIKS1_E Line | Count | Source | 316 | 3.66M | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 3.66M | if (!list.size()) Branch (317:7): [True: 0, False: 3.66M]
| 318 | 0 | abort(); | 319 | | | 320 | 3.66M | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 3.66M | } |
_ZN18FuzzedDataProvider16PickValueInArrayIjEET_St16initializer_listIKS1_E Line | Count | Source | 316 | 9.57M | T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) { | 317 | 9.57M | if (!list.size()) Branch (317:7): [True: 0, False: 9.57M]
| 318 | 0 | abort(); | 319 | | | 320 | 9.57M | return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1)); | 321 | 9.57M | } |
|
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 | 676 | size_t num_bytes) { |
331 | 676 | num_bytes = std::min(num_bytes, remaining_bytes_); |
332 | 676 | CopyAndAdvance(destination, num_bytes); |
333 | 676 | return num_bytes; |
334 | 676 | } |
335 | | |
336 | | // Private methods. |
337 | | inline void FuzzedDataProvider::CopyAndAdvance(void *destination, |
338 | 9.63M | size_t num_bytes) { |
339 | 9.63M | std::memcpy(destination, data_ptr_, num_bytes); |
340 | 9.63M | Advance(num_bytes); |
341 | 9.63M | } |
342 | | |
343 | 1.73G | inline void FuzzedDataProvider::Advance(size_t num_bytes) { |
344 | 1.73G | if (num_bytes > remaining_bytes_) Branch (344:7): [True: 0, False: 1.73G]
|
345 | 0 | abort(); |
346 | | |
347 | 1.73G | data_ptr_ += num_bytes; |
348 | 1.73G | remaining_bytes_ -= num_bytes; |
349 | 1.73G | } |
350 | | |
351 | | template <typename T> |
352 | 10.2M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) { |
353 | 10.2M | 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 | 10.2M | std::vector<T> result(size); |
362 | 10.2M | if (size == 0) { Branch (362:7): [True: 46.4k, False: 9.12M]
Branch (362:7): [True: 604k, False: 504k]
|
363 | 650k | if (num_bytes != 0) Branch (363:9): [True: 0, False: 46.4k]
Branch (363:9): [True: 0, False: 604k]
|
364 | 0 | abort(); |
365 | 650k | return result; |
366 | 650k | } |
367 | | |
368 | 9.63M | 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 | 9.63M | result.shrink_to_fit(); |
374 | 9.63M | return result; |
375 | 10.2M | } _ZN18FuzzedDataProvider12ConsumeBytesIhEESt6vectorIT_SaIS2_EEmm Line | Count | Source | 352 | 9.17M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) { | 353 | 9.17M | 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 | 9.17M | std::vector<T> result(size); | 362 | 9.17M | if (size == 0) { Branch (362:7): [True: 46.4k, False: 9.12M]
| 363 | 46.4k | if (num_bytes != 0) Branch (363:9): [True: 0, False: 46.4k]
| 364 | 0 | abort(); | 365 | 46.4k | return result; | 366 | 46.4k | } | 367 | | | 368 | 9.12M | 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 | 9.12M | result.shrink_to_fit(); | 374 | 9.12M | return result; | 375 | 9.17M | } |
_ZN18FuzzedDataProvider12ConsumeBytesISt4byteEESt6vectorIT_SaIS3_EEmm Line | Count | Source | 352 | 1.10M | std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) { | 353 | 1.10M | 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 | 1.10M | std::vector<T> result(size); | 362 | 1.10M | if (size == 0) { Branch (362:7): [True: 604k, False: 504k]
| 363 | 604k | if (num_bytes != 0) Branch (363:9): [True: 0, False: 604k]
| 364 | 0 | abort(); | 365 | 604k | return result; | 366 | 604k | } | 367 | | | 368 | 504k | 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 | 504k | result.shrink_to_fit(); | 374 | 504k | return result; | 375 | 1.10M | } |
|
376 | | |
377 | | template <typename TS, typename TU> |
378 | 1.72G | TS FuzzedDataProvider::ConvertUnsignedToSigned(TU value) { |
379 | 1.72G | static_assert(sizeof(TS) == sizeof(TU), "Incompatible data types."); |
380 | 1.72G | static_assert(!std::numeric_limits<TU>::is_signed, |
381 | 1.72G | "Source type must be unsigned."); |
382 | | |
383 | | if constexpr (std::numeric_limits<TS>::is_modulo) |
384 | | return static_cast<TS>(value); |
385 | | |
386 | | // Avoid using implementation-defined unsigned to signed conversions. |
387 | | // To learn more, see https://stackoverflow.com/questions/13150449. |
388 | 1.72G | constexpr auto TS_max = static_cast<TU>(std::numeric_limits<TS>::max()); |
389 | 1.72G | if (value <= TS_max) { Branch (389:7): [True: 1.46G, False: 258M]
|
390 | 1.46G | return static_cast<TS>(value); |
391 | 1.46G | } else { |
392 | 258M | constexpr auto TS_min = std::numeric_limits<TS>::min(); |
393 | 258M | return TS_min + static_cast<TS>(value - TS_min); |
394 | 258M | } |
395 | 1.72G | } |
396 | | |
397 | | #endif // LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_ |