tests_pass_valgrind.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. unsigned int *timeleft,
  132. struct score *score)
  133. {
  134. struct ccan_file *i;
  135. struct list_head *list;
  136. /* This is slow, so we run once but grab leak info. */
  137. score->total = 0;
  138. score->pass = true;
  139. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  140. list_for_each(list, i, list) {
  141. char *err, *output;
  142. const char *options;
  143. score->total++;
  144. options = concat(score,
  145. per_file_options(&tests_pass_valgrind,
  146. i));
  147. if (streq(options, "FAIL")) {
  148. i->leak_info = NULL;
  149. continue;
  150. }
  151. output = grab_file(i, i->valgrind_log, NULL);
  152. /* No valgrind errors? */
  153. if (!output || output[0] == '\0') {
  154. err = NULL;
  155. i->leak_info = NULL;
  156. } else {
  157. i->leak_info = analyze_output(output, &err);
  158. }
  159. if (err) {
  160. score_file_error(score, i, 0, "%s", err);
  161. score->pass = false;
  162. } else
  163. score->score++;
  164. }
  165. }
  166. }
  167. static void do_leakcheck_vg(struct manifest *m,
  168. unsigned int *timeleft,
  169. struct score *score)
  170. {
  171. struct ccan_file *i;
  172. struct list_head *list;
  173. char **options;
  174. bool leaks = false;
  175. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  176. list_for_each(list, i, list) {
  177. options = per_file_options(&tests_pass_valgrind_noleaks,
  178. i);
  179. if (options[0]) {
  180. if (streq(options[0], "FAIL")) {
  181. leaks = true;
  182. continue;
  183. }
  184. errx(1, "Unknown leakcheck options '%s'",
  185. options[0]);
  186. }
  187. if (i->leak_info) {
  188. score_file_error(score, i, 0, "%s",
  189. i->leak_info);
  190. leaks = true;
  191. }
  192. }
  193. }
  194. /* FIXME: We don't fail for this, since many tests leak. */
  195. score->pass = true;
  196. if (!leaks) {
  197. score->score = 1;
  198. }
  199. }
  200. /* Gcc's warn_unused_result is fascist bullshit. */
  201. #define doesnt_matter()
  202. static void run_under_debugger_vg(struct manifest *m, struct score *score)
  203. {
  204. struct file_error *first;
  205. char *command;
  206. /* Don't ask anything if they suppressed tests. */
  207. if (score->pass)
  208. return;
  209. if (!ask("Should I run the first failing test under the debugger?"))
  210. return;
  211. first = list_top(&score->per_file_errors, struct file_error, list);
  212. command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
  213. concat(score,
  214. per_file_options(&tests_pass_valgrind,
  215. first->file)),
  216. first->file->compiled[COMPILE_NORMAL]);
  217. if (system(command))
  218. doesnt_matter();
  219. }
  220. struct ccanlint tests_pass_valgrind = {
  221. .key = "tests_pass_valgrind",
  222. .name = "Module's run and api tests succeed under valgrind",
  223. .can_run = can_run_vg,
  224. .check = do_run_tests_vg,
  225. .handle = run_under_debugger_vg,
  226. .takes_options = true,
  227. .needs = "tests_pass"
  228. };
  229. struct ccanlint tests_pass_valgrind_noleaks = {
  230. .key = "tests_pass_valgrind_noleaks",
  231. .name = "Module's run and api tests have no memory leaks",
  232. .check = do_leakcheck_vg,
  233. .takes_options = true,
  234. .needs = "tests_pass_valgrind"
  235. };