run_tests_valgrind.c 4.8 KB

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