Coverage Report

Created: 2025-08-25 06:15

/src/immer/extra/fuzzer/fuzzer_input.hpp
Line
Count
Source (jump to first uncovered line)
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
5.13M
    {
31
5.13M
        if (size_ < size)
32
0
            throw no_more_input{};
33
5.13M
        auto r = data_;
34
5.13M
        data_ += size;
35
5.13M
        size_ -= size;
36
5.13M
        return r;
37
5.13M
    }
38
39
    const std::uint8_t* next(std::size_t size, std::size_t align)
40
5.13M
    {
41
5.13M
        auto& p = const_cast<void*&>(reinterpret_cast<const void*&>(data_));
42
5.13M
        auto r  = std::align(align, size, p, size_);
43
5.13M
        if (r == nullptr)
44
2.44k
            throw no_more_input{};
45
5.13M
        return next(size);
46
5.13M
    }
47
48
    template <typename Fn>
49
    int run(Fn step)
50
2.44k
    {
51
2.44k
        if (size_ > fuzzer_input_max_size)
52
5
            return 0;
53
2.44k
        try {
54
1.63M
            while (step(*this))
55
1.62M
                continue;
56
2.44k
        } catch (const no_more_input&) {
57
2.44k
        };
58
2.44k
        return 0;
59
2.44k
    }
60
};
61
62
template <typename T>
63
const T& read(fuzzer_input& fz)
64
5.13M
{
65
5.13M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
5.13M
}
char const& read<char>(fuzzer_input&)
Line
Count
Source
64
4.99M
{
65
4.99M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
4.99M
}
unsigned char const& read<unsigned char>(fuzzer_input&)
Line
Count
Source
64
140k
{
65
140k
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
140k
}
67
68
template <typename T, typename Cond>
69
T read(fuzzer_input& fz, Cond cond)
70
3.33M
{
71
3.33M
    auto x = read<T>(fz);
72
3.50M
    while (!cond(x))
73
169k
        x = read<T>(fz);
74
3.33M
    return x;
75
3.33M
}
vector-st.cpp:char read<char, LLVMFuzzerTestOneInput::$_0>(fuzzer_input&, LLVMFuzzerTestOneInput::$_0)
Line
Count
Source
70
3.25M
{
71
3.25M
    auto x = read<T>(fz);
72
3.36M
    while (!cond(x))
73
108k
        x = read<T>(fz);
74
3.25M
    return x;
75
3.25M
}
vector-st.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_1::operator()<immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u> >(immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_1::operator()<immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u> >(immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
28.3k
{
71
28.3k
    auto x = read<T>(fz);
72
71.3k
    while (!cond(x))
73
43.0k
        x = read<T>(fz);
74
28.3k
    return x;
75
28.3k
}
vector-st.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_2::operator()<immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u> >(immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_2::operator()<immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u> >(immer::vector<int, immer::memory_policy<immer::heap_policy<immer::cpp_heap>, immer::unsafe_refcount_policy, immer::no_lock_policy, immer::no_transience_policy, false, true>, 2u, 2u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
51.6k
{
71
51.6k
    auto x = read<T>(fz);
72
68.9k
    while (!cond(x))
73
17.2k
        x = read<T>(fz);
74
51.6k
    return x;
75
51.6k
}