Coverage Report

Created: 2024-10-21 15:10

/root/bitcoin/src/test/fuzz/asmap.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2020-2021 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 <netaddress.h>
6
#include <netgroup.h>
7
#include <test/fuzz/fuzz.h>
8
#include <util/asmap.h>
9
10
#include <cstdint>
11
#include <vector>
12
13
//! asmap code that consumes nothing
14
static const std::vector<bool> IPV6_PREFIX_ASMAP = {};
15
16
//! asmap code that consumes the 96 prefix bits of ::ffff:0/96 (IPv4-in-IPv6 map)
17
static const std::vector<bool> IPV4_PREFIX_ASMAP = {
18
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
19
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
20
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
21
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
22
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
23
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
24
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
25
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
26
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
27
    true, true, false, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, // Match 0x00
28
    true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,         // Match 0xFF
29
    true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true          // Match 0xFF
30
};
31
32
FUZZ_TARGET(asmap)
33
0
{
34
    // Encoding: [7 bits: asmap size] [1 bit: ipv6?] [3-130 bytes: asmap] [4 or 16 bytes: addr]
35
0
    if (buffer.size() < 1 + 3 + 4) return;
36
0
    int asmap_size = 3 + (buffer[0] & 127);
37
0
    bool ipv6 = buffer[0] & 128;
38
0
    const size_t addr_size = ipv6 ? ADDR_IPV6_SIZE : ADDR_IPV4_SIZE;
39
0
    if (buffer.size() < size_t(1 + asmap_size + addr_size)) return;
40
0
    std::vector<bool> asmap = ipv6 ? IPV6_PREFIX_ASMAP : IPV4_PREFIX_ASMAP;
41
0
    asmap.reserve(asmap.size() + 8 * asmap_size);
42
0
    for (int i = 0; i < asmap_size; ++i) {
43
0
        for (int j = 0; j < 8; ++j) {
44
0
            asmap.push_back((buffer[1 + i] >> j) & 1);
45
0
        }
46
0
    }
47
0
    if (!SanityCheckASMap(asmap, 128)) return;
48
49
0
    const uint8_t* addr_data = buffer.data() + 1 + asmap_size;
50
0
    CNetAddr net_addr;
51
0
    if (ipv6) {
52
0
        assert(addr_size == ADDR_IPV6_SIZE);
53
0
        net_addr.SetLegacyIPv6({addr_data, addr_size});
54
0
    } else {
55
0
        assert(addr_size == ADDR_IPV4_SIZE);
56
0
        in_addr ipv4;
57
0
        memcpy(&ipv4, addr_data, addr_size);
58
0
        net_addr.SetIP(CNetAddr{ipv4});
59
0
    }
60
0
    NetGroupManager netgroupman{asmap};
61
0
    (void)netgroupman.GetMappedAS(net_addr);
62
0
}