tests_pass_valgrind.c 6.5 KB

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