aboutsummaryrefslogtreecommitdiff
path: root/src/test/fuzz
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/fuzz')
-rw-r--r--src/test/fuzz/base_encode_decode.cpp3
-rw-r--r--src/test/fuzz/bitset.cpp316
-rw-r--r--src/test/fuzz/fees.cpp4
-rw-r--r--src/test/fuzz/fuzz.cpp5
-rw-r--r--src/test/fuzz/fuzz.h5
-rw-r--r--src/test/fuzz/i2p.cpp63
-rw-r--r--src/test/fuzz/integer.cpp2
-rw-r--r--src/test/fuzz/kitchen_sink.cpp11
-rw-r--r--src/test/fuzz/locale.cpp4
-rw-r--r--src/test/fuzz/message.cpp2
-rw-r--r--src/test/fuzz/mini_miner.cpp12
-rw-r--r--src/test/fuzz/muhash.cpp12
-rw-r--r--src/test/fuzz/package_eval.cpp19
-rw-r--r--src/test/fuzz/partially_downloaded_block.cpp6
-rw-r--r--src/test/fuzz/rbf.cpp34
-rw-r--r--src/test/fuzz/rpc.cpp3
-rw-r--r--src/test/fuzz/script_assets_test_minimizer.cpp2
-rw-r--r--src/test/fuzz/script_parsing.cpp (renamed from src/test/fuzz/spanparsing.cpp)15
-rw-r--r--src/test/fuzz/string.cpp13
-rw-r--r--src/test/fuzz/timeoffsets.cpp4
-rw-r--r--src/test/fuzz/tx_pool.cpp22
-rw-r--r--src/test/fuzz/util.cpp4
-rw-r--r--src/test/fuzz/util/net.cpp70
-rw-r--r--src/test/fuzz/util/net.h2
-rw-r--r--src/test/fuzz/validation_load_mempool.cpp6
-rw-r--r--src/test/fuzz/vecdeque.cpp491
26 files changed, 1036 insertions, 94 deletions
diff --git a/src/test/fuzz/base_encode_decode.cpp b/src/test/fuzz/base_encode_decode.cpp
index d322416d34..0cc8cb5886 100644
--- a/src/test/fuzz/base_encode_decode.cpp
+++ b/src/test/fuzz/base_encode_decode.cpp
@@ -14,6 +14,9 @@
#include <string>
#include <vector>
+using util::TrimString;
+using util::TrimStringView;
+
FUZZ_TARGET(base_encode_decode)
{
const std::string random_encoded_string(buffer.begin(), buffer.end());
diff --git a/src/test/fuzz/bitset.cpp b/src/test/fuzz/bitset.cpp
new file mode 100644
index 0000000000..7684337729
--- /dev/null
+++ b/src/test/fuzz/bitset.cpp
@@ -0,0 +1,316 @@
+// Copyright (c) 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 <span.h>
+#include <test/fuzz/util.h>
+#include <test/util/xoroshiro128plusplus.h>
+#include <util/bitset.h>
+
+#include <bitset>
+#include <vector>
+
+namespace {
+
+/** Pop the first byte from a byte-span, and return it. */
+uint8_t ReadByte(FuzzBufferType& buffer)
+{
+ if (buffer.empty()) return 0;
+ uint8_t ret = buffer.front();
+ buffer = buffer.subspan(1);
+ return ret;
+}
+
+/** Perform a simulation fuzz test on BitSet type S. */
+template<typename S>
+void TestType(FuzzBufferType buffer)
+{
+ /** This fuzz test's design is based on the assumption that the actual bits stored in the
+ * bitsets and their simulations do not matter for the purpose of detecting edge cases, thus
+ * these are taken from a deterministically-seeded RNG instead. To provide some level of
+ * variation however, pick the seed based on the buffer size and size of the chosen bitset. */
+ XoRoShiRo128PlusPlus rng(buffer.size() + 0x10000 * S::Size());
+
+ using Sim = std::bitset<S::Size()>;
+ // Up to 4 real BitSets (initially 2).
+ std::vector<S> real(2);
+ // Up to 4 std::bitsets with the same corresponding contents.
+ std::vector<Sim> sim(2);
+
+ /* Compare sim[idx] with real[idx], using all inspector operations. */
+ auto compare_fn = [&](unsigned idx) {
+ /* iterators and operator[] */
+ auto it = real[idx].begin();
+ unsigned first = S::Size();
+ unsigned last = S::Size();
+ for (unsigned i = 0; i < S::Size(); ++i) {
+ bool match = (it != real[idx].end()) && *it == i;
+ assert(sim[idx][i] == real[idx][i]);
+ assert(match == real[idx][i]);
+ assert((it == real[idx].end()) != (it != real[idx].end()));
+ if (match) {
+ ++it;
+ if (first == S::Size()) first = i;
+ last = i;
+ }
+ }
+ assert(it == real[idx].end());
+ assert(!(it != real[idx].end()));
+ /* Any / None */
+ assert(sim[idx].any() == real[idx].Any());
+ assert(sim[idx].none() == real[idx].None());
+ /* First / Last */
+ if (sim[idx].any()) {
+ assert(first == real[idx].First());
+ assert(last == real[idx].Last());
+ }
+ /* Count */
+ assert(sim[idx].count() == real[idx].Count());
+ };
+
+ LIMITED_WHILE(buffer.size() > 0, 1000) {
+ // Read one byte to determine which operation to execute on the BitSets.
+ int command = ReadByte(buffer) % 64;
+ // Read another byte that determines which bitsets will be involved.
+ unsigned args = ReadByte(buffer);
+ unsigned dest = ((args & 7) * sim.size()) >> 3;
+ unsigned src = (((args >> 3) & 7) * sim.size()) >> 3;
+ unsigned aux = (((args >> 6) & 3) * sim.size()) >> 2;
+ // Args are in range for non-empty sim, or sim is completely empty and will be grown
+ assert((sim.empty() && dest == 0 && src == 0 && aux == 0) ||
+ (!sim.empty() && dest < sim.size() && src < sim.size() && aux < sim.size()));
+
+ // Pick one operation based on value of command. Not all operations are always applicable.
+ // Loop through the applicable ones until command reaches 0 (which avoids the need to
+ // compute the number of applicable commands ahead of time).
+ while (true) {
+ if (dest < sim.size() && command-- == 0) {
+ /* Set() (true) */
+ unsigned val = ReadByte(buffer) % S::Size();
+ assert(sim[dest][val] == real[dest][val]);
+ sim[dest].set(val);
+ real[dest].Set(val);
+ break;
+ } else if (dest < sim.size() && command-- == 0) {
+ /* Reset() */
+ unsigned val = ReadByte(buffer) % S::Size();
+ assert(sim[dest][val] == real[dest][val]);
+ sim[dest].reset(val);
+ real[dest].Reset(val);
+ break;
+ } else if (dest < sim.size() && command-- == 0) {
+ /* Set() (conditional) */
+ unsigned val = ReadByte(buffer) % S::Size();
+ assert(sim[dest][val] == real[dest][val]);
+ sim[dest].set(val, args >> 7);
+ real[dest].Set(val, args >> 7);
+ break;
+ } else if (sim.size() < 4 && command-- == 0) {
+ /* Construct empty. */
+ sim.resize(sim.size() + 1);
+ real.resize(real.size() + 1);
+ break;
+ } else if (sim.size() < 4 && command-- == 0) {
+ /* Construct singleton. */
+ unsigned val = ReadByte(buffer) % S::Size();
+ std::bitset<S::Size()> newset;
+ newset[val] = true;
+ sim.push_back(newset);
+ real.push_back(S::Singleton(val));
+ break;
+ } else if (dest < sim.size() && command-- == 0) {
+ /* Make random. */
+ compare_fn(dest);
+ sim[dest].reset();
+ real[dest] = S{};
+ for (unsigned i = 0; i < S::Size(); ++i) {
+ if (rng() & 1) {
+ sim[dest][i] = true;
+ real[dest].Set(i);
+ }
+ }
+ break;
+ } else if (dest < sim.size() && command-- == 0) {
+ /* Assign initializer list. */
+ unsigned r1 = rng() % S::Size();
+ unsigned r2 = rng() % S::Size();
+ unsigned r3 = rng() % S::Size();
+ compare_fn(dest);
+ sim[dest].reset();
+ real[dest] = {r1, r2, r3};
+ sim[dest].set(r1);
+ sim[dest].set(r2);
+ sim[dest].set(r3);
+ break;
+ } else if (!sim.empty() && command-- == 0) {
+ /* Destruct. */
+ compare_fn(sim.size() - 1);
+ sim.pop_back();
+ real.pop_back();
+ break;
+ } else if (sim.size() < 4 && src < sim.size() && command-- == 0) {
+ /* Copy construct. */
+ sim.emplace_back(sim[src]);
+ real.emplace_back(real[src]);
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* Copy assign. */
+ compare_fn(dest);
+ sim[dest] = sim[src];
+ real[dest] = real[src];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* swap() function. */
+ swap(sim[dest], sim[src]);
+ swap(real[dest], real[src]);
+ break;
+ } else if (sim.size() < 4 && command-- == 0) {
+ /* Construct with initializer list. */
+ unsigned r1 = rng() % S::Size();
+ unsigned r2 = rng() % S::Size();
+ sim.emplace_back();
+ sim.back().set(r1);
+ sim.back().set(r2);
+ real.push_back(S{r1, r2});
+ break;
+ } else if (dest < sim.size() && command-- == 0) {
+ /* Fill() + copy assign. */
+ unsigned len = ReadByte(buffer) % S::Size();
+ compare_fn(dest);
+ sim[dest].reset();
+ for (unsigned i = 0; i < len; ++i) sim[dest][i] = true;
+ real[dest] = S::Fill(len);
+ break;
+ } else if (src < sim.size() && command-- == 0) {
+ /* Iterator copy based compare. */
+ unsigned val = ReadByte(buffer) % S::Size();
+ /* In a first loop, compare begin..end, and copy to it_copy at some point. */
+ auto it = real[src].begin(), it_copy = it;
+ for (unsigned i = 0; i < S::Size(); ++i) {
+ if (i == val) it_copy = it;
+ bool match = (it != real[src].end()) && *it == i;
+ assert(match == sim[src][i]);
+ if (match) ++it;
+ }
+ assert(it == real[src].end());
+ /* Then compare from the copied point again to end. */
+ for (unsigned i = val; i < S::Size(); ++i) {
+ bool match = (it_copy != real[src].end()) && *it_copy == i;
+ assert(match == sim[src][i]);
+ if (match) ++it_copy;
+ }
+ assert(it_copy == real[src].end());
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* operator|= */
+ compare_fn(dest);
+ sim[dest] |= sim[src];
+ real[dest] |= real[src];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* operator&= */
+ compare_fn(dest);
+ sim[dest] &= sim[src];
+ real[dest] &= real[src];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* operator-= */
+ compare_fn(dest);
+ sim[dest] &= ~sim[src];
+ real[dest] -= real[src];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ /* operator^= */
+ compare_fn(dest);
+ sim[dest] ^= sim[src];
+ real[dest] ^= real[src];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ /* operator| */
+ compare_fn(dest);
+ sim[dest] = sim[src] | sim[aux];
+ real[dest] = real[src] | real[aux];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ /* operator& */
+ compare_fn(dest);
+ sim[dest] = sim[src] & sim[aux];
+ real[dest] = real[src] & real[aux];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ /* operator- */
+ compare_fn(dest);
+ sim[dest] = sim[src] & ~sim[aux];
+ real[dest] = real[src] - real[aux];
+ break;
+ } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ /* operator^ */
+ compare_fn(dest);
+ sim[dest] = sim[src] ^ sim[aux];
+ real[dest] = real[src] ^ real[aux];
+ break;
+ } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ /* IsSupersetOf() and IsSubsetOf() */
+ bool is_superset = (sim[aux] & ~sim[src]).none();
+ bool is_subset = (sim[src] & ~sim[aux]).none();
+ assert(real[src].IsSupersetOf(real[aux]) == is_superset);
+ assert(real[src].IsSubsetOf(real[aux]) == is_subset);
+ assert(real[aux].IsSupersetOf(real[src]) == is_subset);
+ assert(real[aux].IsSubsetOf(real[src]) == is_superset);
+ break;
+ } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ /* operator== and operator!= */
+ assert((sim[src] == sim[aux]) == (real[src] == real[aux]));
+ assert((sim[src] != sim[aux]) == (real[src] != real[aux]));
+ break;
+ } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ /* Overlaps() */
+ assert((sim[src] & sim[aux]).any() == real[src].Overlaps(real[aux]));
+ assert((sim[src] & sim[aux]).any() == real[aux].Overlaps(real[src]));
+ break;
+ }
+ }
+ }
+ /* Fully compare the final state. */
+ for (unsigned i = 0; i < sim.size(); ++i) {
+ compare_fn(i);
+ }
+}
+
+} // namespace
+
+FUZZ_TARGET(bitset)
+{
+ unsigned typdat = ReadByte(buffer) % 8;
+ if (typdat == 0) {
+ /* 16 bits */
+ TestType<bitset_detail::IntBitSet<uint16_t>>(buffer);
+ TestType<bitset_detail::MultiIntBitSet<uint16_t, 1>>(buffer);
+ } else if (typdat == 1) {
+ /* 32 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint16_t, 2>>(buffer);
+ TestType<bitset_detail::IntBitSet<uint32_t>>(buffer);
+ } else if (typdat == 2) {
+ /* 48 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint16_t, 3>>(buffer);
+ } else if (typdat == 3) {
+ /* 64 bits */
+ TestType<bitset_detail::IntBitSet<uint64_t>>(buffer);
+ TestType<bitset_detail::MultiIntBitSet<uint64_t, 1>>(buffer);
+ TestType<bitset_detail::MultiIntBitSet<uint32_t, 2>>(buffer);
+ TestType<bitset_detail::MultiIntBitSet<uint16_t, 4>>(buffer);
+ } else if (typdat == 4) {
+ /* 96 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint32_t, 3>>(buffer);
+ } else if (typdat == 5) {
+ /* 128 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint64_t, 2>>(buffer);
+ TestType<bitset_detail::MultiIntBitSet<uint32_t, 4>>(buffer);
+ } else if (typdat == 6) {
+ /* 192 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint64_t, 3>>(buffer);
+ } else if (typdat == 7) {
+ /* 256 bits */
+ TestType<bitset_detail::MultiIntBitSet<uint64_t, 4>>(buffer);
+ }
+}
diff --git a/src/test/fuzz/fees.cpp b/src/test/fuzz/fees.cpp
index 38a8c6798e..5c760be13d 100644
--- a/src/test/fuzz/fees.cpp
+++ b/src/test/fuzz/fees.cpp
@@ -2,17 +2,19 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <common/messages.h>
#include <consensus/amount.h>
#include <policy/fees.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
-#include <util/fees.h>
#include <cstdint>
#include <string>
#include <vector>
+using common::StringForFeeReason;
+
FUZZ_TARGET(fees)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp
index 9a54a44bd3..c1c9945a04 100644
--- a/src/test/fuzz/fuzz.cpp
+++ b/src/test/fuzz/fuzz.cpp
@@ -101,8 +101,9 @@ void ResetCoverageCounters() {}
void initialize()
{
- // Terminate immediately if a fuzzing harness ever tries to create a TCP socket.
- CreateSock = [](const sa_family_t&) -> std::unique_ptr<Sock> { std::terminate(); };
+ // Terminate immediately if a fuzzing harness ever tries to create a socket.
+ // Individual tests can override this by pointing CreateSock to a mocked alternative.
+ CreateSock = [](int, int, int) -> std::unique_ptr<Sock> { std::terminate(); };
// Terminate immediately if a fuzzing harness ever tries to perform a DNS lookup.
g_dns_lookup = [](const std::string& name, bool allow_lookup) {
diff --git a/src/test/fuzz/fuzz.h b/src/test/fuzz/fuzz.h
index ca74d53de7..c468cd39e3 100644
--- a/src/test/fuzz/fuzz.h
+++ b/src/test/fuzz/fuzz.h
@@ -5,10 +5,9 @@
#ifndef BITCOIN_TEST_FUZZ_FUZZ_H
#define BITCOIN_TEST_FUZZ_FUZZ_H
-#include <span.h>
-
#include <cstdint>
#include <functional>
+#include <span>
#include <string_view>
/**
@@ -23,7 +22,7 @@
#define LIMITED_WHILE(condition, limit) \
for (unsigned _count{limit}; (condition) && _count; --_count)
-using FuzzBufferType = Span<const uint8_t>;
+using FuzzBufferType = std::span<const uint8_t>;
using TypeTestOneInput = std::function<void(FuzzBufferType)>;
struct FuzzTargetOptions {
diff --git a/src/test/fuzz/i2p.cpp b/src/test/fuzz/i2p.cpp
new file mode 100644
index 0000000000..51517187a0
--- /dev/null
+++ b/src/test/fuzz/i2p.cpp
@@ -0,0 +1,63 @@
+// Copyright (c) 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 <common/args.h>
+#include <i2p.h>
+#include <netaddress.h>
+#include <netbase.h>
+#include <test/fuzz/FuzzedDataProvider.h>
+#include <test/fuzz/fuzz.h>
+#include <test/fuzz/util.h>
+#include <test/fuzz/util/net.h>
+#include <test/util/setup_common.h>
+#include <util/fs_helpers.h>
+#include <util/threadinterrupt.h>
+
+void initialize_i2p()
+{
+ static const auto testing_setup = MakeNoLogFileContext<>();
+}
+
+FUZZ_TARGET(i2p, .init = initialize_i2p)
+{
+ FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
+
+ SetMockTime(ConsumeTime(fuzzed_data_provider));
+
+ // Mock CreateSock() to create FuzzedSock.
+ auto CreateSockOrig = CreateSock;
+ CreateSock = [&fuzzed_data_provider](int, int, int) {
+ return std::make_unique<FuzzedSock>(fuzzed_data_provider);
+ };
+
+ const fs::path private_key_path = gArgs.GetDataDirNet() / "fuzzed_i2p_private_key";
+ const CService addr{in6_addr(IN6ADDR_LOOPBACK_INIT), 7656};
+ const Proxy sam_proxy{addr, false};
+ CThreadInterrupt interrupt;
+
+ i2p::sam::Session session{private_key_path, sam_proxy, &interrupt};
+ i2p::Connection conn;
+
+ if (session.Listen(conn)) {
+ if (session.Accept(conn)) {
+ try {
+ (void)conn.sock->RecvUntilTerminator('\n', 10ms, interrupt, i2p::sam::MAX_MSG_SIZE);
+ } catch (const std::runtime_error&) {
+ }
+ }
+ }
+
+ bool proxy_error;
+
+ if (session.Connect(CService{}, conn, proxy_error)) {
+ try {
+ conn.sock->SendComplete("verack\n", 10ms, interrupt);
+ } catch (const std::runtime_error&) {
+ }
+ }
+
+ fs::remove_all(private_key_path);
+
+ CreateSock = CreateSockOrig;
+}
diff --git a/src/test/fuzz/integer.cpp b/src/test/fuzz/integer.cpp
index db246bb84e..8f1d7b6d45 100644
--- a/src/test/fuzz/integer.cpp
+++ b/src/test/fuzz/integer.cpp
@@ -40,6 +40,8 @@
#include <set>
#include <vector>
+using util::ToString;
+
void initialize_integer()
{
SelectParams(ChainType::REGTEST);
diff --git a/src/test/fuzz/kitchen_sink.cpp b/src/test/fuzz/kitchen_sink.cpp
index 82f3a306c5..4468f358d9 100644
--- a/src/test/fuzz/kitchen_sink.cpp
+++ b/src/test/fuzz/kitchen_sink.cpp
@@ -2,13 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <common/messages.h>
#include <merkleblock.h>
+#include <node/types.h>
#include <policy/fees.h>
#include <rpc/util.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
-#include <util/error.h>
#include <util/translation.h>
#include <array>
@@ -16,17 +17,15 @@
#include <optional>
#include <vector>
+using common::TransactionErrorString;
+using node::TransactionError;
+
namespace {
constexpr TransactionError ALL_TRANSACTION_ERROR[] = {
- TransactionError::OK,
TransactionError::MISSING_INPUTS,
TransactionError::ALREADY_IN_CHAIN,
- TransactionError::P2P_DISABLED,
TransactionError::MEMPOOL_REJECTED,
TransactionError::MEMPOOL_ERROR,
- TransactionError::INVALID_PSBT,
- TransactionError::PSBT_MISMATCH,
- TransactionError::SIGHASH_MISMATCH,
TransactionError::MAX_FEE_EXCEEDED,
};
}; // namespace
diff --git a/src/test/fuzz/locale.cpp b/src/test/fuzz/locale.cpp
index 0f2985b504..68db842247 100644
--- a/src/test/fuzz/locale.cpp
+++ b/src/test/fuzz/locale.cpp
@@ -51,7 +51,7 @@ FUZZ_TARGET(locale)
int64_t parseint64_out_without_locale;
const bool parseint64_without_locale = ParseInt64(random_string, &parseint64_out_without_locale);
const int64_t random_int64 = fuzzed_data_provider.ConsumeIntegral<int64_t>();
- const std::string tostring_without_locale = ToString(random_int64);
+ const std::string tostring_without_locale = util::ToString(random_int64);
// The variable `random_int32` is no longer used, but the harness still needs to
// consume the same data that it did previously to not invalidate existing seeds.
const int32_t random_int32 = fuzzed_data_provider.ConsumeIntegral<int32_t>();
@@ -75,7 +75,7 @@ FUZZ_TARGET(locale)
if (parseint64_without_locale) {
assert(parseint64_out_without_locale == parseint64_out_with_locale);
}
- const std::string tostring_with_locale = ToString(random_int64);
+ const std::string tostring_with_locale = util::ToString(random_int64);
assert(tostring_without_locale == tostring_with_locale);
const std::string strprintf_int_with_locale = strprintf("%d", random_int64);
assert(strprintf_int_without_locale == strprintf_int_with_locale);
diff --git a/src/test/fuzz/message.cpp b/src/test/fuzz/message.cpp
index 75baaa2754..6763206f72 100644
--- a/src/test/fuzz/message.cpp
+++ b/src/test/fuzz/message.cpp
@@ -3,12 +3,12 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
+#include <common/signmessage.h>
#include <key_io.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <util/chaintype.h>
-#include <util/message.h>
#include <util/strencodings.h>
#include <cassert>
diff --git a/src/test/fuzz/mini_miner.cpp b/src/test/fuzz/mini_miner.cpp
index 84f9bb4ad0..3a1663364f 100644
--- a/src/test/fuzz/mini_miner.cpp
+++ b/src/test/fuzz/mini_miner.cpp
@@ -7,11 +7,13 @@
#include <test/util/txmempool.h>
#include <test/util/mining.h>
-#include <node/mini_miner.h>
#include <node/miner.h>
+#include <node/mini_miner.h>
#include <primitives/transaction.h>
#include <random.h>
#include <txmempool.h>
+#include <util/check.h>
+#include <util/translation.h>
#include <deque>
#include <vector>
@@ -33,7 +35,9 @@ void initialize_miner()
FUZZ_TARGET(mini_miner, .init = initialize_miner)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
- CTxMemPool pool{CTxMemPool::Options{}};
+ bilingual_str error;
+ CTxMemPool pool{CTxMemPool::Options{}, error};
+ Assert(error.empty());
std::vector<COutPoint> outpoints;
std::deque<COutPoint> available_coins = g_available_coins;
LOCK2(::cs_main, pool.cs);
@@ -109,7 +113,9 @@ FUZZ_TARGET(mini_miner, .init = initialize_miner)
FUZZ_TARGET(mini_miner_selection, .init = initialize_miner)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
- CTxMemPool pool{CTxMemPool::Options{}};
+ bilingual_str error;
+ CTxMemPool pool{CTxMemPool::Options{}, error};
+ Assert(error.empty());
// Make a copy to preserve determinism.
std::deque<COutPoint> available_coins = g_available_coins;
std::vector<CTransactionRef> transactions;
diff --git a/src/test/fuzz/muhash.cpp b/src/test/fuzz/muhash.cpp
index 8304e6fdb8..dd34c465ed 100644
--- a/src/test/fuzz/muhash.cpp
+++ b/src/test/fuzz/muhash.cpp
@@ -43,7 +43,19 @@ FUZZ_TARGET(muhash)
},
[&] {
// Test that dividing a MuHash by itself brings it back to it's initial state
+
+ // See note about clang + self-assignment in test/uint256_tests.cpp
+ #if defined(__clang__)
+ # pragma clang diagnostic push
+ # pragma clang diagnostic ignored "-Wself-assign-overloaded"
+ #endif
+
muhash /= muhash;
+
+ #if defined(__clang__)
+ # pragma clang diagnostic pop
+ #endif
+
muhash.Finalize(out);
out2 = uint256S(initial_state_hash);
},
diff --git a/src/test/fuzz/package_eval.cpp b/src/test/fuzz/package_eval.cpp
index c201118bce..53aedf23ea 100644
--- a/src/test/fuzz/package_eval.cpp
+++ b/src/test/fuzz/package_eval.cpp
@@ -15,7 +15,9 @@
#include <test/util/script.h>
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
+#include <util/check.h>
#include <util/rbf.h>
+#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
@@ -107,7 +109,7 @@ void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chains
SetMockTime(time);
}
-CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
+std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
{
// Take the default options for tests...
CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
@@ -126,8 +128,13 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
mempool_opts.check_ratio = 1;
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
+ bilingual_str error;
// ...and construct a CTxMemPool from it
- return CTxMemPool{mempool_opts};
+ auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
+ // ... ignore the error since it might be beneficial to fuzz even when the
+ // mempool size is unreasonably small
+ Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
+ return mempool;
}
FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
@@ -149,8 +156,8 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
- CTxMemPool tx_pool_{MakeMempool(fuzzed_data_provider, node)};
- MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
+ auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
+ MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
chainstate.SetMempool(&tx_pool);
@@ -173,7 +180,7 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
// Create transaction to add to the mempool
const CTransactionRef tx = [&] {
CMutableTransaction tx_mut;
- tx_mut.nVersion = fuzzed_data_provider.ConsumeBool() ? 3 : CTransaction::CURRENT_VERSION;
+ tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
// Last tx will sweep all outpoints in package
const auto num_in = last_tx ? package_outpoints.size() : fuzzed_data_provider.ConsumeIntegralInRange<int>(1, mempool_outpoints.size());
@@ -307,7 +314,7 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
// just use result_package.m_state here. This makes the expect_valid check meaningless, but
// we can still verify that the contents of m_tx_results are consistent with m_state.
const bool expect_valid{result_package.m_state.IsValid()};
- Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, nullptr));
+ Assert(!CheckPackageMempoolAcceptResult(txs, result_package, expect_valid, &tx_pool));
} else {
// This is empty if it fails early checks, or "full" if transactions are looked at deeper
Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());
diff --git a/src/test/fuzz/partially_downloaded_block.cpp b/src/test/fuzz/partially_downloaded_block.cpp
index 0c874c76e8..77952cab9e 100644
--- a/src/test/fuzz/partially_downloaded_block.cpp
+++ b/src/test/fuzz/partially_downloaded_block.cpp
@@ -10,6 +10,8 @@
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
#include <txmempool.h>
+#include <util/check.h>
+#include <util/translation.h>
#include <cstddef>
#include <cstdint>
@@ -52,7 +54,9 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
CBlockHeaderAndShortTxIDs cmpctblock{*block, fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
- CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
+ bilingual_str error;
+ CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
+ Assert(error.empty());
PartiallyDownloadedBlock pdb{&pool};
// Set of available transactions (mempool or extra_txn)
diff --git a/src/test/fuzz/rbf.cpp b/src/test/fuzz/rbf.cpp
index 64785948f6..eb981352ec 100644
--- a/src/test/fuzz/rbf.cpp
+++ b/src/test/fuzz/rbf.cpp
@@ -13,6 +13,8 @@
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
#include <txmempool.h>
+#include <util/check.h>
+#include <util/translation.h>
#include <cstdint>
#include <optional>
@@ -56,7 +58,9 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
return;
}
- CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
+ bilingual_str error;
+ CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
+ Assert(error.empty());
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), NUM_ITERS)
{
@@ -87,10 +91,14 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
SetMockTime(ConsumeTime(fuzzed_data_provider));
- std::optional<CMutableTransaction> child = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
- if (!child) return;
+ // "Real" virtual size is not important for this test since ConsumeTxMemPoolEntry generates its own virtual size values
+ // so we construct small transactions for performance reasons. Child simply needs an input for later to perhaps connect to parent.
+ CMutableTransaction child;
+ child.vin.resize(1);
- CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
+ bilingual_str error;
+ CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
+ Assert(error.empty());
// Add a bunch of parent-child pairs to the mempool, and remember them.
std::vector<CTransaction> mempool_txs;
@@ -107,15 +115,13 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), NUM_ITERS)
{
// Make sure txns only have one input, and that a unique input is given to avoid circular references
- std::optional<CMutableTransaction> parent = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
- if (!parent) {
- return;
- }
+ CMutableTransaction parent;
assert(iter <= g_outpoints.size());
- parent->vin.resize(1);
- parent->vin[0].prevout = g_outpoints[iter++];
+ parent.vin.resize(1);
+ parent.vin[0].prevout = g_outpoints[iter++];
+ parent.vout.emplace_back(0, CScript());
- mempool_txs.emplace_back(*parent);
+ mempool_txs.emplace_back(parent);
const auto parent_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
running_vsize_total += parent_entry.GetTxSize();
if (running_vsize_total > std::numeric_limits<int32_t>::max()) {
@@ -124,10 +130,10 @@ FUZZ_TARGET(package_rbf, .init = initialize_package_rbf)
break;
}
pool.addUnchecked(parent_entry);
- if (fuzzed_data_provider.ConsumeBool() && !child->vin.empty()) {
- child->vin[0].prevout = COutPoint{mempool_txs.back().GetHash(), 0};
+ if (fuzzed_data_provider.ConsumeBool()) {
+ child.vin[0].prevout = COutPoint{mempool_txs.back().GetHash(), 0};
}
- mempool_txs.emplace_back(*child);
+ mempool_txs.emplace_back(child);
const auto child_entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, mempool_txs.back());
running_vsize_total += child_entry.GetTxSize();
if (running_vsize_total > std::numeric_limits<int32_t>::max()) {
diff --git a/src/test/fuzz/rpc.cpp b/src/test/fuzz/rpc.cpp
index 2325bf0941..4e52c1c091 100644
--- a/src/test/fuzz/rpc.cpp
+++ b/src/test/fuzz/rpc.cpp
@@ -36,6 +36,9 @@
#include <vector>
enum class ChainType;
+using util::Join;
+using util::ToString;
+
namespace {
struct RPCFuzzTestingSetup : public TestingSetup {
RPCFuzzTestingSetup(const ChainType chain_type, const std::vector<const char*>& extra_args) : TestingSetup{chain_type, extra_args}
diff --git a/src/test/fuzz/script_assets_test_minimizer.cpp b/src/test/fuzz/script_assets_test_minimizer.cpp
index 511b581f60..5a8b599df6 100644
--- a/src/test/fuzz/script_assets_test_minimizer.cpp
+++ b/src/test/fuzz/script_assets_test_minimizer.cpp
@@ -17,6 +17,8 @@
#include <string>
#include <vector>
+using util::SplitString;
+
// This fuzz "test" can be used to minimize test cases for script_assets_test in
// src/test/script_tests.cpp. While it written as a fuzz test, and can be used as such,
// fuzzing the inputs is unlikely to construct useful test cases.
diff --git a/src/test/fuzz/spanparsing.cpp b/src/test/fuzz/script_parsing.cpp
index b8996632bc..d29a6ea90c 100644
--- a/src/test/fuzz/spanparsing.cpp
+++ b/src/test/fuzz/script_parsing.cpp
@@ -2,11 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+#include <script/parsing.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
-#include <util/spanparsing.h>
+#include <util/string.h>
-FUZZ_TARGET(spanparsing)
+using util::Split;
+
+FUZZ_TARGET(script_parsing)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const size_t query_size = fuzzed_data_provider.ConsumeIntegral<size_t>();
@@ -15,16 +18,16 @@ FUZZ_TARGET(spanparsing)
const Span<const char> const_span{span_str};
Span<const char> mut_span = const_span;
- (void)spanparsing::Const(query, mut_span);
+ (void)script::Const(query, mut_span);
mut_span = const_span;
- (void)spanparsing::Func(query, mut_span);
+ (void)script::Func(query, mut_span);
mut_span = const_span;
- (void)spanparsing::Expr(mut_span);
+ (void)script::Expr(mut_span);
if (!query.empty()) {
mut_span = const_span;
- (void)spanparsing::Split(mut_span, query.front());
+ (void)Split(mut_span, query.front());
}
}
diff --git a/src/test/fuzz/string.cpp b/src/test/fuzz/string.cpp
index 631da13803..5b822b03f6 100644
--- a/src/test/fuzz/string.cpp
+++ b/src/test/fuzz/string.cpp
@@ -5,6 +5,7 @@
#include <blockfilter.h>
#include <clientversion.h>
#include <common/args.h>
+#include <common/messages.h>
#include <common/settings.h>
#include <common/system.h>
#include <common/url.h>
@@ -21,8 +22,6 @@
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
-#include <util/error.h>
-#include <util/fees.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/translation.h>
@@ -37,6 +36,16 @@
enum class FeeEstimateMode;
+using common::AmountErrMsg;
+using common::AmountHighWarn;
+using common::FeeModeFromString;
+using common::ResolveErrMsg;
+using util::ContainsNoNUL;
+using util::Join;
+using util::RemovePrefix;
+using util::SplitString;
+using util::TrimString;
+
FUZZ_TARGET(string)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
diff --git a/src/test/fuzz/timeoffsets.cpp b/src/test/fuzz/timeoffsets.cpp
index 019337a94a..dfa4dd705d 100644
--- a/src/test/fuzz/timeoffsets.cpp
+++ b/src/test/fuzz/timeoffsets.cpp
@@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <node/timeoffsets.h>
+#include <node/warnings.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/util/setup_common.h>
@@ -19,7 +20,8 @@ void initialize_timeoffsets()
FUZZ_TARGET(timeoffsets, .init = initialize_timeoffsets)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
- TimeOffsets offsets{};
+ node::Warnings warnings{};
+ TimeOffsets offsets{warnings};
LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 4'000) {
(void)offsets.Median();
offsets.Add(std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<std::chrono::seconds::rep>()});
diff --git a/src/test/fuzz/tx_pool.cpp b/src/test/fuzz/tx_pool.cpp
index 9f0aedf29b..b6b91445f9 100644
--- a/src/test/fuzz/tx_pool.cpp
+++ b/src/test/fuzz/tx_pool.cpp
@@ -15,12 +15,15 @@
#include <test/util/script.h>
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
+#include <util/check.h>
#include <util/rbf.h>
+#include <util/translation.h>
#include <validation.h>
#include <validationinterface.h>
using node::BlockAssembler;
using node::NodeContext;
+using util::ToString;
namespace {
@@ -116,7 +119,7 @@ void MockTime(FuzzedDataProvider& fuzzed_data_provider, const Chainstate& chains
SetMockTime(time);
}
-CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
+std::unique_ptr<CTxMemPool> MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeContext& node)
{
// Take the default options for tests...
CTxMemPool::Options mempool_opts{MemPoolOptionsForTest(node)};
@@ -126,7 +129,12 @@ CTxMemPool MakeMempool(FuzzedDataProvider& fuzzed_data_provider, const NodeConte
mempool_opts.require_standard = fuzzed_data_provider.ConsumeBool();
// ...and construct a CTxMemPool from it
- return CTxMemPool{mempool_opts};
+ bilingual_str error;
+ auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
+ // ... ignore the error since it might be beneficial to fuzz even when the
+ // mempool size is unreasonably small
+ Assert(error.empty() || error.original.starts_with("-maxmempool must be at least "));
+ return mempool;
}
void CheckATMPInvariants(const MempoolAcceptResult& res, bool txid_in_mempool, bool wtxid_in_mempool)
@@ -198,8 +206,8 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
constexpr CAmount SUPPLY_TOTAL{COINBASE_MATURITY * 50 * COIN};
SetMempoolConstraints(*node.args, fuzzed_data_provider);
- CTxMemPool tx_pool_{MakeMempool(fuzzed_data_provider, node)};
- MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
+ auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
+ MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
chainstate.SetMempool(&tx_pool);
@@ -226,7 +234,7 @@ FUZZ_TARGET(tx_pool_standard, .init = initialize_tx_pool)
// Create transaction to add to the mempool
const CTransactionRef tx = [&] {
CMutableTransaction tx_mut;
- tx_mut.nVersion = fuzzed_data_provider.ConsumeBool() ? 3 : CTransaction::CURRENT_VERSION;
+ tx_mut.version = fuzzed_data_provider.ConsumeBool() ? TRUC_VERSION : CTransaction::CURRENT_VERSION;
tx_mut.nLockTime = fuzzed_data_provider.ConsumeBool() ? 0 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size());
const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, outpoints_rbf.size() * 2);
@@ -376,8 +384,8 @@ FUZZ_TARGET(tx_pool, .init = initialize_tx_pool)
}
SetMempoolConstraints(*node.args, fuzzed_data_provider);
- CTxMemPool tx_pool_{MakeMempool(fuzzed_data_provider, node)};
- MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
+ auto tx_pool_{MakeMempool(fuzzed_data_provider, node)};
+ MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(tx_pool_.get());
chainstate.SetMempool(&tx_pool);
diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp
index 259b00fcae..92ded99917 100644
--- a/src/test/fuzz/util.cpp
+++ b/src/test/fuzz/util.cpp
@@ -43,9 +43,9 @@ CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider,
{
CMutableTransaction tx_mut;
const auto p2wsh_op_true = fuzzed_data_provider.ConsumeBool();
- tx_mut.nVersion = fuzzed_data_provider.ConsumeBool() ?
+ tx_mut.version = fuzzed_data_provider.ConsumeBool() ?
CTransaction::CURRENT_VERSION :
- fuzzed_data_provider.ConsumeIntegral<int32_t>();
+ fuzzed_data_provider.ConsumeIntegral<uint32_t>();
tx_mut.nLockTime = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, max_num_in);
const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, max_num_out);
diff --git a/src/test/fuzz/util/net.cpp b/src/test/fuzz/util/net.cpp
index 5e7aae670e..ad69c29d12 100644
--- a/src/test/fuzz/util/net.cpp
+++ b/src/test/fuzz/util/net.cpp
@@ -182,6 +182,12 @@ ssize_t FuzzedSock::Recv(void* buf, size_t len, int flags) const
EWOULDBLOCK,
};
assert(buf != nullptr || len == 0);
+
+ // Do the latency before any of the "return" statements.
+ if (m_fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) {
+ std::this_thread::sleep_for(std::chrono::milliseconds{2});
+ }
+
if (len == 0 || m_fuzzed_data_provider.ConsumeBool()) {
const ssize_t r = m_fuzzed_data_provider.ConsumeBool() ? 0 : -1;
if (r == -1) {
@@ -189,47 +195,41 @@ ssize_t FuzzedSock::Recv(void* buf, size_t len, int flags) const
}
return r;
}
- std::vector<uint8_t> random_bytes;
- bool pad_to_len_bytes{m_fuzzed_data_provider.ConsumeBool()};
- if (m_peek_data.has_value()) {
- // `MSG_PEEK` was used in the preceding `Recv()` call, return `m_peek_data`.
- random_bytes = m_peek_data.value();
+
+ size_t copied_so_far{0};
+
+ if (!m_peek_data.empty()) {
+ // `MSG_PEEK` was used in the preceding `Recv()` call, copy the first bytes from `m_peek_data`.
+ const size_t copy_len{std::min(len, m_peek_data.size())};
+ std::memcpy(buf, m_peek_data.data(), copy_len);
+ copied_so_far += copy_len;
if ((flags & MSG_PEEK) == 0) {
- m_peek_data.reset();
+ m_peek_data.erase(m_peek_data.begin(), m_peek_data.begin() + copy_len);
}
- pad_to_len_bytes = false;
- } else if ((flags & MSG_PEEK) != 0) {
- // New call with `MSG_PEEK`.
- random_bytes = ConsumeRandomLengthByteVector(m_fuzzed_data_provider, len);
- if (!random_bytes.empty()) {
- m_peek_data = random_bytes;
- pad_to_len_bytes = false;
- }
- } else {
- random_bytes = ConsumeRandomLengthByteVector(m_fuzzed_data_provider, len);
}
- if (random_bytes.empty()) {
- const ssize_t r = m_fuzzed_data_provider.ConsumeBool() ? 0 : -1;
- if (r == -1) {
- SetFuzzedErrNo(m_fuzzed_data_provider, recv_errnos);
- }
- return r;
+
+ if (copied_so_far == len) {
+ return copied_so_far;
}
- // `random_bytes` might exceed the size of `buf` if e.g. Recv is called with
- // len=N and MSG_PEEK first and afterwards called with len=M (M < N) and
- // without MSG_PEEK.
- size_t recv_len{std::min(random_bytes.size(), len)};
- std::memcpy(buf, random_bytes.data(), recv_len);
- if (pad_to_len_bytes) {
- if (len > random_bytes.size()) {
- std::memset((char*)buf + random_bytes.size(), 0, len - random_bytes.size());
- }
- return len;
+
+ auto new_data = ConsumeRandomLengthByteVector(m_fuzzed_data_provider, len - copied_so_far);
+ if (new_data.empty()) return copied_so_far;
+
+ std::memcpy(reinterpret_cast<uint8_t*>(buf) + copied_so_far, new_data.data(), new_data.size());
+ copied_so_far += new_data.size();
+
+ if ((flags & MSG_PEEK) != 0) {
+ m_peek_data.insert(m_peek_data.end(), new_data.begin(), new_data.end());
}
- if (m_fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) {
- std::this_thread::sleep_for(std::chrono::milliseconds{2});
+
+ if (copied_so_far == len || m_fuzzed_data_provider.ConsumeBool()) {
+ return copied_so_far;
}
- return recv_len;
+
+ // Pad to len bytes.
+ std::memset(reinterpret_cast<uint8_t*>(buf) + copied_so_far, 0x0, len - copied_so_far);
+
+ return len;
}
int FuzzedSock::Connect(const sockaddr*, socklen_t) const
diff --git a/src/test/fuzz/util/net.h b/src/test/fuzz/util/net.h
index ed02680676..1a5902329e 100644
--- a/src/test/fuzz/util/net.h
+++ b/src/test/fuzz/util/net.h
@@ -43,7 +43,7 @@ class FuzzedSock : public Sock
* If `MSG_PEEK` is used, then our `Recv()` returns some random data as usual, but on the next
* `Recv()` call we must return the same data, thus we remember it here.
*/
- mutable std::optional<std::vector<uint8_t>> m_peek_data;
+ mutable std::vector<uint8_t> m_peek_data;
/**
* Whether to pretend that the socket is select(2)-able. This is randomly set in the
diff --git a/src/test/fuzz/validation_load_mempool.cpp b/src/test/fuzz/validation_load_mempool.cpp
index 00678742c9..51140ae039 100644
--- a/src/test/fuzz/validation_load_mempool.cpp
+++ b/src/test/fuzz/validation_load_mempool.cpp
@@ -13,7 +13,9 @@
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
#include <txmempool.h>
+#include <util/check.h>
#include <util/time.h>
+#include <util/translation.h>
#include <validation.h>
#include <cstdint>
@@ -40,7 +42,9 @@ FUZZ_TARGET(validation_load_mempool, .init = initialize_validation_load_mempool)
SetMockTime(ConsumeTime(fuzzed_data_provider));
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
- CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node)};
+ bilingual_str error;
+ CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
+ Assert(error.empty());
auto& chainstate{static_cast<DummyChainState&>(g_setup->m_node.chainman->ActiveChainstate())};
chainstate.SetMempool(&pool);
diff --git a/src/test/fuzz/vecdeque.cpp b/src/test/fuzz/vecdeque.cpp
new file mode 100644
index 0000000000..1d9a98931f
--- /dev/null
+++ b/src/test/fuzz/vecdeque.cpp
@@ -0,0 +1,491 @@
+// Copyright (c) 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 <span.h>
+#include <test/fuzz/util.h>
+#include <test/util/xoroshiro128plusplus.h>
+#include <util/vecdeque.h>
+
+#include <deque>
+#include <stdint.h>
+
+namespace {
+
+/** The maximum number of simultaneous buffers kept by the test. */
+static constexpr size_t MAX_BUFFERS{3};
+/** How many elements are kept in a buffer at most. */
+static constexpr size_t MAX_BUFFER_SIZE{48};
+/** How many operations are performed at most on the buffers in one test. */
+static constexpr size_t MAX_OPERATIONS{1024};
+
+/** Perform a simulation fuzz test on VecDeque type T.
+ *
+ * T must be constructible from a uint64_t seed, comparable to other T, copyable, and movable.
+ */
+template<typename T, bool CheckNoneLeft>
+void TestType(Span<const uint8_t> buffer, uint64_t rng_tweak)
+{
+ FuzzedDataProvider provider(buffer.data(), buffer.size());
+ // Local RNG, only used for the seeds to initialize T objects with.
+ XoRoShiRo128PlusPlus rng(provider.ConsumeIntegral<uint64_t>() ^ rng_tweak);
+
+ // Real circular buffers.
+ std::vector<VecDeque<T>> real;
+ real.reserve(MAX_BUFFERS);
+ // Simulated circular buffers.
+ std::vector<std::deque<T>> sim;
+ sim.reserve(MAX_BUFFERS);
+ // Temporary object of type T.
+ std::optional<T> tmp;
+
+ // Compare a real and a simulated buffer.
+ auto compare_fn = [](const VecDeque<T>& r, const std::deque<T>& s) {
+ assert(r.size() == s.size());
+ assert(r.empty() == s.empty());
+ assert(r.capacity() >= r.size());
+ if (s.size() == 0) return;
+ assert(r.front() == s.front());
+ assert(r.back() == s.back());
+ for (size_t i = 0; i < s.size(); ++i) {
+ assert(r[i] == s[i]);
+ }
+ };
+
+ LIMITED_WHILE(provider.remaining_bytes(), MAX_OPERATIONS) {
+ int command = provider.ConsumeIntegral<uint8_t>() % 64;
+ unsigned idx = real.empty() ? 0 : provider.ConsumeIntegralInRange<unsigned>(0, real.size() - 1);
+ const size_t num_buffers = sim.size();
+ // Pick one operation based on value of command. Not all operations are always applicable.
+ // Loop through the applicable ones until command reaches 0 (which avoids the need to
+ // compute the number of applicable commands ahead of time).
+ const bool non_empty{num_buffers != 0};
+ const bool non_full{num_buffers < MAX_BUFFERS};
+ const bool partially_full{non_empty && non_full};
+ const bool multiple_exist{num_buffers > 1};
+ const bool existing_buffer_non_full{non_empty && sim[idx].size() < MAX_BUFFER_SIZE};
+ const bool existing_buffer_non_empty{non_empty && !sim[idx].empty()};
+ assert(non_full || non_empty);
+ while (true) {
+ if (non_full && command-- == 0) {
+ /* Default construct. */
+ real.emplace_back();
+ sim.emplace_back();
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* resize() */
+ compare_fn(real[idx], sim[idx]);
+ size_t new_size = provider.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
+ real[idx].resize(new_size);
+ sim[idx].resize(new_size);
+ assert(real[idx].size() == new_size);
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* clear() */
+ compare_fn(real[idx], sim[idx]);
+ real[idx].clear();
+ sim[idx].clear();
+ assert(real[idx].empty());
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* Copy construct default. */
+ compare_fn(real[idx], sim[idx]);
+ real[idx] = VecDeque<T>();
+ sim[idx].clear();
+ assert(real[idx].size() == 0);
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* Destruct. */
+ compare_fn(real.back(), sim.back());
+ real.pop_back();
+ sim.pop_back();
+ break;
+ }
+ if (partially_full && command-- == 0) {
+ /* Copy construct. */
+ real.emplace_back(real[idx]);
+ sim.emplace_back(sim[idx]);
+ break;
+ }
+ if (partially_full && command-- == 0) {
+ /* Move construct. */
+ VecDeque<T> copy(real[idx]);
+ real.emplace_back(std::move(copy));
+ sim.emplace_back(sim[idx]);
+ break;
+ }
+ if (multiple_exist && command-- == 0) {
+ /* swap() */
+ swap(real[idx], real[(idx + 1) % num_buffers]);
+ swap(sim[idx], sim[(idx + 1) % num_buffers]);
+ break;
+ }
+ if (multiple_exist && command-- == 0) {
+ /* Copy assign. */
+ compare_fn(real[idx], sim[idx]);
+ real[idx] = real[(idx + 1) % num_buffers];
+ sim[idx] = sim[(idx + 1) % num_buffers];
+ break;
+ }
+ if (multiple_exist && command-- == 0) {
+ /* Move assign. */
+ VecDeque<T> copy(real[(idx + 1) % num_buffers]);
+ compare_fn(real[idx], sim[idx]);
+ real[idx] = std::move(copy);
+ sim[idx] = sim[(idx + 1) % num_buffers];
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* Self swap() */
+ swap(real[idx], real[idx]);
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* Self-copy assign. */
+ real[idx] = real[idx];
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* Self-move assign. */
+ // Do not use std::move(real[idx]) here: -Wself-move correctly warns about that.
+ real[idx] = static_cast<VecDeque<T>&&>(real[idx]);
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* reserve() */
+ size_t res_size = provider.ConsumeIntegralInRange<size_t>(0, MAX_BUFFER_SIZE);
+ size_t old_cap = real[idx].capacity();
+ size_t old_size = real[idx].size();
+ real[idx].reserve(res_size);
+ assert(real[idx].size() == old_size);
+ assert(real[idx].capacity() == std::max(old_cap, res_size));
+ break;
+ }
+ if (non_empty && command-- == 0) {
+ /* shrink_to_fit() */
+ size_t old_size = real[idx].size();
+ real[idx].shrink_to_fit();
+ assert(real[idx].size() == old_size);
+ assert(real[idx].capacity() == old_size);
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* push_back() (copying) */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ real[idx].push_back(*tmp);
+ sim[idx].push_back(*tmp);
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* push_back() (moving) */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ sim[idx].push_back(*tmp);
+ real[idx].push_back(std::move(*tmp));
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* emplace_back() */
+ uint64_t seed{rng()};
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ sim[idx].emplace_back(seed);
+ real[idx].emplace_back(seed);
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* push_front() (copying) */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ real[idx].push_front(*tmp);
+ sim[idx].push_front(*tmp);
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* push_front() (moving) */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ sim[idx].push_front(*tmp);
+ real[idx].push_front(std::move(*tmp));
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_full && command-- == 0) {
+ /* emplace_front() */
+ uint64_t seed{rng()};
+ size_t old_size = real[idx].size();
+ size_t old_cap = real[idx].capacity();
+ sim[idx].emplace_front(seed);
+ real[idx].emplace_front(seed);
+ assert(real[idx].size() == old_size + 1);
+ if (old_cap > old_size) {
+ assert(real[idx].capacity() == old_cap);
+ } else {
+ assert(real[idx].capacity() > old_cap);
+ assert(real[idx].capacity() <= 2 * (old_cap + 1));
+ }
+ break;
+ }
+ if (existing_buffer_non_empty && command-- == 0) {
+ /* front() [modifying] */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ assert(sim[idx].front() == real[idx].front());
+ sim[idx].front() = *tmp;
+ real[idx].front() = std::move(*tmp);
+ assert(real[idx].size() == old_size);
+ break;
+ }
+ if (existing_buffer_non_empty && command-- == 0) {
+ /* back() [modifying] */
+ tmp = T(rng());
+ size_t old_size = real[idx].size();
+ assert(sim[idx].back() == real[idx].back());
+ sim[idx].back() = *tmp;
+ real[idx].back() = *tmp;
+ assert(real[idx].size() == old_size);
+ break;
+ }
+ if (existing_buffer_non_empty && command-- == 0) {
+ /* operator[] [modifying] */
+ tmp = T(rng());
+ size_t pos = provider.ConsumeIntegralInRange<size_t>(0, sim[idx].size() - 1);
+ size_t old_size = real[idx].size();
+ assert(sim[idx][pos] == real[idx][pos]);
+ sim[idx][pos] = *tmp;
+ real[idx][pos] = std::move(*tmp);
+ assert(real[idx].size() == old_size);
+ break;
+ }
+ if (existing_buffer_non_empty && command-- == 0) {
+ /* pop_front() */
+ assert(sim[idx].front() == real[idx].front());
+ size_t old_size = real[idx].size();
+ sim[idx].pop_front();
+ real[idx].pop_front();
+ assert(real[idx].size() == old_size - 1);
+ break;
+ }
+ if (existing_buffer_non_empty && command-- == 0) {
+ /* pop_back() */
+ assert(sim[idx].back() == real[idx].back());
+ size_t old_size = real[idx].size();
+ sim[idx].pop_back();
+ real[idx].pop_back();
+ assert(real[idx].size() == old_size - 1);
+ break;
+ }
+ }
+ }
+
+ /* Fully compare the final state. */
+ for (unsigned i = 0; i < sim.size(); ++i) {
+ // Make sure const getters work.
+ const VecDeque<T>& realbuf = real[i];
+ const std::deque<T>& simbuf = sim[i];
+ compare_fn(realbuf, simbuf);
+ for (unsigned j = 0; j < sim.size(); ++j) {
+ assert((realbuf == real[j]) == (simbuf == sim[j]));
+ assert(((realbuf <=> real[j]) >= 0) == (simbuf >= sim[j]));
+ assert(((realbuf <=> real[j]) <= 0) == (simbuf <= sim[j]));
+ }
+ // Clear out the buffers so we can check below that no objects exist anymore.
+ sim[i].clear();
+ real[i].clear();
+ }
+
+ if constexpr (CheckNoneLeft) {
+ tmp = std::nullopt;
+ T::CheckNoneExist();
+ }
+}
+
+/** Data structure with built-in tracking of all existing objects. */
+template<size_t Size>
+class TrackedObj
+{
+ static_assert(Size > 0);
+
+ /* Data type for map that actually stores the object data.
+ *
+ * The key is a pointer to the TrackedObj, the value is the uint64_t it was initialized with.
+ * Default-constructed and moved-from objects hold an std::nullopt.
+ */
+ using track_map_type = std::map<const TrackedObj<Size>*, std::optional<uint64_t>>;
+
+private:
+
+ /** Actual map. */
+ static inline track_map_type g_tracker;
+
+ /** Iterators into the tracker map for this object.
+ *
+ * This is an array of size Size, all holding the same value, to give the object configurable
+ * size. The value is g_tracker.end() if this object is not fully initialized. */
+ typename track_map_type::iterator m_track_entry[Size];
+
+ void Check() const
+ {
+ auto it = g_tracker.find(this);
+ for (size_t i = 0; i < Size; ++i) {
+ assert(m_track_entry[i] == it);
+ }
+ }
+
+ /** Create entry for this object in g_tracker and populate m_track_entry. */
+ void Register()
+ {
+ auto [it, inserted] = g_tracker.emplace(this, std::nullopt);
+ assert(inserted);
+ for (size_t i = 0; i < Size; ++i) {
+ m_track_entry[i] = it;
+ }
+ }
+
+ void Deregister()
+ {
+ Check();
+ assert(m_track_entry[0] != g_tracker.end());
+ g_tracker.erase(m_track_entry[0]);
+ for (size_t i = 0; i < Size; ++i) {
+ m_track_entry[i] = g_tracker.end();
+ }
+ }
+
+ /** Get value corresponding to this object in g_tracker. */
+ std::optional<uint64_t>& Deref()
+ {
+ Check();
+ assert(m_track_entry[0] != g_tracker.end());
+ return m_track_entry[0]->second;
+ }
+
+ /** Get value corresponding to this object in g_tracker. */
+ const std::optional<uint64_t>& Deref() const
+ {
+ Check();
+ assert(m_track_entry[0] != g_tracker.end());
+ return m_track_entry[0]->second;
+ }
+
+public:
+ ~TrackedObj() { Deregister(); }
+ TrackedObj() { Register(); }
+
+ TrackedObj(uint64_t value)
+ {
+ Register();
+ Deref() = value;
+ }
+
+ TrackedObj(const TrackedObj& other)
+ {
+ Register();
+ Deref() = other.Deref();
+ }
+
+ TrackedObj(TrackedObj&& other)
+ {
+ Register();
+ Deref() = other.Deref();
+ other.Deref() = std::nullopt;
+ }
+
+ TrackedObj& operator=(const TrackedObj& other)
+ {
+ if (this == &other) return *this;
+ Deref() = other.Deref();
+ return *this;
+ }
+
+ TrackedObj& operator=(TrackedObj&& other)
+ {
+ if (this == &other) return *this;
+ Deref() = other.Deref();
+ other.Deref() = std::nullopt;
+ return *this;
+ }
+
+ friend bool operator==(const TrackedObj& a, const TrackedObj& b)
+ {
+ return a.Deref() == b.Deref();
+ }
+
+ friend std::strong_ordering operator<=>(const TrackedObj& a, const TrackedObj& b)
+ {
+ // Libc++ 15 & 16 do not support std::optional<T>::operator<=> yet. See
+ // https://reviews.llvm.org/D146392.
+ if (!a.Deref().has_value() || !b.Deref().has_value()) {
+ return a.Deref().has_value() <=> b.Deref().has_value();
+ }
+ return *a.Deref() <=> *b.Deref();
+ }
+
+ static void CheckNoneExist()
+ {
+ assert(g_tracker.empty());
+ }
+};
+
+} // namespace
+
+FUZZ_TARGET(vecdeque)
+{
+ // Run the test with simple uints (which satisfy all the trivial properties).
+ static_assert(std::is_trivially_copyable_v<uint32_t>);
+ static_assert(std::is_trivially_destructible_v<uint64_t>);
+ TestType<uint8_t, false>(buffer, 1);
+ TestType<uint16_t, false>(buffer, 2);
+ TestType<uint32_t, false>(buffer, 3);
+ TestType<uint64_t, false>(buffer, 4);
+
+ // Run the test with TrackedObjs (which do not).
+ static_assert(!std::is_trivially_copyable_v<TrackedObj<3>>);
+ static_assert(!std::is_trivially_destructible_v<TrackedObj<17>>);
+ TestType<TrackedObj<1>, true>(buffer, 5);
+ TestType<TrackedObj<3>, true>(buffer, 6);
+ TestType<TrackedObj<17>, true>(buffer, 7);
+}