tests_pass_valgrind.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. static char *get_leaks(const char *output, char **errs)
  47. {
  48. char *leaks = talloc_strdup(output, "");
  49. unsigned int i;
  50. char **lines = strsplit(output, output, "\n");
  51. *errs = talloc_strdup(output, "");
  52. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  53. if (strstr(lines[i], " lost ")) {
  54. /* A leak... */
  55. if (strstr(lines[i], " definitely lost ")) {
  56. /* Definite leak, report. */
  57. while (lines[i] && !blank_line(lines[i])) {
  58. leaks = talloc_append_string(leaks,
  59. lines[i]);
  60. leaks = talloc_append_string(leaks,
  61. "\n");
  62. i++;
  63. }
  64. } else
  65. /* Not definite, ignore. */
  66. while (lines[i] && !blank_line(lines[i]))
  67. i++;
  68. } else {
  69. /* A real error. */
  70. while (lines[i] && !blank_line(lines[i])) {
  71. *errs = talloc_append_string(*errs, lines[i]);
  72. *errs = talloc_append_string(*errs, "\n");
  73. i++;
  74. }
  75. }
  76. }
  77. if (!leaks[0]) {
  78. talloc_free(leaks);
  79. leaks = NULL;
  80. }
  81. if (!(*errs)[0]) {
  82. talloc_free(*errs);
  83. *errs = NULL;
  84. }
  85. return leaks;
  86. }
  87. /* FIXME: Run examples, too! */
  88. static void do_run_tests_vg(struct manifest *m,
  89. bool keep,
  90. unsigned int *timeleft,
  91. struct score *score)
  92. {
  93. struct ccan_file *i;
  94. struct list_head *list;
  95. char *cmdout;
  96. /* This is slow, so we run once but grab leak info. */
  97. score->total = 0;
  98. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  99. list_for_each(list, i, list) {
  100. char *output, *err;
  101. score->total++;
  102. /* FIXME: Valgrind's output sucks. XML is unreadable by
  103. * humans, and you can't have both. */
  104. run_command(score, timeleft, &cmdout,
  105. "valgrind -q --error-exitcode=101"
  106. " --child-silent-after-fork=yes"
  107. " --leak-check=full"
  108. " --log-fd=3 %s %s"
  109. " 3> valgrind.log",
  110. tests_pass_valgrind.options ?
  111. tests_pass_valgrind.options : "",
  112. i->compiled);
  113. output = grab_file(i, "valgrind.log", NULL);
  114. if (!output || output[0] == '\0') {
  115. err = NULL;
  116. i->leak_info = NULL;
  117. } else {
  118. i->leak_info = get_leaks(output, &err);
  119. }
  120. if (err)
  121. score_file_error(score, i, 0, "%s", err);
  122. else
  123. score->score++;
  124. }
  125. }
  126. if (score->score == score->total)
  127. score->pass = true;
  128. }
  129. static void do_leakcheck_vg(struct manifest *m,
  130. bool keep,
  131. unsigned int *timeleft,
  132. struct score *score)
  133. {
  134. struct ccan_file *i;
  135. struct list_head *list;
  136. bool leaks = false;
  137. foreach_ptr(list, &m->run_tests, &m->api_tests) {
  138. list_for_each(list, i, list) {
  139. if (i->leak_info) {
  140. score_file_error(score, i, 0, "%s",
  141. i->leak_info);
  142. leaks = true;
  143. }
  144. }
  145. }
  146. /* FIXME: We don't fail for this, since many tests leak. */
  147. score->pass = true;
  148. if (!leaks) {
  149. score->score = 1;
  150. }
  151. }
  152. /* Gcc's warn_unused_result is fascist bullshit. */
  153. #define doesnt_matter()
  154. static void run_under_debugger_vg(struct manifest *m, struct score *score)
  155. {
  156. struct file_error *first;
  157. char *command;
  158. if (!ask("Should I run the first failing test under the debugger?"))
  159. return;
  160. first = list_top(&score->per_file_errors, struct file_error, list);
  161. command = talloc_asprintf(m, "valgrind --db-attach=yes%s %s",
  162. tests_pass_valgrind.options ?
  163. tests_pass_valgrind.options : "",
  164. first->file->compiled);
  165. if (system(command))
  166. doesnt_matter();
  167. }
  168. struct ccanlint tests_pass_valgrind = {
  169. .key = "tests_pass_valgrind",
  170. .name = "Module's run and api tests succeed under valgrind",
  171. .can_run = can_run_vg,
  172. .check = do_run_tests_vg,
  173. .handle = run_under_debugger_vg,
  174. .takes_options = true,
  175. .needs = "tests_pass"
  176. };
  177. struct ccanlint tests_pass_valgrind_noleaks = {
  178. .key = "tests_pass_valgrind_noleaks",
  179. .name = "Module's run and api tests have no memory leaks",
  180. .check = do_leakcheck_vg,
  181. .needs = "tests_pass_valgrind"
  182. };
  183. REGISTER_TEST(tests_pass_valgrind_noleaks);