1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
15 // Simply forwards to <fuzzer/FuzzedDataProvider.h> when available from clang,
16 // or provides a simple stub implementation.
19 #if defined(__clang__)
20 #include <fuzzer/FuzzedDataProvider.h>
21 #else // !defined(__clang__)
23 // If a fuzzer wants to use FuzzedDataProvider to build a fuzz target unit
24 // test without clang, it can use this trivial class with the same signature.
25 // The behavior will NOT be the same as a fuzzer unit test built by clang for
26 // non-trivial inputs. This means non-clang fuzzer unit tests will not be
27 // effective regression tests if given a seed corpus. These non-clang tests are
28 // still useful, however, as they will guarantee the fuzzer code can compile and
31 // The methods of this class are intentionally undocumented. To see the
32 // documentation for each, consult the real header file, e.g. in
33 // .cipd/pigweed/lib/clang/11.0.0/include/fuzzer/FuzzedDataProvider.h
39 #include "pw_log/log.h"
41 class FuzzedDataProvider {
43 FuzzedDataProvider(const uint8_t* data, size_t size) {
46 PW_LOG_INFO("Fuzzing is disabled for the current compiler.");
47 PW_LOG_INFO("Using trivial stub implementation for FuzzedDataProvider.");
50 ~FuzzedDataProvider() = default;
53 std::vector<T> ConsumeBytes(size_t num_bytes) {
55 return std::vector<T>{};
59 std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes,
62 return std::vector<T>{terminator};
66 std::vector<T> ConsumeRemainingBytes() {
67 return std::vector<T>{};
70 std::string ConsumeBytesAsString(size_t num_bytes) {
75 std::string ConsumeRandomLengthString(size_t max_length) {
76 PW_UNUSED(max_length);
80 std::string ConsumeRandomLengthString() { return std::string{}; }
82 std::string ConsumeRemainingBytesAsString() { return std::string{}; }
90 T ConsumeIntegralInRange(T min, T max) {
96 T ConsumeFloatingPoint() {
100 template <typename T>
101 T ConsumeFloatingPointInRange(T min, T max) {
106 template <typename T>
107 T ConsumeProbability() {
111 bool ConsumeBool() { return false; }
113 template <typename T>
115 return static_cast<T>(0);
118 template <typename T, size_t size>
119 T PickValueInArray(const T (&array)[size]) {
120 static_assert(size > 0, "The array must be non empty.");
124 template <typename T>
125 T PickValueInArray(std::initializer_list<const T> list) {
126 static_assert(list.size() > 0, "The list must be non empty.");
127 return *list.begin();
130 size_t ConsumeData(void* destination, size_t num_bytes) {
131 PW_UNUSED(destination);
132 PW_UNUSED(num_bytes);
136 size_t remaining_bytes() { return 0; }
139 #endif // defined(__clang__)