Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2022-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <random.h>6#include <span.h>7#include <support/allocators/pool.h>8#include <test/fuzz/FuzzedDataProvider.h>9#include <test/fuzz/fuzz.h>10#include <test/fuzz/util.h>11#include <test/util/poolresourcetester.h>12#include <util/byte_units.h>14#include <cstdint>15#include <tuple>16#include <vector>18namespace {20template <std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>21class PoolResourceFuzzer22{23FuzzedDataProvider& m_provider;24PoolResource<MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES> m_test_resource;25uint64_t m_sequence{0};26size_t m_total_allocated{};28struct Entry {29std::span<std::byte> span;30size_t alignment;31uint64_t seed;33Entry(std::span<std::byte> s, size_t a, uint64_t se) : span(s), alignment(a), seed(se) {}34};36std::vector<Entry> m_entries;38public:39PoolResourceFuzzer(FuzzedDataProvider& provider)40: m_provider{provider},41m_test_resource{provider.ConsumeIntegralInRange<size_t>(MAX_BLOCK_SIZE_BYTES, 262144)}42{43}45void Allocate(size_t size, size_t alignment)46{47assert(size > 0); // Must allocate at least 1 byte.48assert(alignment > 0); // Alignment must be at least 1.49assert((alignment & (alignment - 1)) == 0); // Alignment must be power of 2.50assert((size & (alignment - 1)) == 0); // Size must be a multiple of alignment.52auto span = std::span(static_cast<std::byte*>(m_test_resource.Allocate(size, alignment)), size);53m_total_allocated += size;55auto ptr_val = reinterpret_cast<std::uintptr_t>(span.data());56assert((ptr_val & (alignment - 1)) == 0);58uint64_t seed = m_sequence++;59RandomContentFill(m_entries.emplace_back(span, alignment, seed));60}62void63Allocate()64{65if (m_total_allocated > 16_MiB) return;66size_t alignment_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 7);67size_t alignment = size_t{1} << alignment_bits;68size_t size_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 16 - alignment_bits);69size_t size = m_provider.ConsumeIntegralInRange<size_t>(size_t{1} << size_bits, (size_t{1} << (size_bits + 1)) - 1U) << alignment_bits;70Allocate(size, alignment);71}73void RandomContentFill(Entry& entry)74{75InsecureRandomContext(entry.seed).fillrand(entry.span);76}78void RandomContentCheck(const Entry& entry)79{80std::vector<std::byte> expect(entry.span.size());81InsecureRandomContext(entry.seed).fillrand(expect);82assert(std::ranges::equal(entry.span, expect));83}85void Deallocate(const Entry& entry)86{87auto ptr_val = reinterpret_cast<std::uintptr_t>(entry.span.data());88assert((ptr_val & (entry.alignment - 1)) == 0);89RandomContentCheck(entry);90m_total_allocated -= entry.span.size();91m_test_resource.Deallocate(entry.span.data(), entry.span.size(), entry.alignment);92}94void Deallocate()95{96if (m_entries.empty()) {97return;98}100size_t idx = m_provider.ConsumeIntegralInRange<size_t>(0, m_entries.size() - 1);101Deallocate(m_entries[idx]);102if (idx != m_entries.size() - 1) {103m_entries[idx] = std::move(m_entries.back());104}105m_entries.pop_back();106}108void Clear()109{110while (!m_entries.empty()) {111Deallocate();112}114PoolResourceTester::CheckAllDataAccountedFor(m_test_resource);115}117void Fuzz()118{119LIMITED_WHILE (m_provider.ConsumeBool(), 10000) {120CallOneOf(121m_provider,122[&] { Allocate(); },123[&] { Deallocate(); });124}125Clear();126}127};130} // namespace132FUZZ_TARGET(pool_resource)133{134FuzzedDataProvider provider(buffer.data(), buffer.size());135CallOneOf(136provider,137[&] { PoolResourceFuzzer<128, 1>{provider}.Fuzz(); },138[&] { PoolResourceFuzzer<128, 2>{provider}.Fuzz(); },139[&] { PoolResourceFuzzer<128, 4>{provider}.Fuzz(); },140[&] { PoolResourceFuzzer<128, 8>{provider}.Fuzz(); },142[&] { PoolResourceFuzzer<8, 8>{provider}.Fuzz(); },143[&] { PoolResourceFuzzer<16, 16>{provider}.Fuzz(); },145[&] { PoolResourceFuzzer<256, alignof(max_align_t)>{provider}.Fuzz(); },146[&] { PoolResourceFuzzer<256, 64>{provider}.Fuzz(); });147}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.