tests_pass_valgrind.c 6.6 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. /* Note: we already test safe_mode in run_tests.c */
  21. static const char *can_run_vg(struct manifest *m)
  22. {
  23. if (!do_valgrind)
  24. return talloc_asprintf(m, "No valgrind support");
  25. return NULL;
  26. }
  27. static void do_leakcheck_vg(struct manifest *m,
  28. unsigned int *timeleft,
  29. struct score *score);
  30. static struct ccanlint tests_pass_valgrind_noleaks = {
  31. .key = "tests_pass_valgrind_noleaks",
  32. .name = "Module's run and api tests have no memory leaks",
  33. .check = do_leakcheck_vg,
  34. .takes_options = true,
  35. .needs = "tests_pass_valgrind"
  36. };
  37. REGISTER_TEST(tests_pass_valgrind_noleaks);
  38. /* Example output:
  39. ==2749== Conditional jump or move depends on uninitialised value(s)
  40. ==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
  41. ==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
  42. ==2749== by 0x408EACF: printf (printf.c:35)
  43. ==2749== by 0x8048465: main (in /tmp/foo)
  44. ==2749==
  45. ==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
  46. ==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
  47. ==2749== by 0x8048444: main (in /tmp/foo)
  48. ==2749==
  49. */
  50. static bool blank_line(const char *line)
  51. {
  52. return line[strspn(line, "=0123456789 ")] == '\0';
  53. }
  54. /* Removes matching lines from lines array, returns them. FIXME: Hacky. */
  55. static char **extract_matching(const char *prefix, char *lines[])
  56. {
  57. unsigned int i, num_ret = 0;
  58. char **ret = talloc_array(lines, char *, talloc_array_length(lines));
  59. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  60. if (strstarts(lines[i], prefix)) {
  61. ret[num_ret++] = talloc_steal(ret, lines[i]);
  62. lines[i] = (char *)"";
  63. }
  64. }
  65. ret[num_ret++] = NULL;
  66. /* Make sure length is correct! */
  67. return talloc_realloc(NULL, ret, char *, num_ret);
  68. }
  69. static char *get_leaks(char *lines[], char **errs)
  70. {
  71. char *leaks = talloc_strdup(lines, "");
  72. unsigned int i;
  73. for (i = 0; i < talloc_array_length(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. leaks = talloc_append_string(leaks,
  80. lines[i]);
  81. leaks = talloc_append_string(leaks,
  82. "\n");
  83. i++;
  84. }
  85. } else
  86. /* Not definite, ignore. */
  87. while (lines[i] && !blank_line(lines[i]))
  88. i++;
  89. } else {
  90. /* A real error. */
  91. while (lines[i] && !blank_line(lines[i])) {
  92. *errs = talloc_append_string(*errs, lines[i]);
  93. *errs = talloc_append_string(*errs, "\n");
  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 = talloc_strdup(output, "");
  104. unsigned int i;
  105. char **lines = strsplit(output, output, "\n");
  106. *errs = talloc_strdup(output, "");
  107. for (i = 0; i < talloc_array_length(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 = talloc_strndup(output, lines[i], preflen);
  114. sublines = extract_matching(prefix, lines);
  115. leaks = talloc_append_string(leaks, get_leaks(sublines, errs));
  116. }
  117. if (!leaks[0]) {
  118. talloc_free(leaks);
  119. leaks = NULL;
  120. }
  121. if (!(*errs)[0]) {
  122. talloc_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 = talloc_strdup(score, "");
  131. for (i = 0; bits[i]; i++) {
  132. if (i)
  133. ret = talloc_append_string(ret, " ");
  134. ret = talloc_append_string(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 = 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 = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
  222. concat(score,
  223. 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);