Fix misleadingly indented statements.
[pspp.git] / src / libpspp / taint.c
blobc655d60a61cf9009fae1043ee9833877625f2704
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include "libpspp/taint.h"
21 #include <stddef.h>
23 #include "libpspp/array.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/cast.h"
27 #include "gl/xalloc.h"
29 /* This code maintains two invariants:
31 1. If a node is tainted, then all of its successors are
32 tainted.
34 2. If a node is tainted, then it and all of its predecessors are
35 successor-tainted. */
37 /* A list of pointers to taint structures. */
38 struct taint_list
40 size_t cnt;
41 struct taint **taints;
44 static void taint_list_init (struct taint_list *);
45 static void taint_list_destroy (struct taint_list *);
46 static void taint_list_add (struct taint_list *, struct taint *);
47 static void taint_list_remove (struct taint_list *, const struct taint *);
49 /* A taint. */
50 struct taint
52 size_t ref_cnt; /* Number of owners. */
53 struct taint_list successors; /* Successors in graph. */
54 struct taint_list predecessors; /* Predecessors in graph. */
55 bool tainted; /* Is this node tainted? */
56 bool tainted_successor; /* Is/was any derived taint tainted? */
59 static void recursively_set_taint (struct taint *);
60 static void recursively_set_tainted_successor (struct taint *);
62 /* Creates and returns a new taint object. */
63 struct taint *
64 taint_create (void)
66 struct taint *taint = xmalloc (sizeof *taint);
67 taint->ref_cnt = 1;
68 taint_list_init (&taint->successors);
69 taint_list_init (&taint->predecessors);
70 taint->tainted = false;
71 taint->tainted_successor = false;
72 return taint;
75 /* Returns a clone of the given TAINT.
76 The new and old taint objects are logically indistinguishable,
77 as if they were the same object. (In this implementation,
78 they are in fact the same object, but this is not a guarantee
79 made by the interface.) */
80 struct taint *
81 taint_clone (const struct taint *taint_)
83 struct taint *taint = CONST_CAST (struct taint *, taint_);
85 assert (taint->ref_cnt > 0);
86 taint->ref_cnt++;
87 return taint;
90 /* Destroys the given TAINT.
91 Returns false if TAINT was tainted, true otherwise.
92 Any propagation relationships through TAINT are preserved.
93 That is, if A taints B and B taints C, then destroying B will
94 preserve the transitive relationship, so that tainting A will
95 still taint C. */
96 bool
97 taint_destroy (struct taint *taint)
99 if ( taint )
101 bool was_tainted = taint_is_tainted (taint);
102 if (--taint->ref_cnt == 0)
104 size_t i, j;
106 for (i = 0; i < taint->predecessors.cnt; i++)
107 for (j = 0; j < taint->successors.cnt; j++)
108 taint_propagate (taint->predecessors.taints[i],
109 taint->successors.taints[j]);
111 for (i = 0; i < taint->predecessors.cnt; i++)
112 taint_list_remove (&taint->predecessors.taints[i]->successors, taint);
113 for (i = 0; i < taint->successors.cnt; i++)
114 taint_list_remove (&taint->successors.taints[i]->predecessors, taint);
116 taint_list_destroy (&taint->successors);
117 taint_list_destroy (&taint->predecessors);
118 free (taint);
120 return !was_tainted;
123 return true;
126 /* Adds a propagation relationship from FROM to TO. This means
127 that, should FROM ever become tainted, then TO will
128 automatically be marked tainted as well. This takes effect
129 immediately: if FROM is currently tainted, then TO will be
130 tainted after the call completes.
132 Taint propagation is transitive: if A propagates to B and B
133 propagates to C, then tainting A taints both B and C. Taint
134 propagation is not commutative: propagation from A to B does
135 not imply propagation from B to A. Taint propagation is
136 robust against loops, so that if A propagates to B and vice
137 versa, whether directly or indirectly, then tainting either A
138 or B will cause the other to be tainted, without producing an
139 infinite loop. */
140 void
141 taint_propagate (const struct taint *from_, const struct taint *to_)
143 struct taint *from = CONST_CAST (struct taint *, from_);
144 struct taint *to = CONST_CAST (struct taint *, to_);
146 if (from != to)
148 taint_list_add (&from->successors, to);
149 taint_list_add (&to->predecessors, from);
150 if (from->tainted && !to->tainted)
151 recursively_set_taint (to);
152 else if (to->tainted_successor && !from->tainted_successor)
153 recursively_set_tainted_successor (from);
157 /* Returns true if TAINT is tainted, false otherwise. */
158 bool
159 taint_is_tainted (const struct taint *taint)
161 return taint->tainted;
164 /* Marks TAINT tainted and propagates the taint to all of its
165 successors. */
166 void
167 taint_set_taint (const struct taint *taint_)
169 struct taint *taint = CONST_CAST (struct taint *, taint_);
170 if (!taint->tainted)
171 recursively_set_taint (taint);
174 /* Returns true if TAINT is successor-tainted, that is, if it or
175 any of its successors is or ever has been tainted. (A
176 "successor" of a taint object X is any taint object that can
177 be reached by following propagation relationships starting
178 from X.) */
179 bool
180 taint_has_tainted_successor (const struct taint *taint)
182 return taint->tainted_successor;
185 /* Attempts to reset the successor-taint on TAINT. This is
186 successful only if TAINT currently has no tainted successor. */
187 void
188 taint_reset_successor_taint (const struct taint *taint_)
190 struct taint *taint = CONST_CAST (struct taint *, taint_);
192 if (taint->tainted_successor)
194 size_t i;
196 for (i = 0; i < taint->successors.cnt; i++)
197 if (taint->successors.taints[i]->tainted_successor)
198 return;
200 taint->tainted_successor = false;
204 /* Initializes LIST as an empty list of taints. */
205 static void
206 taint_list_init (struct taint_list *list)
208 list->cnt = 0;
209 list->taints = NULL;
212 /* Destroys LIST. */
213 static void
214 taint_list_destroy (struct taint_list *list)
216 free (list->taints);
219 /* Returns true if TAINT is in LIST, false otherwise. */
220 static bool
221 taint_list_contains (const struct taint_list *list, const struct taint *taint)
223 size_t i;
225 for (i = 0; i < list->cnt; i++)
226 if (list->taints[i] == taint)
227 return true;
229 return false;
232 /* Returns true if X is zero or a power of 2, false otherwise. */
233 static bool
234 is_zero_or_power_of_2 (size_t x)
236 return (x & (x - 1)) == 0;
239 /* Adds TAINT to LIST, if it isn't already in the list. */
240 static void
241 taint_list_add (struct taint_list *list, struct taint *taint)
243 if (!taint_list_contains (list, taint))
245 /* To save a few bytes of memory per list, we don't store
246 the list capacity as a separate member. Instead, the
247 list capacity is always zero or a power of 2. Thus, if
248 the list count is one of these threshold values, we need
249 to allocate more memory. */
250 if (is_zero_or_power_of_2 (list->cnt))
251 list->taints = xnrealloc (list->taints,
252 list->cnt == 0 ? 1 : 2 * list->cnt,
253 sizeof *list->taints);
254 list->taints[list->cnt++] = taint;
258 /* Removes TAINT from LIST (which must contain it). */
259 static void
260 taint_list_remove (struct taint_list *list, const struct taint *taint)
262 size_t i;
264 for (i = 0; i < list->cnt; i++)
265 if (list->taints[i] == taint)
267 remove_element (list->taints, list->cnt, sizeof *list->taints, i);
268 list->cnt--;
269 return;
272 NOT_REACHED ();
275 /* Marks TAINT as tainted, as well as all of its successors
276 recursively. Also marks TAINT's predecessors as
277 successor-tainted, recursively. */
278 static void
279 recursively_set_taint (struct taint *taint)
281 size_t i;
283 taint->tainted = taint->tainted_successor = true;
284 for (i = 0; i < taint->successors.cnt; i++)
286 struct taint *s = taint->successors.taints[i];
287 if (!s->tainted)
288 recursively_set_taint (s);
290 for (i = 0; i < taint->predecessors.cnt; i++)
292 struct taint *p = taint->predecessors.taints[i];
293 if (!p->tainted_successor)
294 recursively_set_tainted_successor (p);
298 /* Marks TAINT as successor-tainted, as well as all of its
299 predecessors recursively. */
300 static void
301 recursively_set_tainted_successor (struct taint *taint)
303 size_t i;
305 taint->tainted_successor = true;
306 for (i = 0; i < taint->predecessors.cnt; i++)
308 struct taint *p = taint->predecessors.taints[i];
309 if (!p->tainted_successor)
310 recursively_set_tainted_successor (p);