/src/igraph/fuzzing/weighted_centrality.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  | 755  | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { | 
25  | 755  |     igraph_t graph;  | 
26  | 755  |     igraph_vector_int_t edges;  | 
27  | 755  |     igraph_vector_t weights;  | 
28  |  |  | 
29  | 755  |     igraph_set_warning_handler(igraph_warning_handler_ignore);  | 
30  |  |  | 
31  | 755  |     if (Size % 3 == 0 || Size % 3 == 2 || Size > (3 * 256) + 1 || Size < 1) { | 
32  | 15  |         return 0;  | 
33  | 15  |     }  | 
34  |  |  | 
35  | 740  |     igraph_vector_int_init(&edges, ((Size-1) / 3) * 2);  | 
36  | 740  |     igraph_vector_init(&weights, (Size-1) / 3);  | 
37  | 17.7k  |     for (size_t i=0; i < ((Size-1) / 3); ++i) { | 
38  | 17.0k  |         VECTOR(edges)[i * 2] = Data[i * 3 + 1];  | 
39  | 17.0k  |         VECTOR(edges)[i * 2 + 1] = Data[i * 3 + 2];  | 
40  |  |         // We keep the weights strictly positive, as this is required by some algorithms.  | 
41  |  |         // Error at src/centrality/betweenness.c:437 : Weight vector must be positive. - Invalid value.  | 
42  | 17.0k  |         VECTOR(weights)[i] = ((double) Data[i * 3 + 3] + 1.0) / 105.0;  | 
43  | 17.0k  |     }  | 
44  |  |  | 
45  |  |     // Turn on attribute handling. Weights will be stored as an edge attribute  | 
46  |  |     // in order to allow graph simplification while retainingweights.  | 
47  | 740  |     igraph_set_attribute_table(&igraph_cattribute_table);  | 
48  |  |  | 
49  | 740  |     igraph_rng_seed(igraph_rng_default(), 42);  | 
50  |  |  | 
51  | 740  |     if (igraph_create(&graph, &edges, Data[0], IGRAPH_DIRECTED) == IGRAPH_SUCCESS) { | 
52  | 740  |         igraph_vector_t v;  | 
53  | 740  |         igraph_vector_int_t iv;  | 
54  | 740  |         igraph_bool_t b;  | 
55  | 740  |         igraph_real_t r;  | 
56  |  |  | 
57  |  |         /* Limit graph size for the sake of performance. */  | 
58  | 740  |         if (igraph_vcount(&graph) <= 64) { | 
59  | 717  |             igraph_vector_init(&v, 0);  | 
60  | 717  |             igraph_vector_int_init(&iv, 0);  | 
61  |  |  | 
62  | 717  |             igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_ALL, false, 4);  | 
63  | 717  |             igraph_betweenness_cutoff(&graph, &weights, &v, igraph_vss_all(), IGRAPH_IN, false, 5);  | 
64  | 717  |             igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,  | 
65  | 717  |                                            false, 4);  | 
66  | 717  |             igraph_edge_betweenness_cutoff(&graph, &weights, &v, igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_UNDIRECTED,  | 
67  | 717  |                                            false, 3);  | 
68  | 717  |             if (igraph_vcount(&graph) >= 10) { | 
69  | 637  |                 igraph_betweenness_subset(&graph, &weights,  | 
70  | 637  |                                           &v,  | 
71  | 637  |                                           igraph_vss_range(0,5), igraph_vss_range(5,10),  | 
72  | 637  |                                           igraph_vss_all(), IGRAPH_DIRECTED, false);  | 
73  | 637  |                 igraph_edge_betweenness_subset(&graph, &weights,  | 
74  | 637  |                                                &v,  | 
75  | 637  |                                                igraph_vss_range(0,10), igraph_vss_range(0, 10),  | 
76  | 637  |                                                igraph_ess_all(IGRAPH_EDGEORDER_ID), IGRAPH_DIRECTED,false);  | 
77  | 637  |             }  | 
78  | 717  |             igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);  | 
79  | 717  |             igraph_closeness_cutoff(&graph, &v, &iv, &b, igraph_vss_all(), IGRAPH_OUT, &weights, true, 4);  | 
80  | 717  |             igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_ALL, &weights, true, 3);  | 
81  | 717  |             igraph_harmonic_centrality_cutoff(&graph, &v, igraph_vss_all(), IGRAPH_IN, &weights, true, 4);  | 
82  | 717  |             igraph_global_efficiency(&graph, &weights, &r, IGRAPH_DIRECTED);  | 
83  | 717  |             igraph_local_efficiency(&graph, &weights, &v, igraph_vss_all(), IGRAPH_DIRECTED, IGRAPH_OUT);  | 
84  | 717  |             igraph_pagerank(&graph, &weights, &v, &r, 0.6, IGRAPH_DIRECTED, igraph_vss_all(),  | 
85  | 717  |                             IGRAPH_PAGERANK_ALGO_PRPACK, NULL);  | 
86  | 717  |             igraph_constraint(&graph, &v, igraph_vss_all(), &weights);  | 
87  |  |  | 
88  | 717  |             { | 
89  | 717  |                 igraph_attribute_combination_t comb;  | 
90  | 717  |                 SETEANV(&graph, "weight", &weights);  | 
91  | 717  |                 igraph_attribute_combination(&comb,  | 
92  | 717  |                                              "weight", IGRAPH_ATTRIBUTE_COMBINE_SUM,  | 
93  | 717  |                                              IGRAPH_NO_MORE_ATTRIBUTES);  | 
94  |  |                 // This operation would be simpler if we use IGRAPH_TO_UNDIRECTED_EACH,  | 
95  |  |                 // as collapsing edges happens in the simplification step anyway.  | 
96  |  |                 // We use IGRAPH_TO_UNDIRECTED_COLLAPSE to exercise more code.  | 
97  | 717  |                 igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_COLLAPSE, &comb);  | 
98  | 717  |                 igraph_simplify(&graph, true, true, &comb);  | 
99  | 717  |                 igraph_attribute_combination_destroy(&comb);  | 
100  | 717  |                 EANV(&graph, "weight", &weights);  | 
101  | 717  |                 DELEA(&graph, "weight");  | 
102  | 717  |             }  | 
103  |  |  | 
104  | 717  |             igraph_diversity(&graph, &weights, &v, igraph_vss_all());  | 
105  | 717  |             igraph_transitivity_barrat(&graph, &v, igraph_vss_all(), &weights, IGRAPH_TRANSITIVITY_NAN);  | 
106  |  |  | 
107  | 717  |             igraph_vector_int_destroy(&iv);  | 
108  | 717  |             igraph_vector_destroy(&v);  | 
109  | 717  |         }  | 
110  |  |  | 
111  | 740  |         igraph_destroy(&graph);  | 
112  | 740  |     }  | 
113  |  |  | 
114  | 740  |     igraph_vector_int_destroy(&edges);  | 
115  | 740  |     igraph_vector_destroy(&weights);  | 
116  |  |  | 
117  | 740  |     IGRAPH_ASSERT(IGRAPH_FINALLY_STACK_EMPTY);  | 
118  |  |  | 
119  | 740  |     return 0;  // Non-zero return values are reserved for future use.  | 
120  | 740  | }  |