Coverage Report

Created: 2025-10-27 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/immer/extra/fuzzer/set-gc.cpp
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
#include "fuzzer_gc_guard.hpp"
10
#include "fuzzer_input.hpp"
11
12
#include <immer/heap/gc_heap.hpp>
13
#include <immer/refcount/no_refcount_policy.hpp>
14
#include <immer/set.hpp>
15
16
#include <immer/algorithm.hpp>
17
18
#include <array>
19
20
using gc_memory = immer::memory_policy<immer::heap_policy<immer::gc_heap>,
21
                                       immer::no_refcount_policy,
22
                                       immer::default_lock_policy,
23
                                       immer::gc_transience_policy,
24
                                       false>;
25
26
struct colliding_hash_t
27
{
28
1.24M
    std::size_t operator()(std::size_t x) const { return x & ~15; }
29
};
30
31
extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
32
                                      std::size_t size)
33
3.05k
{
34
3.05k
    auto guard = fuzzer_gc_guard{};
35
36
3.05k
    constexpr auto var_count = 4;
37
38
3.05k
    using set_t =
39
3.05k
        immer::set<size_t, colliding_hash_t, std::equal_to<>, gc_memory>;
40
41
3.05k
    auto vars = std::array<set_t, var_count>{};
42
43
522k
    auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < var_count; };
44
45
163k
    return fuzzer_input{data, size}.run([&](auto& in) {
46
163k
        enum ops
47
163k
        {
48
163k
            op_insert,
49
163k
            op_erase,
50
163k
            op_insert_move,
51
163k
            op_erase_move,
52
163k
            op_iterate,
53
163k
            op_diff
54
163k
        };
55
163k
        auto src = read<char>(in, is_valid_var);
56
163k
        auto dst = read<char>(in, is_valid_var);
57
163k
        switch (read<char>(in)) {
58
85.6k
        case op_insert: {
59
85.6k
            auto value = read<size_t>(in);
60
85.6k
            vars[dst]  = vars[src].insert(value);
61
85.6k
            break;
62
0
        }
63
12.1k
        case op_erase: {
64
12.1k
            auto value = read<size_t>(in);
65
12.1k
            vars[dst]  = vars[src].erase(value);
66
12.1k
            break;
67
0
        }
68
5.52k
        case op_insert_move: {
69
5.52k
            auto value = read<size_t>(in);
70
5.52k
            vars[dst]  = std::move(vars[src]).insert(value);
71
5.52k
            break;
72
0
        }
73
2.61k
        case op_erase_move: {
74
2.61k
            auto value = read<size_t>(in);
75
2.61k
            vars[dst]  = std::move(vars[src]).erase(value);
76
2.61k
            break;
77
0
        }
78
3.91k
        case op_iterate: {
79
3.91k
            auto srcv = vars[src];
80
441k
            immer::for_each(srcv, [&](auto&& v) {
81
441k
                vars[dst] = std::move(vars[dst]).insert(v);
82
441k
            });
83
3.91k
            break;
84
0
        }
85
31.5k
        case op_diff: {
86
31.5k
            auto&& a = vars[src];
87
31.5k
            auto&& b = vars[dst];
88
31.5k
            diff(
89
31.5k
                a,
90
31.5k
                b,
91
138k
                [&](auto&& x) {
92
138k
                    assert(!a.count(x));
93
138k
                    assert(b.count(x));
94
138k
                },
95
190k
                [&](auto&& x) {
96
190k
                    assert(a.count(x));
97
190k
                    assert(!b.count(x));
98
190k
                },
99
31.5k
                [&](auto&& x, auto&& y) { assert(false); });
100
31.5k
        }
101
50.9k
        default:
102
50.9k
            break;
103
163k
        };
104
160k
        return true;
105
163k
    });
106
3.05k
}