| Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2022 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <fuzzer/FuzzedDataProvider.h> | 
|  | 18 |  | 
|  | 19 | /** | 
|  | 20 | * A thread-safe interface to the FuzzedDataProvider | 
|  | 21 | */ | 
|  | 22 | class ThreadSafeFuzzedDataProvider : FuzzedDataProvider { | 
|  | 23 | private: | 
|  | 24 | std::mutex mLock; | 
|  | 25 |  | 
|  | 26 | public: | 
|  | 27 | ThreadSafeFuzzedDataProvider(const uint8_t* data, size_t size) | 
|  | 28 | : FuzzedDataProvider(data, size) {} | 
|  | 29 |  | 
|  | 30 | template <typename T> | 
|  | 31 | std::vector<T> ConsumeBytes(size_t num_bytes) { | 
|  | 32 | std::scoped_lock _l(mLock); | 
|  | 33 | return FuzzedDataProvider::ConsumeBytes<T>(num_bytes); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | template <typename T> | 
|  | 37 | std::vector<T> ConsumeBytesWithTerminator(size_t num_bytes, T terminator) { | 
|  | 38 | std::scoped_lock _l(mLock); | 
|  | 39 | return FuzzedDataProvider::ConsumeBytesWithTerminator<T>(num_bytes, terminator); | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | template <typename T> | 
|  | 43 | std::vector<T> ConsumeRemainingBytes() { | 
|  | 44 | std::scoped_lock _l(mLock); | 
|  | 45 | return FuzzedDataProvider::ConsumeRemainingBytes<T>(); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | std::string ConsumeBytesAsString(size_t num_bytes) { | 
|  | 49 | std::scoped_lock _l(mLock); | 
|  | 50 | return FuzzedDataProvider::ConsumeBytesAsString(num_bytes); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | std::string ConsumeRandomLengthString(size_t max_length) { | 
|  | 54 | std::scoped_lock _l(mLock); | 
|  | 55 | return FuzzedDataProvider::ConsumeRandomLengthString(max_length); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | std::string ConsumeRandomLengthString() { | 
|  | 59 | std::scoped_lock _l(mLock); | 
|  | 60 | return FuzzedDataProvider::ConsumeRandomLengthString(); | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | std::string ConsumeRemainingBytesAsString() { | 
|  | 64 | std::scoped_lock _l(mLock); | 
|  | 65 | return FuzzedDataProvider::ConsumeRemainingBytesAsString(); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | template <typename T> | 
|  | 69 | T ConsumeIntegral() { | 
|  | 70 | std::scoped_lock _l(mLock); | 
|  | 71 | return FuzzedDataProvider::ConsumeIntegral<T>(); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | template <typename T> | 
|  | 75 | T ConsumeIntegralInRange(T min, T max) { | 
|  | 76 | std::scoped_lock _l(mLock); | 
|  | 77 | return FuzzedDataProvider::ConsumeIntegralInRange<T>(min, max); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | template <typename T> | 
|  | 81 | T ConsumeFloatingPoint() { | 
|  | 82 | std::scoped_lock _l(mLock); | 
|  | 83 | return FuzzedDataProvider::ConsumeFloatingPoint<T>(); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | template <typename T> | 
|  | 87 | T ConsumeFloatingPointInRange(T min, T max) { | 
|  | 88 | std::scoped_lock _l(mLock); | 
|  | 89 | return FuzzedDataProvider::ConsumeFloatingPointInRange<T>(min, max); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | template <typename T> | 
|  | 93 | T ConsumeProbability() { | 
|  | 94 | std::scoped_lock _l(mLock); | 
|  | 95 | return FuzzedDataProvider::ConsumeProbability<T>(); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | bool ConsumeBool() { | 
|  | 99 | std::scoped_lock _l(mLock); | 
|  | 100 | return FuzzedDataProvider::ConsumeBool(); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | template <typename T> | 
|  | 104 | T ConsumeEnum() { | 
|  | 105 | std::scoped_lock _l(mLock); | 
|  | 106 | return FuzzedDataProvider::ConsumeEnum<T>(); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | template <typename T, size_t size> | 
|  | 110 | T PickValueInArray(const T (&array)[size]) { | 
|  | 111 | std::scoped_lock _l(mLock); | 
|  | 112 | return FuzzedDataProvider::PickValueInArray(array); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | template <typename T, size_t size> | 
|  | 116 | T PickValueInArray(const std::array<T, size>& array) { | 
|  | 117 | std::scoped_lock _l(mLock); | 
|  | 118 | return FuzzedDataProvider::PickValueInArray(array); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | template <typename T> | 
|  | 122 | T PickValueInArray(std::initializer_list<const T> list) { | 
|  | 123 | std::scoped_lock _l(mLock); | 
|  | 124 | return FuzzedDataProvider::PickValueInArray(list); | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | size_t ConsumeData(void* destination, size_t num_bytes) { | 
|  | 128 | std::scoped_lock _l(mLock); | 
|  | 129 | return FuzzedDataProvider::ConsumeData(destination, num_bytes); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | size_t remaining_bytes() { | 
|  | 133 | std::scoped_lock _l(mLock); | 
|  | 134 | return FuzzedDataProvider::remaining_bytes(); | 
|  | 135 | } | 
|  | 136 | }; |