[llvm-branch-commits] [compiler-rt] 70de7e0 - [compiler-rt] FuzzedDataProvider: Add PickValueInArray for std::array

Max Moroz via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Dec 30 10:33:28 PST 2020


Author: Max Moroz
Date: 2020-12-30T10:25:26-08:00
New Revision: 70de7e0d9a95b7fcd7c105b06bd90fdf4e01f563

URL: https://github.com/llvm/llvm-project/commit/70de7e0d9a95b7fcd7c105b06bd90fdf4e01f563
DIFF: https://github.com/llvm/llvm-project/commit/70de7e0d9a95b7fcd7c105b06bd90fdf4e01f563.diff

LOG: [compiler-rt] FuzzedDataProvider: Add PickValueInArray for std::array

This makes `PickValueInArray` work for `std::array<T, s>` (C++11). I've also tested the C++17 `std::array` (with compiler-deduced template parameters)

```
Author:
MarcoFalke <falke.marco at gmail.com>
```

Reviewed By: Dor1s

Differential Revision: https://reviews.llvm.org/D93412

Added: 
    

Modified: 
    compiler-rt/include/fuzzer/FuzzedDataProvider.h
    compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/include/fuzzer/FuzzedDataProvider.h b/compiler-rt/include/fuzzer/FuzzedDataProvider.h
index 83bcd0134a7d..744a9d78cec0 100644
--- a/compiler-rt/include/fuzzer/FuzzedDataProvider.h
+++ b/compiler-rt/include/fuzzer/FuzzedDataProvider.h
@@ -14,6 +14,7 @@
 #define LLVM_FUZZER_FUZZED_DATA_PROVIDER_H_
 
 #include <algorithm>
+#include <array>
 #include <climits>
 #include <cstddef>
 #include <cstdint>
@@ -71,6 +72,8 @@ class FuzzedDataProvider {
 
   // Returns a value from the given array.
   template <typename T, size_t size> T PickValueInArray(const T (&array)[size]);
+  template <typename T, size_t size>
+  T PickValueInArray(const std::array<T, size> &array);
   template <typename T> T PickValueInArray(std::initializer_list<const T> list);
 
   // Writes data to the given destination and returns number of bytes written.
@@ -301,6 +304,12 @@ T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) {
   return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
 }
 
+template <typename T, size_t size>
+T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) {
+  static_assert(size > 0, "The array must be non empty.");
+  return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
+}
+
 template <typename T>
 T FuzzedDataProvider::PickValueInArray(std::initializer_list<const T> list) {
   // TODO(Dor1s): switch to static_assert once C++14 is allowed.

diff  --git a/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp b/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
index 99d9d8ecbe9b..ea6774e5a5cd 100644
--- a/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
+++ b/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
@@ -283,6 +283,20 @@ TEST(FuzzedDataProvider, ConsumeBool) {
   EXPECT_EQ(false, DataProv.ConsumeBool());
 }
 
+TEST(FuzzedDataProvider, PickValueInStdArray) {
+  FuzzedDataProvider DataProv(Data, sizeof(Data));
+  const std::array<int, 5> Array = {1, 2, 3, 4, 5};
+  EXPECT_EQ(5, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(2, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(2, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(3, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(3, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(3, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(1, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(3, DataProv.PickValueInArray(Array));
+  EXPECT_EQ(2, DataProv.PickValueInArray(Array));
+}
+
 TEST(FuzzedDataProvider, PickValueInArray) {
   FuzzedDataProvider DataProv(Data, sizeof(Data));
   const int Array[] = {1, 2, 3, 4, 5};


        


More information about the llvm-branch-commits mailing list