tests_pass_valgrind.c 6.5 KB

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