run-malloc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <setjmp.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <assert.h>
  7. #include <ccan/tap/tap.h>
  8. /* We don't actually want it to exit... */
  9. static jmp_buf exited;
  10. #define exit(status) longjmp(exited, (status) + 1)
  11. #define printf saved_printf
  12. static int saved_printf(const char *fmt, ...);
  13. #define fprintf saved_fprintf
  14. static int saved_fprintf(FILE *ignored, const char *fmt, ...);
  15. #define vfprintf saved_vfprintf
  16. static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap);
  17. /* Hack to avoid a memory leak which valgrind complains about. */
  18. #define realloc set_realloc
  19. static void *set_realloc(void *ptr, size_t size);
  20. #define free set_free
  21. static void set_free(void *ptr);
  22. /* Include the C files directly. */
  23. #include <ccan/failtest/failtest.c>
  24. #undef realloc
  25. #undef free
  26. static char *buffer;
  27. static void *set_realloc(void *ptr, size_t size)
  28. {
  29. return buffer = realloc(ptr, size);
  30. }
  31. static void set_free(void *ptr)
  32. {
  33. if (ptr == buffer)
  34. buffer = NULL;
  35. free(ptr);
  36. }
  37. static char *output = NULL;
  38. static int saved_vprintf(const char *fmt, va_list ap)
  39. {
  40. int ret;
  41. int len = 0;
  42. va_list ap2;
  43. va_copy(ap2, ap);
  44. ret = vsnprintf(NULL, 0, fmt, ap2);
  45. va_end(ap2);
  46. if (output)
  47. len = strlen(output);
  48. output = realloc(output, len + ret + 1);
  49. return vsprintf(output + len, fmt, ap);
  50. }
  51. static int saved_vfprintf(FILE *ignored, const char *fmt, va_list ap)
  52. {
  53. return saved_vprintf(fmt, ap);
  54. }
  55. static int saved_printf(const char *fmt, ...)
  56. {
  57. va_list ap;
  58. int ret;
  59. va_start(ap, fmt);
  60. ret = saved_vprintf(fmt, ap);
  61. va_end(ap);
  62. return ret;
  63. }
  64. static int saved_fprintf(FILE *ignored, const char *fmt, ...)
  65. {
  66. va_list ap;
  67. int ret;
  68. va_start(ap, fmt);
  69. ret = saved_vprintf(fmt, ap);
  70. va_end(ap);
  71. return ret;
  72. }
  73. int main(void)
  74. {
  75. int status;
  76. plan_tests(3);
  77. failtest_init(0, NULL);
  78. status = setjmp(exited);
  79. if (status == 0) {
  80. char *p = failtest_malloc(1, "run-malloc.c", 1);
  81. /* If we just segv, valgrind counts that as a failure.
  82. * So kill ourselves creatively. */
  83. if (!p)
  84. kill(getpid(), SIGSEGV);
  85. fail("Expected child to crash!");
  86. } else {
  87. ok1(status == 2);
  88. ok1(strstr(output, "Killed by signal"));
  89. ok1(strstr(output, "--failpath=M\n"));
  90. }
  91. free(buffer);
  92. return exit_status();
  93. }