Struct bitcoin_fuzz::FuzzedDataProvider
source · pub struct FuzzedDataProvider { /* private fields */ }Expand description
| In addition to the comments below, the | API is also briefly documented at | https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider |
Implementations§
source§impl FuzzedDataProvider
impl FuzzedDataProvider
sourcepub fn new(data: *const u8, size: usize) -> Self
pub fn new(data: *const u8, size: usize) -> Self
| |data| is an array of length |size| that the | FuzzedDataProvider wraps to provide more | granular access. |data| must outlive the | FuzzedDataProvider.
sourcepub fn remaining_bytes(&mut self) -> usize
pub fn remaining_bytes(&mut self) -> usize
| Reports the remaining bytes available | for fuzzed input. |
sourcepub fn consume_bytes<T>(&mut self, num_bytes: usize) -> Vec<T>
pub fn consume_bytes<T>(&mut self, num_bytes: usize) -> Vec<T>
| Methods returning std::vector of bytes. These | are the most popular choice when splitting | fuzzing input into pieces, as every piece is | put into a separate buffer (i.e. ASan would | catch any under-/overflow) and the memory | will be released automatically. |
| Returns a std::vector containing | num_bytes | of |
| input data. If fewer than | num_bytes | of data |
| remain, returns a shorter std::vector | ||
| containing all of the data that’s left. Can be | ||
| used with any byte sized type, such as char, | ||
| unsigned char, uint8_t, etc. |
sourcepub fn consume_bytes_with_terminator<T: Zero>(
&mut self,
num_bytes: usize,
terminator: Option<T>
) -> Vec<T>
pub fn consume_bytes_with_terminator<T: Zero>( &mut self, num_bytes: usize, terminator: Option<T> ) -> Vec<T>
| Similar to |ConsumeBytes|, but also appends the | terminator value at the end of the resulting | vector. Useful, when a mutable null-terminated | C-string is needed, for example. But that is | a rare case. Better avoid it, if possible, and | prefer using |ConsumeBytes| or | |ConsumeBytesAsString| methods.
sourcepub fn consume_remaining_bytes<T>(&mut self) -> Vec<T>
pub fn consume_remaining_bytes<T>(&mut self) -> Vec<T>
| Returns a std::vector containing all | remaining bytes of the input data. |
sourcepub fn consume_bytes_as_string(&mut self, num_bytes: usize) -> String
pub fn consume_bytes_as_string(&mut self, num_bytes: usize) -> String
| Methods returning strings. Use only when you | need a std::string or a null terminated | C-string. Otherwise, prefer the methods | returning std::vector. |
| Returns a std::string containing | num_bytes | of |
| input data. Using this and | .c_str() | on the |
| resulting string is the best way to get an | ||
| immutable null-terminated C string. If fewer | ||
| than | num_bytes | of data remain, returns |
| a shorter std::string containing all of the | ||
| data that’s left. |
sourcepub fn consume_random_length_string_with_maxlen(
&mut self,
max_length: usize
) -> String
pub fn consume_random_length_string_with_maxlen( &mut self, max_length: usize ) -> String
| Returns a std::string of length from 0 to | |max_length|. When it runs out of input data, | returns what remains of the input. Designed to | be more stable with respect to a fuzzer | inserting characters than just picking a random | length and then consuming that many bytes with | |ConsumeBytes|.
sourcepub fn consume_random_length_string(&mut self) -> String
pub fn consume_random_length_string(&mut self) -> String
| Returns a std::string of length from | 0 to remaining_bytes_|. |
sourcepub fn consume_remaining_bytes_as_string(&mut self) -> String
pub fn consume_remaining_bytes_as_string(&mut self) -> String
| Returns a std::string containing all remaining | bytes of the input data. | | Prefer using |ConsumeRemainingBytes| unless you | actually need a std::string object.
sourcepub fn consume_integral<T>(&mut self) -> T
pub fn consume_integral<T>(&mut self) -> T
| Methods returning integer values. |
| Returns a number in the range [Type’s min, | Type’s max]. The value might not be uniformly | distributed in the given range. If there’s no | input data left, always returns |min|.
sourcepub fn consume_integral_in_range<T>(&mut self, min: T, max: T) -> T
pub fn consume_integral_in_range<T>(&mut self, min: T, max: T) -> T
| Returns a number in the range [min, max] by | consuming bytes from the input data. The value | might not be uniformly distributed in the given | range. If there’s no input data left, always | returns |min|. |min| must be less than or equal | to |max|.
sourcepub fn consume_floating_point<T>(&mut self) -> T
pub fn consume_floating_point<T>(&mut self) -> T
| Methods returning floating point values. |
| Returns a floating point value in the range | [Type’s lowest, Type’s max] by consuming bytes | from the input data. If there’s no input data | left, always returns approximately 0.
sourcepub fn consume_floating_point_in_range<T>(&mut self, min: T, max: T) -> T
pub fn consume_floating_point_in_range<T>(&mut self, min: T, max: T) -> T
| Returns a floating point value in the given | range by consuming bytes from the input | data. If there’s no input data left, returns | |min|. Note that |min| must be less than or | equal to |max|.
sourcepub fn consume_probability<T>(&mut self) -> T
pub fn consume_probability<T>(&mut self) -> T
| Returns a floating point number in the | range [0.0, 1.0]. If there’s no input | data left, always returns 0. | | 0 <= return value <= 1. |
sourcepub fn consume_bool(&mut self) -> bool
pub fn consume_bool(&mut self) -> bool
| Reads one byte and returns a bool, or | false when no data remains. |
sourcepub fn consume_enum<T>(&mut self) -> T
pub fn consume_enum<T>(&mut self) -> T
| Returns a value chosen from the given | enum. |
| Returns an enum value. The enum must start at | 0 and be contiguous. It must also contain | |kMaxValue| aliased to its largest (inclusive) | value. Such as: enum class Foo { SomeValue, | OtherValue, kMaxValue = OtherValue };
sourcepub fn pick_value_in_array<T, const size: usize>(
&mut self,
array: &[T; size]
) -> T
pub fn pick_value_in_array<T, const size: usize>( &mut self, array: &[T; size] ) -> T
| Returns a copy of the value selected | from the given fixed-size |array|. |
pub fn pick_value_in_array_with_initlist<T>( &mut self, list: InitializerList<T> ) -> T
sourcepub fn consume_data(
&mut self,
destination: *mut c_void,
num_bytes: usize
) -> usize
pub fn consume_data( &mut self, destination: *mut c_void, num_bytes: usize ) -> usize
| Writes data to the given destination | and returns number of bytes written. |
| Writes |num_bytes| of input data to the given | destination pointer. If there is not enough | data left, writes all remaining bytes. Return | value is the number of bytes written. | | In general, it’s better to avoid using this | function, but it may be useful in cases when | it’s necessary to fill a certain buffer or | object with fuzzing data.
sourcepub fn copy_and_advance(&mut self, destination: *mut c_void, num_bytes: usize)
pub fn copy_and_advance(&mut self, destination: *mut c_void, num_bytes: usize)
| Private methods. |