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/>. */
19 #include "libpspp/taint.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
34 2. If a node is tainted, then it and all of its predecessors are
37 /* A list of pointers to taint structures. */
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 *);
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. */
66 struct taint *taint = xmalloc (sizeof *taint);
68 taint_list_init (&taint->successors);
69 taint_list_init (&taint->predecessors);
70 taint->tainted = false;
71 taint->tainted_successor = false;
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.) */
81 taint_clone (const struct taint *taint_)
83 struct taint *taint = CONST_CAST (struct taint *, taint_);
85 assert (taint->ref_cnt > 0);
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
97 taint_destroy (struct taint *taint)
101 bool was_tainted = taint_is_tainted (taint);
102 if (--taint->ref_cnt == 0)
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);
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
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_);
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. */
159 taint_is_tainted (const struct taint *taint)
161 return taint->tainted;
164 /* Marks TAINT tainted and propagates the taint to all of its
167 taint_set_taint (const struct taint *taint_)
169 struct taint *taint = CONST_CAST (struct taint *, taint_);
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
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. */
188 taint_reset_successor_taint (const struct taint *taint_)
190 struct taint *taint = CONST_CAST (struct taint *, taint_);
192 if (taint->tainted_successor)
196 for (i = 0; i < taint->successors.cnt; i++)
197 if (taint->successors.taints[i]->tainted_successor)
200 taint->tainted_successor = false;
204 /* Initializes LIST as an empty list of taints. */
206 taint_list_init (struct taint_list *list)
214 taint_list_destroy (struct taint_list *list)
219 /* Returns true if TAINT is in LIST, false otherwise. */
221 taint_list_contains (const struct taint_list *list, const struct taint *taint)
225 for (i = 0; i < list->cnt; i++)
226 if (list->taints[i] == taint)
232 /* Returns true if X is zero or a power of 2, false otherwise. */
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. */
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). */
260 taint_list_remove (struct taint_list *list, const struct taint *taint)
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);
275 /* Marks TAINT as tainted, as well as all of its successors
276 recursively. Also marks TAINT's predecessors as
277 successor-tainted, recursively. */
279 recursively_set_taint (struct taint *taint)
283 taint->tainted = taint->tainted_successor = true;
284 for (i = 0; i < taint->successors.cnt; i++)
286 struct taint *s = taint->successors.taints[i];
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. */
301 recursively_set_tainted_successor (struct taint *taint)
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);