tests_pass_valgrind.c 7.2 KB

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