tor  0.4.2.1-alpha-dev
pubsub_check.c
Go to the documentation of this file.
1 /* Copyright (c) 2001, Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2018, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
12 #define PUBSUB_PRIVATE
13 
14 #include "lib/dispatch/dispatch_naming.h"
15 #include "lib/dispatch/msgtypes.h"
19 
20 #include "lib/container/bitarray.h"
22 #include "lib/log/util_bug.h"
23 #include "lib/malloc/malloc.h"
25 
26 #include <string.h>
27 
28 static void pubsub_adjmap_add(pubsub_adjmap_t *map,
29  const pubsub_cfg_t *item);
30 
35 static pubsub_adjmap_t *
37 {
38  pubsub_adjmap_t *map = tor_malloc_zero(sizeof(*map));
39  const size_t n_subsystems = get_num_subsys_ids();
40  const size_t n_msgs = get_num_message_ids();
41 
42  map->n_subsystems = n_subsystems;
43  map->n_msgs = n_msgs;
44 
45  map->pub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
46  map->sub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
47  map->pub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
48  map->sub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
49 
50  SMARTLIST_FOREACH_BEGIN(cfg->items, const pubsub_cfg_t *, item) {
51  pubsub_adjmap_add(map, item);
52  } SMARTLIST_FOREACH_END(item);
53 
54  return map;
55 }
56 
60 static void
61 pubsub_adjmap_add(pubsub_adjmap_t *map,
62  const pubsub_cfg_t *item)
63 {
64  smartlist_t **by_subsys;
65  smartlist_t **by_msg;
66 
67  tor_assert(item->subsys < map->n_subsystems);
68  tor_assert(item->msg < map->n_msgs);
69 
70  if (item->is_publish) {
71  by_subsys = &map->pub_by_subsys[item->subsys];
72  by_msg = &map->pub_by_msg[item->msg];
73  } else {
74  by_subsys = &map->sub_by_subsys[item->subsys];
75  by_msg = &map->sub_by_msg[item->msg];
76  }
77 
78  if (! *by_subsys)
79  *by_subsys = smartlist_new();
80  if (! *by_msg)
81  *by_msg = smartlist_new();
82  smartlist_add(*by_subsys, (void*) item);
83  smartlist_add(*by_msg, (void *) item);
84 }
85 
89 #define pubsub_adjmap_free(m) \
90  FREE_AND_NULL(pubsub_adjmap_t, pubsub_adjmap_free_, m)
91 
96 static void
98 {
99  if (!lsts)
100  return;
101 
102  for (unsigned i = 0; i < n; ++i) {
103  smartlist_free(lsts[i]);
104  }
105  tor_free(lsts);
106 }
107 
111 static void
112 pubsub_adjmap_free_(pubsub_adjmap_t *map)
113 {
114  if (!map)
115  return;
116  pubsub_adjmap_free_helper(map->pub_by_subsys, map->n_subsystems);
117  pubsub_adjmap_free_helper(map->sub_by_subsys, map->n_subsystems);
118  pubsub_adjmap_free_helper(map->pub_by_msg, map->n_msgs);
119  pubsub_adjmap_free_helper(map->sub_by_msg, map->n_msgs);
120  tor_free(map);
121 }
122 
126 static int
128 {
129  if (sl)
130  return smartlist_len(sl);
131  else
132  return 0;
133 }
134 
137 static const char *
138 format_flags(unsigned flags)
139 {
140  static char buf[32];
141  buf[0] = 0;
142  if (flags & DISP_FLAG_EXCL) {
143  strlcat(buf, " EXCL", sizeof(buf));
144  }
145  if (flags & DISP_FLAG_STUB) {
146  strlcat(buf, " STUB", sizeof(buf));
147  }
148  return buf[0] ? buf+1 : buf;
149 }
150 
155 static void
156 pubsub_cfg_dump(const pubsub_cfg_t *cfg, int severity, const char *prefix)
157 {
158  tor_assert(prefix);
159 
160  tor_log(severity, LD_MESG,
161  "%s%s %s: %s{%s} on %s (%s) <%u %u %u %u %x> [%s:%d]",
162  prefix,
163  get_subsys_id_name(cfg->subsys),
164  cfg->is_publish ? "PUB" : "SUB",
165  get_message_id_name(cfg->msg),
166  get_msg_type_id_name(cfg->type),
167  get_channel_id_name(cfg->channel),
168  format_flags(cfg->flags),
169  cfg->subsys, cfg->msg, cfg->type, cfg->channel, cfg->flags,
170  cfg->added_by_file, cfg->added_by_line);
171 }
172 
177 static void
178 get_message_bitarray(const pubsub_adjmap_t *map,
179  const smartlist_t *items,
180  bitarray_t **out)
181 {
182  *out = bitarray_init_zero((unsigned)map->n_subsystems);
183  if (! items)
184  return;
185 
186  SMARTLIST_FOREACH_BEGIN(items, const pubsub_cfg_t *, cfg) {
187  bitarray_set(*out, cfg->subsys);
188  } SMARTLIST_FOREACH_END(cfg);
189 }
190 
201 static int
202 lint_message_graph(const pubsub_adjmap_t *map,
203  message_id_t msg,
204  const smartlist_t *pub,
205  const smartlist_t *sub)
206 {
207  bitarray_t *published_by = NULL;
208  bitarray_t *subscribed_by = NULL;
209  bool ok = true;
210 
211  get_message_bitarray(map, pub, &published_by);
212  get_message_bitarray(map, sub, &subscribed_by);
213 
214  /* Check whether any subsystem is publishing and subscribing the same
215  * message. [??]
216  */
217  for (unsigned i = 0; i < map->n_subsystems; ++i) {
218  if (bitarray_is_set(published_by, i) &&
219  bitarray_is_set(subscribed_by, i)) {
220  log_warn(LD_MESG|LD_BUG,
221  "Message \"%s\" is published and subscribed by the same "
222  "subsystem \"%s\".",
223  get_message_id_name(msg),
224  get_subsys_id_name(i));
225  ok = false;
226  }
227  }
228 
229  bitarray_free(published_by);
230  bitarray_free(subscribed_by);
231 
232  return ok ? 0 : -1;
233 }
234 
239 static int
240 lint_message_consistency(message_id_t msg,
241  const smartlist_t *pub,
242  const smartlist_t *sub)
243 {
244  if (!smartlist_len_opt(pub) && !smartlist_len_opt(sub))
245  return 0; // LCOV_EXCL_LINE -- this was already checked.
246 
247  /* The 'all' list has the publishers and the subscribers. */
248  smartlist_t *all = smartlist_new();
249  if (pub)
250  smartlist_add_all(all, pub);
251  if (sub)
252  smartlist_add_all(all, sub);
253 
254  const pubsub_cfg_t *item0 = smartlist_get(all, 0);
255 
256  /* Indicates which subsystems we've found publishing/subscribing here. */
257  bool pub_excl = false, sub_excl = false, chan_same = true, type_same = true;
258 
259  /* Simple message consistency properties across messages.
260  */
261  SMARTLIST_FOREACH_BEGIN(all, const pubsub_cfg_t *, cfg) {
262  chan_same &= (cfg->channel == item0->channel);
263  type_same &= (cfg->type == item0->type);
264  if (cfg->is_publish)
265  pub_excl |= (cfg->flags & DISP_FLAG_EXCL) != 0;
266  else
267  sub_excl |= (cfg->flags & DISP_FLAG_EXCL) != 0;
268  } SMARTLIST_FOREACH_END(cfg);
269 
270  bool ok = true;
271 
272  if (! chan_same) {
273  log_warn(LD_MESG|LD_BUG,
274  "Message \"%s\" is associated with multiple inconsistent "
275  "channels.",
276  get_message_id_name(msg));
277  ok = false;
278  }
279  if (! type_same) {
280  log_warn(LD_MESG|LD_BUG,
281  "Message \"%s\" is associated with multiple inconsistent "
282  "message types.",
283  get_message_id_name(msg));
284  ok = false;
285  }
286 
287  /* Enforce exclusive-ness for publishers and subscribers that have asked for
288  * it.
289  */
290  if (pub_excl && smartlist_len_opt(pub) > 1) {
291  log_warn(LD_MESG|LD_BUG,
292  "Message \"%s\" has multiple publishers, but at least one is "
293  "marked as exclusive.",
294  get_message_id_name(msg));
295  ok = false;
296  }
297  if (sub_excl && smartlist_len_opt(sub) > 1) {
298  log_warn(LD_MESG|LD_BUG,
299  "Message \"%s\" has multiple subscribers, but at least one is "
300  "marked as exclusive.",
301  get_message_id_name(msg));
302  ok = false;
303  }
304 
305  smartlist_free(all);
306 
307  return ok ? 0 : -1;
308 }
309 
315 static int
316 lint_message(const pubsub_adjmap_t *map, message_id_t msg)
317 {
318  /* NOTE: Some of the checks in this function are maybe over-zealous, and we
319  * might not want to have them forever. I've marked them with [?] below.
320  */
321  if (BUG(msg >= map->n_msgs))
322  return 0; // LCOV_EXCL_LINE
323 
324  const smartlist_t *pub = map->pub_by_msg[msg];
325  const smartlist_t *sub = map->sub_by_msg[msg];
326 
327  const size_t n_pub = smartlist_len_opt(pub);
328  const size_t n_sub = smartlist_len_opt(sub);
329 
330  if (n_pub == 0 && n_sub == 0) {
331  log_info(LD_MESG, "Nobody is publishing or subscribing to message "
332  "\"%s\".",
333  get_message_id_name(msg));
334  return 0; // No publishers or subscribers: nothing to do.
335  }
336  /* We'll set this to false if there are any problems. */
337  bool ok = true;
338 
339  /* First make sure that if there are publishers, there are subscribers. */
340  if (n_pub == 0) {
341  log_warn(LD_MESG|LD_BUG,
342  "Message \"%s\" has subscribers, but no publishers.",
343  get_message_id_name(msg));
344  ok = false;
345  } else if (n_sub == 0) {
346  log_warn(LD_MESG|LD_BUG,
347  "Message \"%s\" has publishers, but no subscribers.",
348  get_message_id_name(msg));
349  ok = false;
350  }
351 
352  /* Check the message graph topology. */
353  if (lint_message_graph(map, msg, pub, sub) < 0)
354  ok = false;
355 
356  /* Check whether the messages have the same fields set on them. */
357  if (lint_message_consistency(msg, pub, sub) < 0)
358  ok = false;
359 
360  if (!ok) {
361  /* There was a problem -- let's log all the publishers and subscribers on
362  * this message */
363  if (pub) {
364  SMARTLIST_FOREACH(pub, pubsub_cfg_t *, cfg,
365  pubsub_cfg_dump(cfg, LOG_WARN, " "));
366  }
367  if (sub) {
368  SMARTLIST_FOREACH(sub, pubsub_cfg_t *, cfg,
369  pubsub_cfg_dump(cfg, LOG_WARN, " "));
370  }
371  }
372 
373  return ok ? 0 : -1;
374 }
375 
380 static int
381 pubsub_adjmap_check(const pubsub_adjmap_t *map)
382 {
383  bool all_ok = true;
384  for (unsigned i = 0; i < map->n_msgs; ++i) {
385  if (lint_message(map, i) < 0) {
386  all_ok = false;
387  }
388  }
389  return all_ok ? 0 : -1;
390 }
391 
396 int
398 {
399  pubsub_adjmap_t *map = pubsub_build_adjacency_map(builder->items);
400  int rv = -1;
401 
402  if (!map)
403  goto err; // should be impossible
404 
405  if (pubsub_adjmap_check(map) < 0)
406  goto err;
407 
408  rv = 0;
409  err:
410  pubsub_adjmap_free(map);
411  return rv;
412 }
static pubsub_adjmap_t * pubsub_build_adjacency_map(const pubsub_items_t *cfg)
Definition: pubsub_check.c:36
static int smartlist_len_opt(const smartlist_t *sl)
Definition: pubsub_check.c:127
Header for smartlist.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
unsigned int bitarray_t
Definition: bitarray.h:30
static int lint_message_graph(const pubsub_adjmap_t *map, message_id_t msg, const smartlist_t *pub, const smartlist_t *sub)
Definition: pubsub_check.c:202
struct pubsub_builder_t pubsub_builder_t
Definition: pubsub_build.h:28
struct pubsub_items_t pubsub_items_t
Definition: pubsub_build.h:35
#define DISP_FLAG_EXCL
Definition: pubsub_flags.h:20
static int lint_message_consistency(message_id_t msg, const smartlist_t *pub, const smartlist_t *sub)
Definition: pubsub_check.c:240
Implements a variable-sized (but non-resizeable) bit-array.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:633
void smartlist_add(smartlist_t *sl, void *element)
Flags that can be set on publish/subscribe messages.
static void pubsub_cfg_dump(const pubsub_cfg_t *cfg, int severity, const char *prefix)
Definition: pubsub_check.c:156
#define tor_free(p)
Definition: malloc.h:52
Headers for util_malloc.c.
static void bitarray_set(bitarray_t *b, int bit)
Definition: bitarray.h:68
static int pubsub_adjmap_check(const pubsub_adjmap_t *map)
Definition: pubsub_check.c:381
Types used for messages in the dispatcher code.
tor_assert(buffer)
int pubsub_builder_check(pubsub_builder_t *builder)
Definition: pubsub_check.c:397
#define LD_MESG
Definition: log.h:119
private structures used for configuring dispatchers and messages.
#define DISP_FLAG_STUB
Definition: pubsub_flags.h:30
#define LOG_WARN
Definition: log.h:51
static bitarray_t * bitarray_init_zero(unsigned int n_bits)
Definition: bitarray.h:33
#define pubsub_adjmap_free(m)
Definition: pubsub_check.c:89
static void pubsub_adjmap_free_helper(smartlist_t **lsts, size_t n)
Definition: pubsub_check.c:97
static const char * format_flags(unsigned flags)
Definition: pubsub_check.c:138
#define SMARTLIST_FOREACH(sl, type, var, cmd)
void smartlist_add_all(smartlist_t *s1, const smartlist_t *s2)
Header for compat_string.c.
static void get_message_bitarray(const pubsub_adjmap_t *map, const smartlist_t *items, bitarray_t **out)
Definition: pubsub_check.c:178
static void pubsub_adjmap_free_(pubsub_adjmap_t *map)
Definition: pubsub_check.c:112
Macros to manage assertions, fatal and non-fatal.
Header used for constructing the OO publish-subscribe facility.
static unsigned int bitarray_is_set(bitarray_t *b, int bit)
Definition: bitarray.h:81
#define LD_BUG
Definition: log.h:84
static int lint_message(const pubsub_adjmap_t *map, message_id_t msg)
Definition: pubsub_check.c:316
static void pubsub_adjmap_add(pubsub_adjmap_t *map, const pubsub_cfg_t *item)
Definition: pubsub_check.c:61