Coverage Report

Created: 2025-08-28 06:18

/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.04M
    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.99k
{
34
2.99k
    auto guard = fuzzer_gc_guard{};
35
36
2.99k
    constexpr auto var_count = 4;
37
38
2.99k
    using set_t =
39
2.99k
        immer::set<size_t, colliding_hash_t, std::equal_to<>, gc_memory>;
40
41
2.99k
    auto vars = std::array<set_t, var_count>{};
42
43
462k
    auto is_valid_var = [&](auto idx) { return idx >= 0 && idx < var_count; };
44
45
139k
    return fuzzer_input{data, size}.run([&](auto& in) {
46
139k
        enum ops
47
139k
        {
48
139k
            op_insert,
49
139k
            op_erase,
50
139k
            op_insert_move,
51
139k
            op_erase_move,
52
139k
            op_iterate,
53
139k
            op_diff
54
139k
        };
55
139k
        auto src = read<char>(in, is_valid_var);
56
139k
        auto dst = read<char>(in, is_valid_var);
57
139k
        switch (read<char>(in)) {
58
74.8k
        case op_insert: {
59
74.8k
            auto value = read<size_t>(in);
60
74.8k
            vars[dst]  = vars[src].insert(value);
61
74.8k
            break;
62
0
        }
63
9.63k
        case op_erase: {
64
9.63k
            auto value = read<size_t>(in);
65
9.63k
            vars[dst]  = vars[src].erase(value);
66
9.63k
            break;
67
0
        }
68
4.42k
        case op_insert_move: {
69
4.42k
            auto value = read<size_t>(in);
70
4.42k
            vars[dst]  = std::move(vars[src]).insert(value);
71
4.42k
            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.35k
        case op_iterate: {
79
3.35k
            auto srcv = vars[src];
80
333k
            immer::for_each(srcv, [&](auto&& v) {
81
333k
                vars[dst] = std::move(vars[dst]).insert(v);
82
333k
            });
83
3.35k
            break;
84
0
        }
85
25.6k
        case op_diff: {
86
25.6k
            auto&& a = vars[src];
87
25.6k
            auto&& b = vars[dst];
88
25.6k
            diff(
89
25.6k
                a,
90
25.6k
                b,
91
148k
                [&](auto&& x) {
92
148k
                    assert(!a.count(x));
93
148k
                    assert(b.count(x));
94
148k
                },
95
147k
                [&](auto&& x) {
96
147k
                    assert(a.count(x));
97
147k
                    assert(!b.count(x));
98
147k
                },
99
25.6k
                [&](auto&& x, auto&& y) { assert(false); });
100
25.6k
        }
101
42.9k
        default:
102
42.9k
            break;
103
139k
        };
104
136k
        return true;
105
139k
    });
106
2.99k
}