1 // Copyright 2022 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 #include <cstddef>
16 #include <cstdint>
17 #include <limits>
18
19 #include "pw_fuzzer/fuzzed_data_provider.h"
20 #include "pw_random/fuzzer.h"
21
22 namespace {
23 enum class IntegerType : uint8_t {
24 kUint8,
25 kUint16,
26 kUint32,
27 kUint64,
28 kMaxValue = kUint64,
29 };
30
31 template <typename T>
FuzzGetInt(FuzzedDataProvider * provider)32 void FuzzGetInt(FuzzedDataProvider* provider) {
33 pw::random::FuzzerRandomGenerator rng(provider);
34 T value = 0;
35 T bound =
36 provider->ConsumeIntegralInRange<T>(1, std::numeric_limits<T>::max());
37 rng.GetInt(value, bound);
38 PW_CHECK(value < bound);
39 }
40 } // namespace
41
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)42 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
43 FuzzedDataProvider provider(data, size);
44 switch (provider.ConsumeEnum<IntegerType>()) {
45 case IntegerType::kUint8:
46 FuzzGetInt<uint8_t>(&provider);
47 break;
48 case IntegerType::kUint16:
49 FuzzGetInt<uint16_t>(&provider);
50 break;
51 case IntegerType::kUint32:
52 FuzzGetInt<uint32_t>(&provider);
53 break;
54 case IntegerType::kUint64:
55 FuzzGetInt<uint64_t>(&provider);
56 break;
57 }
58 return 0;
59 }
60