summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/fuzzer
diff options
context:
space:
mode:
authorMax Moroz <mmoroz@chromium.org>2019-08-08 19:49:37 +0000
committerMax Moroz <mmoroz@chromium.org>2019-08-08 19:49:37 +0000
commitdf3b465c9c4fed5045470d3eb5b65c14e6de71e6 (patch)
tree528a6cdb7323baa88acf92645cd2b94443ea3b5a /compiler-rt/lib/fuzzer
parentd9cbd2acfa7d205788c43abdc66f6ee9680748ce (diff)
downloadbcm5719-llvm-df3b465c9c4fed5045470d3eb5b65c14e6de71e6.tar.gz
bcm5719-llvm-df3b465c9c4fed5045470d3eb5b65c14e6de71e6.zip
[compiler-rt] Add ConsumeProbability and ConsumeFloatingPoint methods to FDP.
Summary: Also slightly cleaned up the comments and changed the header's extension back to `.h` as per comments on https://reviews.llvm.org/D65812. New methods added: * `ConsumeProbability` returns [0.0, 1.0] by consuming an unsigned integer value from the input data and dividing that value by the integer's max value. * `ConsumeFloatingPointInRange` returns a floating point value in the given range. Relies on `ConsumeProbability` method. This method does not have the limitation of `std::uniform_real_distribution` that requires the given range to be <= the floating point type's max. If the range is too large, this implementation will additionally call `ConsumeBool` to decide whether the result will be in the first or the second half of the range. * `ConsumeFloatingPoint` returns a floating point value in the range `[std::numeric_limits<T>::lowest(), std::numeric_limits<T>::min()]`. Tested on Linux, Mac, Windows. Reviewers: morehouse Reviewed By: morehouse Subscribers: kubamracek, mgorny, dberris, delcypher, #sanitizers, llvm-commits Tags: #llvm, #sanitizers Differential Revision: https://reviews.llvm.org/D65905 llvm-svn: 368331
Diffstat (limited to 'compiler-rt/lib/fuzzer')
-rw-r--r--compiler-rt/lib/fuzzer/tests/CMakeLists.txt2
-rw-r--r--compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp53
-rw-r--r--compiler-rt/lib/fuzzer/utils/FuzzedDataProvider.h2
3 files changed, 54 insertions, 3 deletions
diff --git a/compiler-rt/lib/fuzzer/tests/CMakeLists.txt b/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
index 69e67ab0d50..cfb039c77d0 100644
--- a/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
+++ b/compiler-rt/lib/fuzzer/tests/CMakeLists.txt
@@ -79,7 +79,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
generate_compiler_rt_tests(FuzzedDataProviderTestObjects
FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
- DEPS gtest ${LIBFUZZER_TEST_RUNTIME_DEPS}
+ DEPS gtest ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES
diff --git a/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp b/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
index 36f2090b1ec..222283434eb 100644
--- a/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
+++ b/compiler-rt/lib/fuzzer/tests/FuzzedDataProviderUnittest.cpp
@@ -6,7 +6,7 @@
#include <cstdint>
#include <cstdlib>
-#include <fuzzer/FuzzedDataProvider.hpp>
+#include <fuzzer/FuzzedDataProvider.h>
// The test is intentionally extensive, as behavior of |FuzzedDataProvider| must
// not be broken, given than many fuzz targets depend on it. Changing the
@@ -348,6 +348,57 @@ TEST(FuzzedDataProvider, remaining_bytes) {
EXPECT_EQ(size_t(0), DataProv.remaining_bytes());
}
+TEST(FuzzedDataProvider, ConsumeProbability) {
+ FuzzedDataProvider DataProv(Data, sizeof(Data));
+ ASSERT_FLOAT_EQ(float(0.28969181), DataProv.ConsumeProbability<float>());
+ ASSERT_DOUBLE_EQ(double(0.086814121166605432),
+ DataProv.ConsumeProbability<double>());
+ ASSERT_FLOAT_EQ(float(0.30104411), DataProv.ConsumeProbability<float>());
+ ASSERT_DOUBLE_EQ(double(0.96218831486039413),
+ DataProv.ConsumeProbability<double>());
+ ASSERT_FLOAT_EQ(float(0.67005056), DataProv.ConsumeProbability<float>());
+ ASSERT_DOUBLE_EQ(double(0.69210584173832279),
+ DataProv.ConsumeProbability<double>());
+
+ // Exhaust the buffer.
+ EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data) - 36),
+ DataProv.ConsumeRemainingBytes<uint8_t>());
+ ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
+}
+
+TEST(FuzzedDataProvider, ConsumeFloatingPoint) {
+ FuzzedDataProvider DataProv(Data, sizeof(Data));
+ ASSERT_FLOAT_EQ(float(-2.8546307e+38),
+ DataProv.ConsumeFloatingPoint<float>());
+ ASSERT_DOUBLE_EQ(double(8.0940194040236032e+307),
+ DataProv.ConsumeFloatingPoint<double>());
+ ASSERT_FLOAT_EQ(float(271.49084),
+ DataProv.ConsumeFloatingPointInRange<float>(123.0, 777.0));
+ ASSERT_DOUBLE_EQ(double(30.859126145478349),
+ DataProv.ConsumeFloatingPointInRange<double>(13.37, 31.337));
+ ASSERT_FLOAT_EQ(
+ float(-903.47729),
+ DataProv.ConsumeFloatingPointInRange<float>(-999.9999, -777.77));
+ ASSERT_DOUBLE_EQ(
+ double(24.561393182922771),
+ DataProv.ConsumeFloatingPointInRange<double>(-13.37, 31.337));
+ ASSERT_FLOAT_EQ(float(1.0),
+ DataProv.ConsumeFloatingPointInRange<float>(1.0, 1.0));
+ ASSERT_DOUBLE_EQ(double(-1.0),
+ DataProv.ConsumeFloatingPointInRange<double>(-1.0, -1.0));
+
+ // Exhaust the buffer.
+ EXPECT_EQ((std::vector<uint8_t>(Data, Data + sizeof(Data) - 50)).size(),
+ DataProv.ConsumeRemainingBytes<uint8_t>().size());
+ ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
+ ASSERT_NEAR(std::numeric_limits<double>::lowest(),
+ DataProv.ConsumeFloatingPoint<double>(), 1e-10);
+ ASSERT_FLOAT_EQ(float(123.0),
+ DataProv.ConsumeFloatingPointInRange<float>(123.0, 777.0));
+ ASSERT_DOUBLE_EQ(double(-13.37), DataProv.ConsumeFloatingPointInRange<double>(
+ -13.37, 31.337));
+}
+
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
diff --git a/compiler-rt/lib/fuzzer/utils/FuzzedDataProvider.h b/compiler-rt/lib/fuzzer/utils/FuzzedDataProvider.h
index dd2ea20b906..5692060c7f7 100644
--- a/compiler-rt/lib/fuzzer/utils/FuzzedDataProvider.h
+++ b/compiler-rt/lib/fuzzer/utils/FuzzedDataProvider.h
@@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
-// This a temporary copy of compiler-rt/include/fuzzer/FuzzedDataProvider.hpp.
+// This a temporary copy of compiler-rt/include/fuzzer/FuzzedDataProvider.h.
// TODO(mmoroz@chromium.org): delete this copy.
// A single header library providing an utility class to break up an array of
// bytes. Whenever run on the same input, provides the same output, as long as
OpenPOWER on IntegriCloud