Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_fuzzer / public / pw_fuzzer / fuzzed_data_provider.h
1 // Copyright 2020 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14
15 // Simply forwards to <fuzzer/FuzzedDataProvider.h> when available from clang,
16 // or provides a simple stub implementation.
17 #pragma once
18
19 #if defined(__clang__)
20 #include <fuzzer/FuzzedDataProvider.h>
21 #else  // !defined(__clang__)
22
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
29 // link.
30 //
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
34 #include <cstddef>
35 #include <cstdint>
36 #include <string>
37 #include <vector>
38
39 #include "pw_log/log.h"
40
41 class FuzzedDataProvider {
42  public:
43   FuzzedDataProvider(const uint8_t* data, size_t size) {
44     PW_UNUSED(data);
45     PW_UNUSED(size);
46     PW_LOG_INFO("Fuzzing is disabled for the current compiler.");
47     PW_LOG_INFO("Using trivial stub implementation for FuzzedDataProvider.");
48   }
49
50   ~FuzzedDataProvider() = default;
51
52   template <typename T>
53   std::vector<T> ConsumeBytes(size_t num_bytes) {
54     PW_UNUSED(num_bytes);
55     return std::vector<T>{};
56   }
57
58   template <typename T>
59   std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes,
60                                             T terminator = 0) {
61     PW_UNUSED(num_bytes);
62     return std::vector<T>{terminator};
63   }
64
65   template <typename T>
66   std::vector<T> ConsumeRemainingBytes() {
67     return std::vector<T>{};
68   }
69
70   std::string ConsumeBytesAsString(size_t num_bytes) {
71     PW_UNUSED(num_bytes);
72     return std::string{};
73   }
74
75   std::string ConsumeRandomLengthString(size_t max_length) {
76     PW_UNUSED(max_length);
77     return std::string{};
78   }
79
80   std::string ConsumeRandomLengthString() { return std::string{}; }
81
82   std::string ConsumeRemainingBytesAsString() { return std::string{}; }
83
84   template <typename T>
85   T ConsumeIntegral() {
86     return T(0);
87   }
88
89   template <typename T>
90   T ConsumeIntegralInRange(T min, T max) {
91     PW_UNUSED(max);
92     return T(min);
93   }
94
95   template <typename T>
96   T ConsumeFloatingPoint() {
97     return T(0.0);
98   }
99
100   template <typename T>
101   T ConsumeFloatingPointInRange(T min, T max) {
102     PW_UNUSED(max);
103     return T(min);
104   }
105
106   template <typename T>
107   T ConsumeProbability() {
108     return T(0.0);
109   }
110
111   bool ConsumeBool() { return false; }
112
113   template <typename T>
114   T ConsumeEnum() {
115     return static_cast<T>(0);
116   }
117
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.");
121     return array[0];
122   }
123
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();
128   }
129
130   size_t ConsumeData(void* destination, size_t num_bytes) {
131     PW_UNUSED(destination);
132     PW_UNUSED(num_bytes);
133     return 0;
134   }
135
136   size_t remaining_bytes() { return 0; }
137 };
138
139 #endif  // defined(__clang__)