tests_pass_valgrind.c 6.4 KB

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