2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
2020-01-23 17:26:19 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <memusage.h>
|
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
|
|
|
#include <test/fuzz/fuzz.h>
|
2021-06-07 13:40:12 +02:00
|
|
|
#include <test/fuzz/util.h>
|
2021-05-18 12:36:53 -07:00
|
|
|
#include <util/serfloat.h>
|
2020-01-23 17:26:19 +00:00
|
|
|
#include <version.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
2021-05-18 12:36:53 -07:00
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
2020-01-23 17:26:19 +00:00
|
|
|
|
2020-12-03 16:42:49 +01:00
|
|
|
FUZZ_TARGET(float)
|
2020-01-23 17:26:19 +00:00
|
|
|
{
|
|
|
|
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
|
|
|
|
|
|
|
{
|
2021-06-07 13:40:12 +02:00
|
|
|
const double d{[&] {
|
|
|
|
double tmp;
|
|
|
|
CallOneOf(
|
|
|
|
fuzzed_data_provider,
|
|
|
|
// an actual number
|
|
|
|
[&] { tmp = fuzzed_data_provider.ConsumeFloatingPoint<double>(); },
|
|
|
|
// special numbers and NANs
|
|
|
|
[&] { tmp = fuzzed_data_provider.PickValueInArray({
|
|
|
|
std::numeric_limits<double>::infinity(),
|
|
|
|
-std::numeric_limits<double>::infinity(),
|
|
|
|
std::numeric_limits<double>::min(),
|
|
|
|
-std::numeric_limits<double>::min(),
|
|
|
|
std::numeric_limits<double>::max(),
|
|
|
|
-std::numeric_limits<double>::max(),
|
|
|
|
std::numeric_limits<double>::lowest(),
|
|
|
|
-std::numeric_limits<double>::lowest(),
|
|
|
|
std::numeric_limits<double>::quiet_NaN(),
|
|
|
|
-std::numeric_limits<double>::quiet_NaN(),
|
|
|
|
std::numeric_limits<double>::signaling_NaN(),
|
|
|
|
-std::numeric_limits<double>::signaling_NaN(),
|
|
|
|
std::numeric_limits<double>::denorm_min(),
|
|
|
|
-std::numeric_limits<double>::denorm_min(),
|
|
|
|
}); },
|
|
|
|
// Anything from raw memory (also checks that DecodeDouble doesn't crash on any input)
|
|
|
|
[&] { tmp = DecodeDouble(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); });
|
|
|
|
return tmp;
|
|
|
|
}()};
|
2020-01-23 17:26:19 +00:00
|
|
|
(void)memusage::DynamicUsage(d);
|
|
|
|
|
2021-05-18 12:36:53 -07:00
|
|
|
uint64_t encoded = EncodeDouble(d);
|
|
|
|
if constexpr (std::numeric_limits<double>::is_iec559) {
|
|
|
|
if (!std::isnan(d)) {
|
|
|
|
uint64_t encoded_in_memory;
|
|
|
|
std::copy((const unsigned char*)&d, (const unsigned char*)(&d + 1), (unsigned char*)&encoded_in_memory);
|
|
|
|
assert(encoded_in_memory == encoded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double d_deserialized = DecodeDouble(encoded);
|
|
|
|
assert(std::isnan(d) == std::isnan(d_deserialized));
|
|
|
|
assert(std::isnan(d) || d == d_deserialized);
|
2020-01-23 17:26:19 +00:00
|
|
|
}
|
|
|
|
}
|