Coverage Report

Created: 2025-10-12 06:28

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.38M
    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.12k
{
34
3.12k
    auto guard = fuzzer_gc_guard{};
35
36
3.12k
    constexpr auto var_count = 4;
37
38
3.12k
    using set_t =
39
3.12k
        immer::set<size_t, colliding_hash_t, std::equal_to<>, gc_memory>;
40
41
3.12k
    auto vars = std::array<set_t, var_count>{};
42
43
530k
    auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < var_count; };
44
45
165k
    return fuzzer_input{data, size}.run([&](auto& in) {
46
165k
        enum ops
47
165k
        {
48
165k
            op_insert,
49
165k
            op_erase,
50
165k
            op_insert_move,
51
165k
            op_erase_move,
52
165k
            op_iterate,
53
165k
            op_diff
54
165k
        };
55
165k
        auto src = read<char>(in, is_valid_var);
56
165k
        auto dst = read<char>(in, is_valid_var);
57
165k
        switch (read<char>(in)) {
58
87.2k
        case op_insert: {
59
87.2k
            auto value = read<size_t>(in);
60
87.2k
            vars[dst]  = vars[src].insert(value);
61
87.2k
            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.60k
        case op_insert_move: {
69
5.60k
            auto value = read<size_t>(in);
70
5.60k
            vars[dst]  = std::move(vars[src]).insert(value);
71
5.60k
            break;
72
0
        }
73
1.10k
        case op_erase_move: {
74
1.10k
            auto value = read<size_t>(in);
75
1.10k
            vars[dst]  = std::move(vars[src]).erase(value);
76
1.10k
            break;
77
0
        }
78
3.76k
        case op_iterate: {
79
3.76k
            auto srcv = vars[src];
80
431k
            immer::for_each(srcv, [&](auto&& v) {
81
431k
                vars[dst] = std::move(vars[dst]).insert(v);
82
431k
            });
83
3.76k
            break;
84
0
        }
85
32.9k
        case op_diff: {
86
32.9k
            auto&& a = vars[src];
87
32.9k
            auto&& b = vars[dst];
88
32.9k
            diff(
89
32.9k
                a,
90
32.9k
                b,
91
179k
                [&](auto&& x) {
92
179k
                    assert(!a.count(x));
93
179k
                    assert(b.count(x));
94
179k
                },
95
224k
                [&](auto&& x) {
96
224k
                    assert(a.count(x));
97
224k
                    assert(!b.count(x));
98
224k
                },
99
32.9k
                [&](auto&& x, auto&& y) { assert(false); });
100
32.9k
        }
101
52.1k
        default:
102
52.1k
            break;
103
165k
        };
104
162k
        return true;
105
165k
    });
106
3.12k
}