Coverage Report

Created: 2025-10-10 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/immer/extra/fuzzer/fuzzer_input.hpp
Line
Count
Source
1
//
2
// immer: immutable data structures for C++
3
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
4
//
5
// This software is distributed under the Boost Software License, Version 1.0.
6
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
7
//
8
9
#pragma once
10
11
#include <cstdint>
12
#include <memory>
13
#include <stdexcept>
14
15
#if defined(__GNUC__) && (__GNUC__ == 9 || __GNUC__ == 8 || __GNUC__ == 10)
16
#define IMMER_DISABLE_FUZZER_DUE_TO_GCC_BUG 1
17
#endif
18
19
struct no_more_input : std::exception
20
{};
21
22
constexpr auto fuzzer_input_max_size = 1 << 16;
23
24
struct fuzzer_input
25
{
26
    const std::uint8_t* data_;
27
    std::size_t size_;
28
29
    const std::uint8_t* next(std::size_t size)
30
10.7M
    {
31
10.7M
        if (size_ < size)
32
0
            throw no_more_input{};
33
10.7M
        auto r = data_;
34
10.7M
        data_ += size;
35
10.7M
        size_ -= size;
36
10.7M
        return r;
37
10.7M
    }
38
39
    const std::uint8_t* next(std::size_t size, std::size_t align)
40
10.7M
    {
41
10.7M
        auto& p = const_cast<void*&>(reinterpret_cast<const void*&>(data_));
42
10.7M
        auto r  = std::align(align, size, p, size_);
43
10.7M
        if (r == nullptr)
44
11.1k
            throw no_more_input{};
45
10.7M
        return next(size);
46
10.7M
    }
47
48
    template <typename Fn>
49
    int run(Fn step)
50
11.1k
    {
51
11.1k
        if (size_ > fuzzer_input_max_size)
52
6
            return 0;
53
11.1k
        try {
54
2.69M
            while (step(*this))
55
2.68M
                continue;
56
11.1k
        } catch (const no_more_input&) {
57
11.1k
        };
58
11.1k
        return 0;
59
11.1k
    }
60
};
61
62
template <typename T>
63
const T& read(fuzzer_input& fz)
64
10.7M
{
65
10.7M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
10.7M
}
char const& read<char>(fuzzer_input&)
Line
Count
Source
64
10.3M
{
65
10.3M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
10.3M
}
unsigned char const& read<unsigned char>(fuzzer_input&)
Line
Count
Source
64
360k
{
65
360k
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
360k
}
67
68
template <typename T, typename Cond>
69
T read(fuzzer_input& fz, Cond cond)
70
7.58M
{
71
7.58M
    auto x = read<T>(fz);
72
8.03M
    while (!cond(x))
73
452k
        x = read<T>(fz);
74
7.58M
    return x;
75
7.58M
}
flex-vector-bo.cpp:char read<char, LLVMFuzzerTestOneInput::$_0>(fuzzer_input&, LLVMFuzzerTestOneInput::$_0)
Line
Count
Source
70
6.04M
{
71
6.04M
    auto x = read<T>(fz);
72
6.40M
    while (!cond(x))
73
367k
        x = read<T>(fz);
74
6.04M
    return x;
75
6.04M
}
flex-vector-bo.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_2::operator()<immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u> >(immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_2::operator()<immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u> >(immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
201k
{
71
201k
    auto x = read<T>(fz);
72
210k
    while (!cond(x))
73
8.43k
        x = read<T>(fz);
74
201k
    return x;
75
201k
}
flex-vector-bo.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_3::operator()<immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u> >(immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_3::operator()<immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u> >(immer::flex_vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, true, true>, 3u, 3u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
136k
{
71
136k
    auto x = read<T>(fz);
72
149k
    while (!cond(x))
73
13.3k
        x = read<T>(fz);
74
136k
    return x;
75
136k
}
flex-vector-bo.cpp:char read<char, LLVMFuzzerTestOneInput::$_1::operator()<char>(char) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_1::operator()<char>(char) const::{lambda(auto:1)#1})
Line
Count
Source
70
1.20M
{
71
1.20M
    auto x = read<T>(fz);
72
1.26M
    while (!cond(x))
73
63.2k
        x = read<T>(fz);
74
1.20M
    return x;
75
1.20M
}