Coverage Report

Created: 2025-10-12 06:28

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
8.37M
    {
31
8.37M
        if (size_ < size)
32
0
            throw no_more_input{};
33
8.37M
        auto r = data_;
34
8.37M
        data_ += size;
35
8.37M
        size_ -= size;
36
8.37M
        return r;
37
8.37M
    }
38
39
    const std::uint8_t* next(std::size_t size, std::size_t align)
40
8.38M
    {
41
8.38M
        auto& p = const_cast<void*&>(reinterpret_cast<const void*&>(data_));
42
8.38M
        auto r  = std::align(align, size, p, size_);
43
8.38M
        if (r == nullptr)
44
8.38k
            throw no_more_input{};
45
8.37M
        return next(size);
46
8.38M
    }
47
48
    template <typename Fn>
49
    int run(Fn step)
50
8.39k
    {
51
8.39k
        if (size_ > fuzzer_input_max_size)
52
8
            return 0;
53
8.38k
        try {
54
2.10M
            while (step(*this))
55
2.09M
                continue;
56
8.38k
        } catch (const no_more_input&) {
57
8.38k
        };
58
8.38k
        return 0;
59
8.38k
    }
60
};
61
62
template <typename T>
63
const T& read(fuzzer_input& fz)
64
8.38M
{
65
8.38M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
8.38M
}
char const& read<char>(fuzzer_input&)
Line
Count
Source
64
7.97M
{
65
7.97M
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
7.97M
}
unsigned char const& read<unsigned char>(fuzzer_input&)
Line
Count
Source
64
407k
{
65
407k
    return *reinterpret_cast<const T*>(fz.next(sizeof(T), alignof(T)));
66
407k
}
67
68
template <typename T, typename Cond>
69
T read(fuzzer_input& fz, Cond cond)
70
5.76M
{
71
5.76M
    auto x = read<T>(fz);
72
6.28M
    while (!cond(x))
73
520k
        x = read<T>(fz);
74
5.76M
    return x;
75
5.76M
}
flex-vector.cpp:char read<char, LLVMFuzzerTestOneInput::$_0>(fuzzer_input&, LLVMFuzzerTestOneInput::$_0)
Line
Count
Source
70
5.09M
{
71
5.09M
    auto x = read<T>(fz);
72
5.53M
    while (!cond(x))
73
437k
        x = read<T>(fz);
74
5.09M
    return x;
75
5.09M
}
flex-vector.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_2::operator()<immer::flex_vector<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>, 2u, 2u> >(immer::flex_vector<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>, 2u, 2u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_2::operator()<immer::flex_vector<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>, 2u, 2u> >(immer::flex_vector<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>, 2u, 2u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
240k
{
71
240k
    auto x = read<T>(fz);
72
273k
    while (!cond(x))
73
33.4k
        x = read<T>(fz);
74
240k
    return x;
75
240k
}
flex-vector.cpp:unsigned char read<unsigned char, LLVMFuzzerTestOneInput::$_3::operator()<immer::flex_vector<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>, 2u, 2u> >(immer::flex_vector<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>, 2u, 2u>&) const::{lambda(auto:1)#1}>(fuzzer_input&, LLVMFuzzerTestOneInput::$_3::operator()<immer::flex_vector<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>, 2u, 2u> >(immer::flex_vector<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>, 2u, 2u>&) const::{lambda(auto:1)#1})
Line
Count
Source
70
121k
{
71
121k
    auto x = read<T>(fz);
72
133k
    while (!cond(x))
73
12.1k
        x = read<T>(fz);
74
121k
    return x;
75
121k
}
flex-vector.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
311k
{
71
311k
    auto x = read<T>(fz);
72
348k
    while (!cond(x))
73
36.8k
        x = read<T>(fz);
74
311k
    return x;
75
311k
}