pubsub_check.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /**
  7. * @file pubsub_check.c
  8. * @brief Enforce various requirements on a pubsub_builder.
  9. **/
  10. #define PUBSUB_PRIVATE
  11. #include "lib/dispatch/dispatch_naming.h"
  12. #include "lib/dispatch/msgtypes.h"
  13. #include "lib/pubsub/pubsub_flags.h"
  14. #include "lib/pubsub/pubsub_builder_st.h"
  15. #include "lib/pubsub/pubsub_build.h"
  16. #include "lib/container/bitarray.h"
  17. #include "lib/container/smartlist.h"
  18. #include "lib/log/util_bug.h"
  19. #include "lib/malloc/malloc.h"
  20. #include "lib/string/compat_string.h"
  21. #include <string.h>
  22. static void pubsub_adjmap_add(pubsub_adjmap_t *map,
  23. const pubsub_cfg_t *item);
  24. /**
  25. * Helper: contruct and return a new pubsub_adjacency_map from <b>cfg</b>.
  26. * Return NULL on error.
  27. **/
  28. static pubsub_adjmap_t *
  29. pubsub_build_adjacency_map(const pubsub_items_t *cfg)
  30. {
  31. pubsub_adjmap_t *map = tor_malloc_zero(sizeof(*map));
  32. const size_t n_subsystems = get_num_subsys_ids();
  33. const size_t n_msgs = get_num_message_ids();
  34. map->n_subsystems = n_subsystems;
  35. map->n_msgs = n_msgs;
  36. map->pub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
  37. map->sub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
  38. map->pub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
  39. map->sub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
  40. SMARTLIST_FOREACH_BEGIN(cfg->items, const pubsub_cfg_t *, item) {
  41. pubsub_adjmap_add(map, item);
  42. } SMARTLIST_FOREACH_END(item);
  43. return map;
  44. }
  45. /**
  46. * Helper: add a single pubsub_cfg_t to an adjacency map.
  47. **/
  48. static void
  49. pubsub_adjmap_add(pubsub_adjmap_t *map,
  50. const pubsub_cfg_t *item)
  51. {
  52. smartlist_t **by_subsys;
  53. smartlist_t **by_msg;
  54. tor_assert(item->subsys < map->n_subsystems);
  55. tor_assert(item->msg < map->n_msgs);
  56. if (item->is_publish) {
  57. by_subsys = &map->pub_by_subsys[item->subsys];
  58. by_msg = &map->pub_by_msg[item->msg];
  59. } else {
  60. by_subsys = &map->sub_by_subsys[item->subsys];
  61. by_msg = &map->sub_by_msg[item->msg];
  62. }
  63. if (! *by_subsys)
  64. *by_subsys = smartlist_new();
  65. if (! *by_msg)
  66. *by_msg = smartlist_new();
  67. smartlist_add(*by_subsys, (void*) item);
  68. smartlist_add(*by_msg, (void *) item);
  69. }
  70. /**
  71. * Release all storage held by m and set m to NULL.
  72. **/
  73. #define pubsub_adjmap_free(m) \
  74. FREE_AND_NULL(pubsub_adjmap_t, pubsub_adjmap_free_, m)
  75. /**
  76. * Free every element of an <b>n</b>-element array of smartlists, then
  77. * free the array itself.
  78. **/
  79. static void
  80. pubsub_adjmap_free_helper(smartlist_t **lsts, size_t n)
  81. {
  82. if (!lsts)
  83. return;
  84. for (unsigned i = 0; i < n; ++i) {
  85. smartlist_free(lsts[i]);
  86. }
  87. tor_free(lsts);
  88. }
  89. /**
  90. * Release all storage held by <b>map</b>.
  91. **/
  92. static void
  93. pubsub_adjmap_free_(pubsub_adjmap_t *map)
  94. {
  95. if (!map)
  96. return;
  97. pubsub_adjmap_free_helper(map->pub_by_subsys, map->n_subsystems);
  98. pubsub_adjmap_free_helper(map->sub_by_subsys, map->n_subsystems);
  99. pubsub_adjmap_free_helper(map->pub_by_msg, map->n_msgs);
  100. pubsub_adjmap_free_helper(map->sub_by_msg, map->n_msgs);
  101. tor_free(map);
  102. }
  103. /**
  104. * Helper: return the length of <b>sl</b>, or 0 if sl is NULL.
  105. **/
  106. static int
  107. smartlist_len_opt(const smartlist_t *sl)
  108. {
  109. if (sl)
  110. return smartlist_len(sl);
  111. else
  112. return 0;
  113. }
  114. /** Return a pointer to a statically allocated string encoding the
  115. * dispatcher flags in <b>flags</b>. */
  116. static const char *
  117. format_flags(unsigned flags)
  118. {
  119. static char buf[32];
  120. buf[0] = 0;
  121. if (flags & DISP_FLAG_EXCL) {
  122. strlcat(buf, " EXCL", sizeof(buf));
  123. }
  124. if (flags & DISP_FLAG_STUB) {
  125. strlcat(buf, " STUB", sizeof(buf));
  126. }
  127. return buf[0] ? buf+1 : buf;
  128. }
  129. /**
  130. * Log a message containing a description of <b>cfg</b> at severity, prefixed
  131. * by the string <b>prefix</b>.
  132. */
  133. static void
  134. pubsub_cfg_dump(const pubsub_cfg_t *cfg, int severity, const char *prefix)
  135. {
  136. tor_assert(prefix);
  137. tor_log(severity, LD_MESG,
  138. "%s%s %s: %s{%s} on %s (%s) <%u %u %u %u %x> [%s:%d]",
  139. prefix,
  140. get_subsys_id_name(cfg->subsys),
  141. cfg->is_publish ? "PUB" : "SUB",
  142. get_message_id_name(cfg->msg),
  143. get_msg_type_id_name(cfg->type),
  144. get_channel_id_name(cfg->channel),
  145. format_flags(cfg->flags),
  146. cfg->subsys, cfg->msg, cfg->type, cfg->channel, cfg->flags,
  147. cfg->added_by_file, cfg->added_by_line);
  148. }
  149. /**
  150. * Helper: fill a bitarray <b>out</b> with entries corresponding to the
  151. * subsystems listed in <b>items</b>.
  152. **/
  153. static void
  154. get_message_bitarray(const pubsub_adjmap_t *map,
  155. const smartlist_t *items,
  156. bitarray_t **out)
  157. {
  158. *out = bitarray_init_zero((unsigned)map->n_subsystems);
  159. if (! items)
  160. return;
  161. SMARTLIST_FOREACH_BEGIN(items, const pubsub_cfg_t *, cfg) {
  162. bitarray_set(*out, cfg->subsys);
  163. } SMARTLIST_FOREACH_END(cfg);
  164. }
  165. /**
  166. * Helper for lint_message: check that all the pubsub_cfg_t items in the two
  167. * respective smartlists obey our local graph topology rules.
  168. *
  169. * (Right now this is just a matter of "each subsystem only
  170. * publishes/subscribes once; no subsystem is a publisher and subscriber for
  171. * the same message.")
  172. *
  173. * Return 0 on success, -1 on failure.
  174. **/
  175. static int
  176. lint_message_graph(const pubsub_adjmap_t *map,
  177. message_id_t msg,
  178. const smartlist_t *pub,
  179. const smartlist_t *sub)
  180. {
  181. bitarray_t *published_by = NULL;
  182. bitarray_t *subscribed_by = NULL;
  183. bool ok = true;
  184. get_message_bitarray(map, pub, &published_by);
  185. get_message_bitarray(map, sub, &subscribed_by);
  186. /* Check whether any subsystem is publishing and subscribing the same
  187. * message. [??]
  188. */
  189. for (unsigned i = 0; i < map->n_subsystems; ++i) {
  190. if (bitarray_is_set(published_by, i) &&
  191. bitarray_is_set(subscribed_by, i)) {
  192. log_warn(LD_MESG|LD_BUG,
  193. "Message \"%s\" is published and subscribed by the same "
  194. "subsystem \"%s\".",
  195. get_message_id_name(msg),
  196. get_subsys_id_name(i));
  197. ok = false;
  198. }
  199. }
  200. bitarray_free(published_by);
  201. bitarray_free(subscribed_by);
  202. return ok ? 0 : -1;
  203. }
  204. /**
  205. * Helper for lint_message: check that all the pubsub_cfg_t items in the two
  206. * respective smartlists have compatible flags, channels, and types.
  207. **/
  208. static int
  209. lint_message_consistency(message_id_t msg,
  210. const smartlist_t *pub,
  211. const smartlist_t *sub)
  212. {
  213. if (!smartlist_len_opt(pub) && !smartlist_len_opt(sub))
  214. return 0; // LCOV_EXCL_LINE -- this was already checked.
  215. /* The 'all' list has the publishers and the subscribers. */
  216. smartlist_t *all = smartlist_new();
  217. if (pub)
  218. smartlist_add_all(all, pub);
  219. if (sub)
  220. smartlist_add_all(all, sub);
  221. const pubsub_cfg_t *item0 = smartlist_get(all, 0);
  222. /* Indicates which subsystems we've found publishing/subscribing here. */
  223. bool pub_excl = false, sub_excl = false, chan_same = true, type_same = true;
  224. /* Simple message consistency properties across messages.
  225. */
  226. SMARTLIST_FOREACH_BEGIN(all, const pubsub_cfg_t *, cfg) {
  227. chan_same &= (cfg->channel == item0->channel);
  228. type_same &= (cfg->type == item0->type);
  229. if (cfg->is_publish)
  230. pub_excl |= (cfg->flags & DISP_FLAG_EXCL) != 0;
  231. else
  232. sub_excl |= (cfg->flags & DISP_FLAG_EXCL) != 0;
  233. } SMARTLIST_FOREACH_END(cfg);
  234. bool ok = true;
  235. if (! chan_same) {
  236. log_warn(LD_MESG|LD_BUG,
  237. "Message \"%s\" is associated with multiple inconsistent "
  238. "channels.",
  239. get_message_id_name(msg));
  240. ok = false;
  241. }
  242. if (! type_same) {
  243. log_warn(LD_MESG|LD_BUG,
  244. "Message \"%s\" is associated with multiple inconsistent "
  245. "message types.",
  246. get_message_id_name(msg));
  247. ok = false;
  248. }
  249. /* Enforce exclusive-ness for publishers and subscribers that have asked for
  250. * it.
  251. */
  252. if (pub_excl && smartlist_len_opt(pub) > 1) {
  253. log_warn(LD_MESG|LD_BUG,
  254. "Message \"%s\" has multiple publishers, but at least one is "
  255. "marked as exclusive.",
  256. get_message_id_name(msg));
  257. ok = false;
  258. }
  259. if (sub_excl && smartlist_len_opt(sub) > 1) {
  260. log_warn(LD_MESG|LD_BUG,
  261. "Message \"%s\" has multiple subscribers, but at least one is "
  262. "marked as exclusive.",
  263. get_message_id_name(msg));
  264. ok = false;
  265. }
  266. smartlist_free(all);
  267. return ok ? 0 : -1;
  268. }
  269. /**
  270. * Check whether there are any errors or inconsistencies for the message
  271. * described by <b>msg</b> in <b>map</b>. If there are problems, log about
  272. * them, and return -1. Otherwise return 0.
  273. **/
  274. static int
  275. lint_message(const pubsub_adjmap_t *map, message_id_t msg)
  276. {
  277. /* NOTE: Some of the checks in this function are maybe over-zealous, and we
  278. * might not want to have them forever. I've marked them with [?] below.
  279. */
  280. if (BUG(msg >= map->n_msgs))
  281. return 0; // LCOV_EXCL_LINE
  282. const smartlist_t *pub = map->pub_by_msg[msg];
  283. const smartlist_t *sub = map->sub_by_msg[msg];
  284. const size_t n_pub = smartlist_len_opt(pub);
  285. const size_t n_sub = smartlist_len_opt(sub);
  286. if (n_pub == 0 && n_sub == 0) {
  287. log_info(LD_MESG, "Nobody is publishing or subscribing to message "
  288. "\"%s\".",
  289. get_message_id_name(msg));
  290. return 0; // No publishers or subscribers: nothing to do.
  291. }
  292. /* We'll set this to false if there are any problems. */
  293. bool ok = true;
  294. /* First make sure that if there are publishers, there are subscribers. */
  295. if (n_pub == 0) {
  296. log_warn(LD_MESG|LD_BUG,
  297. "Message \"%s\" has subscribers, but no publishers.",
  298. get_message_id_name(msg));
  299. ok = false;
  300. } else if (n_sub == 0) {
  301. log_warn(LD_MESG|LD_BUG,
  302. "Message \"%s\" has publishers, but no subscribers.",
  303. get_message_id_name(msg));
  304. ok = false;
  305. }
  306. /* Check the message graph topology. */
  307. if (lint_message_graph(map, msg, pub, sub) < 0)
  308. ok = false;
  309. /* Check whether the messages have the same fields set on them. */
  310. if (lint_message_consistency(msg, pub, sub) < 0)
  311. ok = false;
  312. if (!ok) {
  313. /* There was a problem -- let's log all the publishers and subscribers on
  314. * this message */
  315. if (pub) {
  316. SMARTLIST_FOREACH(pub, pubsub_cfg_t *, cfg,
  317. pubsub_cfg_dump(cfg, LOG_WARN, " "));
  318. }
  319. if (sub) {
  320. SMARTLIST_FOREACH(sub, pubsub_cfg_t *, cfg,
  321. pubsub_cfg_dump(cfg, LOG_WARN, " "));
  322. }
  323. }
  324. return ok ? 0 : -1;
  325. }
  326. /**
  327. * Check all the messages in <b>map</b> for consistency. Return 0 on success,
  328. * -1 on problems.
  329. **/
  330. static int
  331. pubsub_adjmap_check(const pubsub_adjmap_t *map)
  332. {
  333. bool all_ok = true;
  334. for (unsigned i = 0; i < map->n_msgs; ++i) {
  335. if (lint_message(map, i) < 0) {
  336. all_ok = false;
  337. }
  338. }
  339. return all_ok ? 0 : -1;
  340. }
  341. /**
  342. * Check builder for consistency and various constraints. Return 0 on success,
  343. * -1 on failure.
  344. **/
  345. int
  346. pubsub_builder_check(pubsub_builder_t *builder)
  347. {
  348. pubsub_adjmap_t *map = pubsub_build_adjacency_map(builder->items);
  349. int rv = -1;
  350. if (!map)
  351. goto err; // should be impossible
  352. if (pubsub_adjmap_check(map) < 0)
  353. goto err;
  354. rv = 0;
  355. err:
  356. pubsub_adjmap_free(map);
  357. return rv;
  358. }