Skip to content

Commit

Permalink
Add fuzz test for FeeFrac
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Jan 18, 2024
1 parent ebcacdb commit 5e48ade
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/descriptor_parse.cpp \
test/fuzz/deserialize.cpp \
test/fuzz/eval_script.cpp \
test/fuzz/feefrac.cpp \
test/fuzz/fee_rate.cpp \
test/fuzz/feeratediagram.cpp \
test/fuzz/fees.cpp \
Expand Down
117 changes: 117 additions & 0 deletions src/test/fuzz/feefrac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) 2024 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/feefrac.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>

#include <compare>
#include <cstdint>
#include <iostream>

namespace {

/** Compute a * b, represented in 4x32 bits, highest limb first. */
std::array<uint32_t, 4> Mul128(uint64_t a, uint64_t b)
{
std::array<uint32_t, 4> ret{0, 0, 0, 0};

/** Perform ret += v << (32 * pos), at 128-bit precision. */
auto add_fn = [&](uint64_t v, int pos) {
uint32_t carry = 0;
for (int i = 0; i + pos < 4; ++i) {
uint32_t& limb = ret[3 - pos - i];
uint32_t old = limb;
// Add low or high half of v.
if (i == 0) limb += v;
if (i == 1) limb += v >> 32;
// Add carry from previous position.
limb += carry;
// Compute carry for next position.
carry = (limb < old) || ((limb == old) && carry);
}
// Make sure no overflow.
assert(carry == 0);
};

// Multiply the 4 individual limbs (schoolbook multiply, with base 2^64).
add_fn((a & 0xffffffff) * (b & 0xffffffff), 0);
add_fn((a >> 32) * (b & 0xffffffff), 1);
add_fn((a & 0xffffffff) * (b >> 32), 1);
add_fn((a >> 32) * (b >> 32), 2);
return ret;
}

std::strong_ordering MulCompare(int64_t a1, int64_t a2, int64_t b1, int64_t b2)
{
// Compute and compare signs.
int sign_a = (a1 == 0 ? 0 : a1 < 0 ? -1 : 1) * (a2 == 0 ? 0 : a2 < 0 ? -1 : 1);
int sign_b = (b1 == 0 ? 0 : b1 < 0 ? -1 : 1) * (b2 == 0 ? 0 : b2 < 0 ? -1 : 1);
if (sign_a != sign_b) return sign_a <=> sign_b;

// Compute absolute values.
uint64_t abs_a1 = static_cast<uint64_t>(a1), abs_a2 = static_cast<uint64_t>(a2);
uint64_t abs_b1 = static_cast<uint64_t>(b1), abs_b2 = static_cast<uint64_t>(b2);
if (a1 < 0) abs_a1 = -abs_a1;
if (a2 < 0) abs_a2 = -abs_a2;
if (b1 < 0) abs_b1 = -abs_b1;
if (b2 < 0) abs_b2 = -abs_b2;

// Compute products of absolute values.
auto mul_abs_a = Mul128(abs_a1, abs_a2);
auto mul_abs_b = Mul128(abs_b1, abs_b2);
if (sign_a < 0) {
return mul_abs_b <=> mul_abs_a;
} else {
return mul_abs_a <=> mul_abs_b;
}
}

} // namespace

FUZZ_TARGET(feefrac)
{
FuzzedDataProvider provider(buffer.data(), buffer.size());

int64_t f1 = provider.ConsumeIntegral<int64_t>();
//int64_t f1 = provider.ConsumeIntegralInRange(std::numeric_limits<int64_t>::min() + 1,
// std::numeric_limits<int64_t>::max() - 1);
int32_t s1 = provider.ConsumeIntegral<int32_t>();
if (s1 == 0) f1 = 0;
FeeFrac fr1(f1, s1);
assert(fr1.IsEmpty() == (s1 == 0));

int64_t f2 = provider.ConsumeIntegral<int64_t>();
//int64_t f2 = provider.ConsumeIntegralInRange(std::numeric_limits<int64_t>::min() + 1,
// std::numeric_limits<int64_t>::max() - 1);
int32_t s2 = provider.ConsumeIntegral<int32_t>();
if (s2 == 0) f2 = 0;
FeeFrac fr2(f2, s2);
assert(fr2.IsEmpty() == (s2 == 0));

// Feerate comparisons
auto cmp_feerate = MulCompare(f1, s2, f2, s1);
assert(FeeRateCompare(fr1, fr2) == cmp_feerate);
assert((fr1 << fr2) == (cmp_feerate < 0));
assert((fr1 >> fr2) == (cmp_feerate > 0));

// Compare with manual invocation of FeeFrac::Mul.
auto cmp_mul = FeeFrac::Mul(f1, s2) <=> FeeFrac::Mul(f2, s1);
assert(cmp_mul == cmp_feerate);

// Same, but using FeeFrac::MulFallback.
auto cmp_fallback = FeeFrac::MulFallback(f1, s2) <=> FeeFrac::MulFallback(f2, s1);
assert(cmp_fallback == cmp_feerate);

// Total order comparisons
auto cmp_total = (cmp_feerate == 0) ? (s2 <=> s1) : cmp_feerate;
assert((fr1 <=> fr2) == cmp_total);
assert((fr1 < fr2) == (cmp_total < 0));
assert((fr1 > fr2) == (cmp_total > 0));
assert((fr1 <= fr2) == (cmp_total <= 0));
assert((fr1 >= fr2) == (cmp_total >= 0));
assert((fr1 == fr2) == (cmp_total == 0));
assert((fr1 != fr2) == (cmp_total != 0));
}
3 changes: 3 additions & 0 deletions test/sanitizer_suppressions/ubsan
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ unsigned-integer-overflow:CCoinsViewCache::Uncache
unsigned-integer-overflow:CompressAmount
unsigned-integer-overflow:DecompressAmount
unsigned-integer-overflow:crypto/
unsigned-integer-overflow:MulCompare
unsigned-integer-overflow:MurmurHash3
unsigned-integer-overflow:CBlockPolicyEstimator::processBlockTx
unsigned-integer-overflow:TxConfirmStats::EstimateMedianVal
Expand All @@ -67,6 +68,8 @@ implicit-integer-sign-change:EvalScript
implicit-integer-sign-change:serialize.h
implicit-signed-integer-truncation:crypto/
implicit-unsigned-integer-truncation:crypto/
implicit-unsigned-integer-truncation:Mul128
implicit-unsigned-integer-truncation:MulCompare
shift-base:arith_uint256.cpp
shift-base:crypto/
shift-base:ROTL32
Expand Down

0 comments on commit 5e48ade

Please sign in to comment.