Coverage Report

Created: 2025-08-03 07:06

/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
1.43M
    {
31
1.43M
        if (size_ < size)
32
0
            throw no_more_input{};
33
1.43M
        auto r = data_;
34
1.43M
        data_ += size;
35
1.43M
        size_ -= size;
36
1.43M
        return r;
37
1.43M
    }
38
39
    const std::uint8_t* next(std::size_t size, std::size_t align)
40
1.43M
    {
41
1.43M
        auto& p = const_cast<void*&>(reinterpret_cast<const void*&>(data_));
42
1.43M
        auto r  = std::align(align, size, p, size_);
43
1.43M
        if (r == nullptr)
44
606
            throw no_more_input{};
45
1.43M
        return next(size);
46
1.43M
    }
47
48
    template <typename Fn>
49
    int run(Fn step)
50
612
    {
51
612
        if (size_ > fuzzer_input_max_size)
52
6
            return 0;
53
606
        try {
54
347k
            while (step(*this))
55
346k
                continue;
56
606
        } catch (const no_more_input&) {
57
606
        };
58
606
        return 0;
59
606
    }
60
};
61
62
template <typename T>
63
const T& read(fuzzer_input& fz)
64
1.43M
{
65
1.43M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
1.43M
}
char const& read<char>(fuzzer_input&)
Line
Count
Source
64
1.37M
{
65
1.37M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
1.37M
}
unsigned char const& read<unsigned char>(fuzzer_input&)
Line
Count
Source
64
61.3k
{
65
61.3k
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
61.3k
}
67
68
template <typename T, typename Cond>
69
T read(fuzzer_input& fz, Cond cond)
70
711k
{
71
711k
    auto x = read<T>(fz);
72
1.08M
    while (!cond(x))
73
376k
        x = read<T>(fz);
74
711k
    return x;
75
711k
}
array.cpp:char read<char, LLVMFuzzerTestOneInput::$_0>(fuzzer_input&, LLVMFuzzerTestOneInput::$_0)
Line
Count
Source
70
694k
{
71
694k
    auto x = read<T>(fz);
72
1.02M
    while (!cond(x))
73
332k
        x = read<T>(fz);
74
694k
    return x;
75
694k
}
array.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_1::operator()<immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> > >(immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> >&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_1::operator()<immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> > >(immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> >&) const::{lambda(auto:1)#1})
Line
Count
Source
70
7.73k
{
71
7.73k
    auto x = read<T>(fz);
72
37.5k
    while (!cond(x))
73
29.8k
        x = read<T>(fz);
74
7.73k
    return x;
75
7.73k
}
array.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_2::operator()<immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> > >(immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> >&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_2::operator()<immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> > >(immer::array<int, immer::memory_policy<immer::free_list_heap_policy<immer::cpp_heap, 1024ul>, immer::refcount_policy, immer::spinlock_policy, immer::no_transience_policy, false, true> >&) const::{lambda(auto:1)#1})
Line
Count
Source
70
9.85k
{
71
9.85k
    auto x = read<T>(fz);
72
23.7k
    while (!cond(x))
73
13.9k
        x = read<T>(fz);
74
9.85k
    return x;
75
9.85k
}