Coverage Report

Created: 2025-07-18 07:08

/src/igraph/fuzzing/linear_algos_directed.cpp
Line
Count
Source
1
/*
2
   IGraph library.
3
   Copyright (C) 2024  The igraph development team
4
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 2 of the License, or
8
   (at your option) any later version.
9
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, write to the Free Software
17
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
   02110-1301 USA
19
*/
20
21
#include <igraph.h>
22
#include <cstdlib>
23
24
#include "fuzz_utilities.h"
25
26
1.77k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
27
1.77k
    igraph_t graph;
28
1.77k
    igraph_vector_int_t edges;
29
30
1.77k
    igraph_set_warning_handler(igraph_warning_handler_ignore);
31
32
1.77k
    if (Size % 2 == 0 || Size > 512+1 || Size < 1) {
33
14
        return 0;
34
14
    }
35
36
1.76k
    igraph_vector_int_init(&edges, Size-1);
37
134k
    for (size_t i=0; i < Size-1; ++i) {
38
132k
        VECTOR(edges)[i] = Data[i+1];
39
132k
    }
40
41
    /* Directed */
42
1.76k
    if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) {
43
1.76k
        igraph_vector_int_list_t ivl1, ivl2;
44
1.76k
        igraph_vector_int_t iv1, iv2, iv3, iv4, iv5;
45
1.76k
        igraph_graph_list_t gl;
46
1.76k
        igraph_vector_bool_t bv;
47
1.76k
        igraph_matrix_t m;
48
1.76k
        igraph_integer_t i, i2;
49
1.76k
        igraph_bool_t b, b2, loop, multi, graphical;
50
1.76k
        igraph_real_t r;
51
1.76k
        igraph_t g;
52
53
1.76k
        igraph_vector_int_list_init(&ivl1, 0);
54
1.76k
        igraph_vector_int_list_init(&ivl2, 0);
55
1.76k
        igraph_vector_int_init(&iv1, 0);
56
1.76k
        igraph_vector_int_init(&iv2, 0);
57
1.76k
        igraph_vector_int_init(&iv3, 0);
58
1.76k
        igraph_vector_int_init(&iv4, 0);
59
1.76k
        igraph_vector_int_init(&iv5, 0);
60
1.76k
        igraph_vector_bool_init(&bv, 0);
61
1.76k
        igraph_matrix_init(&m, 0, 0);
62
63
1.76k
        igraph_find_cycle(&graph, &iv1, &iv2, IGRAPH_IN);
64
1.76k
        igraph_connected_components(&graph, &iv1, &iv2, &i, IGRAPH_STRONG);
65
1.76k
        igraph_coreness(&graph, &iv1, IGRAPH_OUT);
66
1.76k
        igraph_assortativity_degree(&graph, &r, IGRAPH_DIRECTED);
67
1.76k
        igraph_count_multiple(&graph, &iv1, igraph_ess_all(IGRAPH_EDGEORDER_ID));
68
1.76k
        igraph_is_loop(&graph, &bv, igraph_ess_all(IGRAPH_EDGEORDER_FROM));
69
1.76k
        igraph_is_multiple(&graph, &bv, igraph_ess_all(IGRAPH_EDGEORDER_TO));
70
1.76k
        igraph_is_mutual(&graph, &bv, igraph_ess_all(IGRAPH_EDGEORDER_TO), false);
71
1.76k
        igraph_maxdegree(&graph, &i, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS);
72
1.76k
        igraph_mean_degree(&graph, &r, IGRAPH_NO_LOOPS);
73
1.76k
        igraph_reciprocity(&graph, &r, true, IGRAPH_RECIPROCITY_DEFAULT);
74
75
        /* Graphicality and graph realization based on the degrees of 'graph'. */
76
1.76k
        igraph_has_loop(&graph, &loop);
77
1.76k
        igraph_has_multiple(&graph, &multi);
78
1.76k
        igraph_degree(&graph, &iv1, igraph_vss_all(), IGRAPH_OUT, IGRAPH_LOOPS);
79
1.76k
        igraph_degree(&graph, &iv2, igraph_vss_all(), IGRAPH_IN, IGRAPH_LOOPS);
80
1.76k
        igraph_is_graphical(&iv1, &iv2, IGRAPH_SIMPLE_SW, &graphical);
81
1.76k
        if (!loop && !multi) {
82
1.00k
            IGRAPH_ASSERT(graphical);
83
1.00k
        }
84
1.76k
        if (graphical) {
85
1.26k
            igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST);
86
1.26k
            igraph_destroy(&g);
87
1.26k
            igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST);
88
1.26k
            igraph_destroy(&g);
89
1.26k
            igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX);
90
1.26k
            igraph_destroy(&g);
91
1.26k
        } else {
92
500
            CHECK_ERROR(
93
500
                igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST),
94
500
                IGRAPH_EINVAL);
95
500
            CHECK_ERROR(
96
500
                igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST),
97
500
                IGRAPH_EINVAL);
98
500
            CHECK_ERROR(
99
500
                igraph_realize_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX),
100
500
                IGRAPH_EINVAL);
101
500
        }
102
1.76k
        igraph_is_graphical(&iv1, &iv2, IGRAPH_LOOPS_SW, &graphical);
103
1.76k
        if (!multi) {
104
1.21k
            IGRAPH_ASSERT(graphical);
105
1.21k
        }
106
1.76k
        if (graphical) {
107
            /* Directed realization with loops but no multi-edges is not yet implemented. */
108
            /* Realize as bipartite (equivalent). */
109
1.39k
            igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST);
110
1.39k
            igraph_destroy(&g);
111
1.39k
            igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST);
112
1.39k
            igraph_destroy(&g);
113
1.39k
            igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX);
114
1.39k
            igraph_destroy(&g);
115
1.39k
        } else {
116
368
            CHECK_ERROR(
117
368
                igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST),
118
368
                IGRAPH_EINVAL);
119
368
            CHECK_ERROR(
120
368
                igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST),
121
368
                IGRAPH_EINVAL);
122
368
            CHECK_ERROR(
123
368
                igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_SIMPLE_SW, IGRAPH_REALIZE_DEGSEQ_INDEX),
124
368
                IGRAPH_EINVAL);
125
368
        }
126
1.76k
        igraph_is_graphical(&iv1, &iv2, IGRAPH_MULTI_SW, &graphical);
127
1.76k
        if (!loop) {
128
1.22k
            IGRAPH_ASSERT(graphical);
129
            /* Directed realization with multi-edges but no loops is not yet implemented. */
130
1.22k
        }
131
1.76k
        igraph_is_graphical(&iv1, &iv2, IGRAPH_LOOPS_SW | IGRAPH_MULTI_SW, &graphical);
132
1.76k
        IGRAPH_ASSERT(graphical);
133
        /* Directed realization with loops and multi-edges is not yet implemented. */
134
        /* Realize as bipartite (equivalent). */
135
1.76k
        igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_SMALLEST);
136
1.76k
        igraph_destroy(&g);
137
1.76k
        igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_LARGEST);
138
1.76k
        igraph_destroy(&g);
139
1.76k
        igraph_realize_bipartite_degree_sequence(&g, &iv1, &iv2, IGRAPH_MULTI_SW, IGRAPH_REALIZE_DEGSEQ_INDEX);
140
1.76k
        igraph_destroy(&g);
141
142
        // These algorithms require a starting vertex,
143
        // so we require the graph to have at least one vertex.
144
1.76k
        if (igraph_vcount(&graph) >= 1) {
145
1.76k
            igraph_distances(&graph, &m, igraph_vss_1(0), igraph_vss_all(), IGRAPH_OUT);
146
1.76k
            igraph_get_shortest_paths(&graph, &ivl1, &ivl2, 0, igraph_vss_all(), IGRAPH_OUT, &iv1, &iv2);
147
1.76k
            igraph_pseudo_diameter(&graph, NULL, &r, 0, &i, &i2, IGRAPH_DIRECTED, true);
148
1.76k
            igraph_bfs(&graph, 0, NULL, IGRAPH_OUT, true, NULL, &iv1, &iv2, &iv3, &iv4, NULL, &iv5, NULL, NULL);
149
150
1.76k
            igraph_reverse_edges(&graph, igraph_ess_all(IGRAPH_EDGEORDER_ID));
151
152
1.76k
            igraph_dfs(&graph, 0, IGRAPH_OUT, true, &iv1, &iv2, &iv3, &iv4, NULL, NULL, NULL);
153
1.76k
            igraph_bfs_simple(&graph, 0, IGRAPH_OUT, &iv1, &iv2, &iv3);
154
1.76k
            igraph_dominator_tree(&graph, 0, &iv1, NULL, &iv2, IGRAPH_OUT);
155
1.76k
            igraph_subcomponent(&graph, &iv1, 0, IGRAPH_OUT);
156
1.76k
            igraph_degree_1(&graph, &i, 0, IGRAPH_OUT, IGRAPH_LOOPS);
157
1.76k
            igraph_degree_1(&graph, &i, 0, IGRAPH_OUT, IGRAPH_NO_LOOPS);
158
159
1.76k
            igraph_vector_int_resize(&iv1, 1);
160
1.76k
            VECTOR(iv1)[0] = 0;
161
1.76k
            igraph_unfold_tree(&graph, &g, IGRAPH_IN, &iv1, &iv2);
162
1.76k
            igraph_destroy(&g);
163
1.76k
        }
164
165
1.76k
        igraph_is_dag(&graph, &b);
166
1.76k
        if (b) {
167
647
            igraph_topological_sorting(&graph, &iv1, IGRAPH_OUT);
168
647
        }
169
170
1.76k
        igraph_feedback_arc_set(&graph, &iv1, NULL, IGRAPH_FAS_APPROX_EADES);
171
172
1.76k
        igraph_is_eulerian(&graph, &b, &b2);
173
1.76k
        if (b) igraph_eulerian_path(&graph, &iv1, &iv2);
174
1.76k
        if (b2) igraph_eulerian_cycle(&graph, &iv1, &iv2);
175
176
1.76k
        igraph_graph_list_init(&gl, 0);
177
1.76k
        igraph_decompose(&graph, &gl, IGRAPH_STRONG, 10, 5);
178
1.76k
        igraph_graph_list_destroy(&gl);
179
180
1.76k
        if (igraph_vcount(&graph) >= 2) {
181
1.74k
            igraph_get_all_eids_between(&graph, &iv2, 0, 1, IGRAPH_DIRECTED);
182
1.74k
            igraph_get_all_eids_between(&graph, &iv2, 1, 0, IGRAPH_UNDIRECTED);
183
1.74k
            igraph_get_all_eids_between(&graph, &iv2, 0, 0, IGRAPH_UNDIRECTED);
184
185
1.74k
            igraph_edges(&graph, igraph_ess_all(IGRAPH_EDGEORDER_FROM), &iv1, 0);
186
1.74k
            igraph_vector_int_push_back(&iv1, 0);
187
1.74k
            igraph_vector_int_push_back(&iv1, 1);
188
1.74k
            igraph_vector_int_push_back(&iv1, 1);
189
1.74k
            igraph_vector_int_push_back(&iv1, 0);
190
1.74k
            igraph_vector_int_push_back(&iv1, 1);
191
1.74k
            igraph_vector_int_push_back(&iv1, 1);
192
1.74k
            igraph_get_eids(&graph, &iv2, &iv1, IGRAPH_DIRECTED, false);
193
1.74k
        }
194
195
1.76k
        igraph_simplify(&graph, true, true, NULL);
196
197
1.76k
        if (igraph_vcount(&graph) >=1) {
198
            // Run only on the simplified graph to avoid a very large number of
199
            // shortest paths due to multi-edges.
200
1.76k
            igraph_get_all_shortest_paths(&graph, &ivl1, &ivl2, &iv1, 0, igraph_vss_all(), IGRAPH_ALL);
201
1.76k
        }
202
203
        /* Basic graph modification */
204
1.76k
        igraph_add_vertices(&graph, 3, NULL);
205
1.76k
        igraph_degree_1(&graph, &i, 0, IGRAPH_IN, IGRAPH_NO_LOOPS);
206
1.76k
        igraph_delete_vertices(&graph, igraph_vss_1(0));
207
1.76k
        igraph_add_edge(&graph, 0, 1);
208
1.76k
        igraph_count_multiple_1(&graph, &i, 0);
209
1.76k
        igraph_delete_edges(&graph, igraph_ess_1(0));
210
211
1.76k
        if (igraph_vcount(&graph) >= 4) {
212
1.74k
            igraph_rewire(&graph, igraph_ecount(&graph) + 1, IGRAPH_REWIRING_SIMPLE);
213
1.74k
        }
214
215
1.76k
        igraph_matrix_destroy(&m);
216
1.76k
        igraph_vector_bool_destroy(&bv);
217
1.76k
        igraph_vector_int_destroy(&iv5);
218
1.76k
        igraph_vector_int_destroy(&iv4);
219
1.76k
        igraph_vector_int_destroy(&iv3);
220
1.76k
        igraph_vector_int_destroy(&iv2);
221
1.76k
        igraph_vector_int_destroy(&iv1);
222
1.76k
        igraph_vector_int_list_destroy(&ivl2);
223
1.76k
        igraph_vector_int_list_destroy(&ivl1);
224
225
1.76k
        igraph_destroy(&graph);
226
1.76k
    }
227
228
1.76k
    igraph_vector_int_destroy(&edges);
229
230
1.76k
    IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);
231
232
1.76k
    return 0;  // Non-zero return values are reserved for future use.
233
1.76k
}