Coverage Report

Created: 2025-08-25 06:15

/src/immer/extra/fuzzer/set-gc.cpp
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
#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.10M
    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
2.94k
{
34
2.94k
    auto guard = fuzzer_gc_guard{};
35
36
2.94k
    constexpr auto var_count = 4;
37
38
2.94k
    using set_t =
39
2.94k
        immer::set<size_t, colliding_hash_t, std::equal_to<>, gc_memory>;
40
41
2.94k
    auto vars = std::array<set_t, var_count>{};
42
43
471k
    auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < var_count; };
44
45
138k
    return fuzzer_input{data, size}.run([&](auto& in) {
46
138k
        enum ops
47
138k
        {
48
138k
            op_insert,
49
138k
            op_erase,
50
138k
            op_insert_move,
51
138k
            op_erase_move,
52
138k
            op_iterate,
53
138k
            op_diff
54
138k
        };
55
138k
        auto src = read<char>(in, is_valid_var);
56
138k
        auto dst = read<char>(in, is_valid_var);
57
138k
        switch (read<char>(in)) {
58
77.1k
        case op_insert: {
59
77.1k
            auto value = read<size_t>(in);
60
77.1k
            vars[dst]  = vars[src].insert(value);
61
77.1k
            break;
62
0
        }
63
7.29k
        case op_erase: {
64
7.29k
            auto value = read<size_t>(in);
65
7.29k
            vars[dst]  = vars[src].erase(value);
66
7.29k
            break;
67
0
        }
68
3.99k
        case op_insert_move: {
69
3.99k
            auto value = read<size_t>(in);
70
3.99k
            vars[dst]  = std::move(vars[src]).insert(value);
71
3.99k
            break;
72
0
        }
73
1.09k
        case op_erase_move: {
74
1.09k
            auto value = read<size_t>(in);
75
1.09k
            vars[dst]  = std::move(vars[src]).erase(value);
76
1.09k
            break;
77
0
        }
78
3.47k
        case op_iterate: {
79
3.47k
            auto srcv = vars[src];
80
399k
            immer::for_each(srcv, [&](auto&& v) {
81
399k
                vars[dst] = std::move(vars[dst]).insert(v);
82
399k
            });
83
3.47k
            break;
84
0
        }
85
23.9k
        case op_diff: {
86
23.9k
            auto&& a = vars[src];
87
23.9k
            auto&& b = vars[dst];
88
23.9k
            diff(
89
23.9k
                a,
90
23.9k
                b,
91
149k
                [&](auto&& x) {
92
149k
                    assert(!a.count(x));
93
149k
                    assert(b.count(x));
94
149k
                },
95
142k
                [&](auto&& x) {
96
142k
                    assert(a.count(x));
97
142k
                    assert(!b.count(x));
98
142k
                },
99
23.9k
                [&](auto&& x, auto&& y) { assert(false); });
100
23.9k
        }
101
42.8k
        default:
102
42.8k
            break;
103
138k
        };
104
135k
        return true;
105
138k
    });
106
2.94k
}