tests_pass_valgrind.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/str/str.h>
  4. #include <ccan/take/take.h>
  5. #include <ccan/foreach/foreach.h>
  6. #include "tests_pass.h"
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <err.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. /* Note: we already test safe_mode in run_tests.c */
  19. static const char *can_run_vg(struct manifest *m)
  20. {
  21. if (!do_valgrind)
  22. return tal_fmt(m, "No valgrind support");
  23. return NULL;
  24. }
  25. static void do_leakcheck_vg(struct manifest *m,
  26. unsigned int *timeleft,
  27. struct score *score);
  28. static struct ccanlint tests_pass_valgrind_noleaks = {
  29. .key = "tests_pass_valgrind_noleaks",
  30. .name = "Module's run and api tests have no memory leaks",
  31. .check = do_leakcheck_vg,
  32. .takes_options = true,
  33. .needs = "tests_pass_valgrind"
  34. };
  35. REGISTER_TEST(tests_pass_valgrind_noleaks);
  36. /* Example output:
  37. ==2749== Conditional jump or move depends on uninitialised value(s)
  38. ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
  39. ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
  40. ==2749== by 0x408EACF: printf (printf.c:35)
  41. ==2749== by 0x8048465: main (in /tmp/foo)
  42. ==2749==
  43. ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
  44. ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
  45. ==2749== by 0x8048444: main (in /tmp/foo)
  46. ==2749==
  47. */
  48. static bool blank_line(const char *line)
  49. {
  50. return line[strspn(line, "=0123456789 ")] == '\0';
  51. }
  52. /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
  53. static char **extract_matching(const char *prefix, char *lines[])
  54. {
  55. unsigned int i, num_ret = 0;
  56. char **ret = tal_arr(lines, char *, tal_count(lines));
  57. for (i = 0; i < tal_count(lines) - 1; i++) {
  58. if (strstarts(lines[i], prefix)) {
  59. ret[num_ret++] = tal_strdup(ret, lines[i]);
  60. lines[i] = (char *)"";
  61. }
  62. }
  63. ret[num_ret++] = NULL;
  64. /* Make sure tal_count is correct! */
  65. tal_resize(&ret, num_ret);
  66. return ret;
  67. }
  68. static char *get_leaks(char *lines[], char **errs)
  69. {
  70. char *leaks = tal_strdup(lines, "");
  71. unsigned int i;
  72. for (i = 0; i < tal_count(lines) - 1; i++) {
  73. if (strstr(lines[i], " lost ")) {
  74. /* A leak... */
  75. if (strstr(lines[i], " definitely lost ")) {
  76. /* Definite leak, report. */
  77. while (lines[i] && !blank_line(lines[i])) {
  78. tal_append_fmt(&leaks, "%s\n",
  79. lines[i]);
  80. i++;
  81. }
  82. } else
  83. /* Not definite, ignore. */
  84. while (lines[i] && !blank_line(lines[i]))
  85. i++;
  86. } else {
  87. /* A real error. */
  88. while (lines[i] && !blank_line(lines[i])) {
  89. if (!*errs)
  90. *errs = tal_fmt(NULL, "%s\n", lines[i]);
  91. else
  92. tal_append_fmt(errs, "%s\n", lines[i]);
  93. i++;
  94. }
  95. }
  96. }
  97. return leaks;
  98. }
  99. /* Returns leaks, and sets errs[] */
  100. static char *analyze_output(const char *output, char **errs)
  101. {
  102. char *leaks = tal_strdup(output, "");
  103. unsigned int i;
  104. char **lines = tal_strsplit(output, output, "\n", STR_EMPTY_OK);
  105. *errs = tal_strdup(output, "");
  106. for (i = 0; i < tal_count(lines) - 1; i++) {
  107. unsigned int preflen = strspn(lines[i], "=0123456789");
  108. char *prefix, **sublines;
  109. /* Ignore erased lines, or weird stuff. */
  110. if (preflen == 0)
  111. continue;
  112. prefix = tal_strndup(output, lines[i], preflen);
  113. sublines = extract_matching(prefix, lines);
  114. leaks = tal_strcat(output, take(leaks),
  115. take(get_leaks(sublines, errs)));
  116. }
  117. if (!leaks[0]) {
  118. tal_free(leaks);
  119. leaks = NULL;
  120. }
  121. if (!(*errs)[0]) {
  122. tal_free(*errs);
  123. *errs = NULL;
  124. }
  125. return leaks;
  126. }
  127. static const char *concat(struct score *score, char *bits[])
  128. {
  129. unsigned int i;
  130. char *ret = tal_strdup(score, "");
  131. for (i = 0; bits[i]; i++) {
  132. if (i)
  133. ret = tal_strcat(score, take(ret), " ");
  134. ret = tal_strcat(score, take(ret), bits[i]);
  135. }
  136. return ret;
  137. }
  138. /* FIXME: Run examples, too! */
  139. static void do_run_tests_vg(struct manifest *m,
  140. unsigned int *timeleft,
  141. struct score *score)
  142. {
  143. struct ccan_file *i;
  144. struct list_head *list;
  145. /* This is slow, so we run once but grab leak info. */
  146. score->total = 0;
  147. score->pass = true;
  148. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  149. list_for_each(list, i, list) {
  150. char *err, *output;
  151. const char *options;
  152. score->total++;
  153. options = concat(score,
  154. per_file_options(&tests_pass_valgrind,
  155. i));
  156. if (streq(options, "FAIL")) {
  157. i->leak_info = NULL;
  158. continue;
  159. }
  160. output = tal_grab_file(i, i->valgrind_log, NULL);
  161. /* No valgrind errors? */
  162. if (!output || output[0] == '\0') {
  163. err = NULL;
  164. i->leak_info = NULL;
  165. } else {
  166. i->leak_info = analyze_output(output, &err);
  167. }
  168. if (err) {
  169. score_file_error(score, i, 0, "%s", err);
  170. score->pass = false;
  171. } else
  172. score->score++;
  173. }
  174. }
  175. }
  176. static void do_leakcheck_vg(struct manifest *m,
  177. unsigned int *timeleft,
  178. struct score *score)
  179. {
  180. struct ccan_file *i;
  181. struct list_head *list;
  182. char **options;
  183. bool leaks = false;
  184. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  185. list_for_each(list, i, list) {
  186. options = per_file_options(&tests_pass_valgrind_noleaks,
  187. i);
  188. if (options[0]) {
  189. if (streq(options[0], "FAIL")) {
  190. leaks = true;
  191. continue;
  192. }
  193. errx(1, "Unknown leakcheck options '%s'",
  194. options[0]);
  195. }
  196. if (i->leak_info) {
  197. score_file_error(score, i, 0, "%s",
  198. i->leak_info);
  199. leaks = true;
  200. }
  201. }
  202. }
  203. /* FIXME: We don't fail for this, since many tests leak. */
  204. score->pass = true;
  205. if (!leaks) {
  206. score->score = 1;
  207. }
  208. }
  209. /* Gcc's warn_unused_result is fascist bullshit. */
  210. #define doesnt_matter()
  211. static void run_under_debugger_vg(struct manifest *m, struct score *score)
  212. {
  213. struct file_error *first;
  214. char *command;
  215. /* Don't ask anything if they suppressed tests. */
  216. if (score->pass)
  217. return;
  218. if (!ask("Should I run the first failing test under the debugger?"))
  219. return;
  220. first = list_top(&score->per_file_errors, struct file_error, list);
  221. command = tal_fmt(m, "valgrind --leak-check=full --db-attach=yes%s %s",
  222. concat(score, per_file_options(&tests_pass_valgrind,
  223. first->file)),
  224. first->file->compiled[COMPILE_NORMAL]);
  225. if (system(command))
  226. doesnt_matter();
  227. }
  228. struct ccanlint tests_pass_valgrind = {
  229. .key = "tests_pass_valgrind",
  230. .name = "Module's run and api tests succeed under valgrind",
  231. .can_run = can_run_vg,
  232. .check = do_run_tests_vg,
  233. .handle = run_under_debugger_vg,
  234. .takes_options = true,
  235. .needs = "tests_pass"
  236. };
  237. REGISTER_TEST(tests_pass_valgrind);