LCOV - code coverage report
Current view: top level - src/test/fuzz - poolresource.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 71 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 82 0
Branches: 0.0 % 50 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       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                 :             : 
      13                 :             : #include <cstdint>
      14                 :             : #include <tuple>
      15                 :             : #include <vector>
      16                 :             : 
      17                 :             : namespace {
      18                 :             : 
      19                 :             : template <std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>
      20                 :           0 : class PoolResourceFuzzer
      21                 :             : {
      22                 :             :     FuzzedDataProvider& m_provider;
      23                 :             :     PoolResource<MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES> m_test_resource;
      24                 :             :     uint64_t m_sequence{0};
      25                 :             :     size_t m_total_allocated{};
      26                 :             : 
      27                 :             :     struct Entry {
      28                 :             :         Span<std::byte> span;
      29                 :             :         size_t alignment;
      30                 :             :         uint64_t seed;
      31                 :             : 
      32                 :           0 :         Entry(Span<std::byte> s, size_t a, uint64_t se) : span(s), alignment(a), seed(se) {}
      33                 :             :     };
      34                 :             : 
      35                 :             :     std::vector<Entry> m_entries;
      36                 :             : 
      37                 :             : public:
      38                 :           0 :     PoolResourceFuzzer(FuzzedDataProvider& provider)
      39                 :           0 :         : m_provider{provider},
      40                 :           0 :           m_test_resource{provider.ConsumeIntegralInRange<size_t>(MAX_BLOCK_SIZE_BYTES, 262144)}
      41                 :             :     {
      42                 :           0 :     }
      43                 :             : 
      44                 :           0 :     void Allocate(size_t size, size_t alignment)
      45                 :             :     {
      46         [ #  # ]:           0 :         assert(size > 0);                           // Must allocate at least 1 byte.
      47         [ #  # ]:           0 :         assert(alignment > 0);                      // Alignment must be at least 1.
      48         [ #  # ]:           0 :         assert((alignment & (alignment - 1)) == 0); // Alignment must be power of 2.
      49         [ #  # ]:           0 :         assert((size & (alignment - 1)) == 0);      // Size must be a multiple of alignment.
      50                 :             : 
      51         [ #  # ]:           0 :         auto span = Span(static_cast<std::byte*>(m_test_resource.Allocate(size, alignment)), size);
      52                 :           0 :         m_total_allocated += size;
      53                 :             : 
      54                 :           0 :         auto ptr_val = reinterpret_cast<std::uintptr_t>(span.data());
      55         [ #  # ]:           0 :         assert((ptr_val & (alignment - 1)) == 0);
      56                 :             : 
      57                 :           0 :         uint64_t seed = m_sequence++;
      58                 :           0 :         RandomContentFill(m_entries.emplace_back(span, alignment, seed));
      59                 :           0 :     }
      60                 :             : 
      61                 :             :     void
      62                 :           0 :     Allocate()
      63                 :             :     {
      64         [ #  # ]:           0 :         if (m_total_allocated > 0x1000000) return;
      65                 :           0 :         size_t alignment_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 7);
      66                 :           0 :         size_t alignment = size_t{1} << alignment_bits;
      67                 :           0 :         size_t size_bits = m_provider.ConsumeIntegralInRange<size_t>(0, 16 - alignment_bits);
      68                 :           0 :         size_t size = m_provider.ConsumeIntegralInRange<size_t>(size_t{1} << size_bits, (size_t{1} << (size_bits + 1)) - 1U) << alignment_bits;
      69                 :           0 :         Allocate(size, alignment);
      70                 :             :     }
      71                 :             : 
      72                 :           0 :     void RandomContentFill(Entry& entry)
      73                 :             :     {
      74                 :           0 :         InsecureRandomContext(entry.seed).fillrand(entry.span);
      75                 :           0 :     }
      76                 :             : 
      77                 :           0 :     void RandomContentCheck(const Entry& entry)
      78                 :             :     {
      79                 :           0 :         std::vector<std::byte> expect(entry.span.size());
      80         [ #  # ]:           0 :         InsecureRandomContext(entry.seed).fillrand(expect);
      81         [ #  # ]:           0 :         assert(entry.span == expect);
      82                 :           0 :     }
      83                 :             : 
      84                 :           0 :     void Deallocate(const Entry& entry)
      85                 :             :     {
      86         [ #  # ]:           0 :         auto ptr_val = reinterpret_cast<std::uintptr_t>(entry.span.data());
      87         [ #  # ]:           0 :         assert((ptr_val & (entry.alignment - 1)) == 0);
      88                 :           0 :         RandomContentCheck(entry);
      89                 :           0 :         m_total_allocated -= entry.span.size();
      90                 :           0 :         m_test_resource.Deallocate(entry.span.data(), entry.span.size(), entry.alignment);
      91                 :           0 :     }
      92                 :             : 
      93                 :           0 :     void Deallocate()
      94                 :             :     {
      95         [ #  # ]:           0 :         if (m_entries.empty()) {
      96                 :             :             return;
      97                 :             :         }
      98                 :             : 
      99                 :           0 :         size_t idx = m_provider.ConsumeIntegralInRange<size_t>(0, m_entries.size() - 1);
     100                 :           0 :         Deallocate(m_entries[idx]);
     101         [ #  # ]:           0 :         if (idx != m_entries.size() - 1) {
     102                 :           0 :             m_entries[idx] = std::move(m_entries.back());
     103                 :             :         }
     104                 :           0 :         m_entries.pop_back();
     105                 :             :     }
     106                 :             : 
     107                 :           0 :     void Clear()
     108                 :             :     {
     109         [ #  # ]:           0 :         while (!m_entries.empty()) {
     110                 :           0 :             Deallocate();
     111                 :             :         }
     112                 :             : 
     113                 :           0 :         PoolResourceTester::CheckAllDataAccountedFor(m_test_resource);
     114                 :           0 :     }
     115                 :             : 
     116                 :           0 :     void Fuzz()
     117                 :             :     {
     118   [ #  #  #  # ]:           0 :         LIMITED_WHILE(m_provider.ConsumeBool(), 10000)
     119                 :             :         {
     120                 :           0 :             CallOneOf(
     121                 :             :                 m_provider,
     122                 :           0 :                 [&] { Allocate(); },
     123                 :           0 :                 [&] { Deallocate(); });
     124                 :             :         }
     125                 :           0 :         Clear();
     126                 :           0 :     }
     127                 :             : };
     128                 :             : 
     129                 :             : 
     130                 :             : } // namespace
     131                 :             : 
     132         [ #  # ]:           0 : FUZZ_TARGET(pool_resource)
     133                 :             : {
     134                 :           0 :     FuzzedDataProvider provider(buffer.data(), buffer.size());
     135                 :           0 :     CallOneOf(
     136                 :             :         provider,
     137         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<128, 1>{provider}.Fuzz(); },
     138         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<128, 2>{provider}.Fuzz(); },
     139         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<128, 4>{provider}.Fuzz(); },
     140         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<128, 8>{provider}.Fuzz(); },
     141                 :             : 
     142         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<8, 8>{provider}.Fuzz(); },
     143         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<16, 16>{provider}.Fuzz(); },
     144                 :             : 
     145         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<256, alignof(max_align_t)>{provider}.Fuzz(); },
     146         [ #  # ]:           0 :         [&] { PoolResourceFuzzer<256, 64>{provider}.Fuzz(); });
     147                 :           0 : }
        

Generated by: LCOV version 2.0-1