LCOV - code coverage report
Current view: top level - src/test/fuzz - golomb_rice.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 61 61
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 4 4
Branches: 60.5 % 86 52

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-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 <blockfilter.h>
       6                 :             : #include <serialize.h>
       7                 :             : #include <streams.h>
       8                 :             : #include <test/fuzz/FuzzedDataProvider.h>
       9                 :             : #include <test/fuzz/fuzz.h>
      10                 :             : #include <test/fuzz/util.h>
      11                 :             : #include <util/bytevectorhash.h>
      12                 :             : #include <util/golombrice.h>
      13                 :             : 
      14                 :             : #include <algorithm>
      15                 :             : #include <cassert>
      16                 :             : #include <cstdint>
      17                 :             : #include <iosfwd>
      18                 :             : #include <unordered_set>
      19                 :             : #include <vector>
      20                 :             : 
      21                 :             : namespace {
      22                 :             : 
      23                 :       39232 : uint64_t HashToRange(const std::vector<uint8_t>& element, const uint64_t f)
      24                 :             : {
      25                 :       78464 :     const uint64_t hash = CSipHasher(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL)
      26                 :       39232 :                               .Write(element)
      27                 :       39232 :                               .Finalize();
      28                 :       78464 :     return FastRange64(hash, f);
      29                 :       39232 : }
      30                 :             : 
      31                 :         297 : std::vector<uint64_t> BuildHashedSet(const std::unordered_set<std::vector<uint8_t>, ByteVectorHash>& elements, const uint64_t f)
      32                 :             : {
      33                 :         297 :     std::vector<uint64_t> hashed_elements;
      34         [ +  - ]:         297 :     hashed_elements.reserve(elements.size());
      35         [ +  + ]:       39529 :     for (const std::vector<uint8_t>& element : elements) {
      36   [ +  -  +  - ]:       39232 :         hashed_elements.push_back(HashToRange(element, f));
      37                 :       39232 :     }
      38         [ +  - ]:         297 :     std::sort(hashed_elements.begin(), hashed_elements.end());
      39                 :         297 :     return hashed_elements;
      40         [ +  - ]:         297 : }
      41                 :             : } // namespace
      42                 :             : 
      43   [ +  -  +  - ]:         339 : FUZZ_TARGET(golomb_rice)
      44                 :             : {
      45                 :         336 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      46                 :         336 :     std::vector<uint8_t> golomb_rice_data;
      47                 :         336 :     std::vector<uint64_t> encoded_deltas;
      48                 :             :     {
      49         [ +  - ]:         336 :         std::unordered_set<std::vector<uint8_t>, ByteVectorHash> elements;
      50         [ +  - ]:         336 :         const int n = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 512);
      51         [ +  + ]:       83542 :         for (int i = 0; i < n; ++i) {
      52         [ +  - ]:       83206 :             elements.insert(ConsumeRandomLengthByteVector(fuzzed_data_provider, 16));
      53                 :       83206 :         }
      54         [ +  - ]:         336 :         VectorWriter stream{golomb_rice_data, 0};
      55         [ +  - ]:         336 :         WriteCompactSize(stream, static_cast<uint32_t>(elements.size()));
      56         [ +  - ]:         336 :         BitStreamWriter bitwriter{stream};
      57         [ +  + ]:         336 :         if (!elements.empty()) {
      58                 :         297 :             uint64_t last_value = 0;
      59   [ +  -  +  + ]:       39529 :             for (const uint64_t value : BuildHashedSet(elements, static_cast<uint64_t>(elements.size()) * static_cast<uint64_t>(BASIC_FILTER_M))) {
      60                 :       39232 :                 const uint64_t delta = value - last_value;
      61         [ +  - ]:       39232 :                 encoded_deltas.push_back(delta);
      62         [ +  - ]:       39232 :                 GolombRiceEncode(bitwriter, BASIC_FILTER_P, delta);
      63                 :       39232 :                 last_value = value;
      64                 :       39232 :             }
      65                 :         297 :         }
      66         [ +  - ]:         336 :         bitwriter.Flush();
      67                 :         336 :     }
      68                 :             : 
      69                 :         336 :     std::vector<uint64_t> decoded_deltas;
      70                 :             :     {
      71   [ +  -  +  - ]:         336 :         SpanReader stream{golomb_rice_data};
      72         [ +  - ]:         336 :         BitStreamReader bitreader{stream};
      73         [ +  - ]:         336 :         const uint32_t n = static_cast<uint32_t>(ReadCompactSize(stream));
      74         [ +  + ]:       39568 :         for (uint32_t i = 0; i < n; ++i) {
      75   [ +  -  +  - ]:       39232 :             decoded_deltas.push_back(GolombRiceDecode(bitreader, BASIC_FILTER_P));
      76                 :       39232 :         }
      77                 :         336 :     }
      78                 :             : 
      79   [ +  -  +  - ]:         336 :     assert(encoded_deltas == decoded_deltas);
      80                 :             : 
      81                 :             :     {
      82                 :         336 :         const std::vector<uint8_t> random_bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider, 1024);
      83   [ +  -  +  - ]:         336 :         SpanReader stream{random_bytes};
      84                 :         336 :         uint32_t n;
      85                 :             :         try {
      86         [ +  + ]:         336 :             n = static_cast<uint32_t>(ReadCompactSize(stream));
      87         [ -  + ]:         336 :         } catch (const std::ios_base::failure&) {
      88                 :             :             return;
      89         [ +  - ]:         208 :         }
      90         [ +  - ]:         128 :         BitStreamReader bitreader{stream};
      91   [ +  -  +  + ]:       18476 :         for (uint32_t i = 0; i < std::min<uint32_t>(n, 1024); ++i) {
      92                 :             :             try {
      93         [ +  + ]:       18348 :                 (void)GolombRiceDecode(bitreader, BASIC_FILTER_P);
      94         [ -  + ]:       18348 :             } catch (const std::ios_base::failure&) {
      95         [ -  + ]:       15465 :             }
      96                 :       18348 :         }
      97         [ +  + ]:         336 :     }
      98         [ -  + ]:       16009 : }
        

Generated by: LCOV version 2.0-1