malloc_fail.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define __USE_GNU 1
  4. #include <dlfcn.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <sys/time.h>
  8. #include <unistd.h>
  9. #include <stdarg.h>
  10. /* Pointer to next malloc function */
  11. typedef void *(*mallocfunc)(size_t);
  12. static mallocfunc next_malloc = NULL;
  13. /* Pointer to next realloc function */
  14. typedef void *(*reallocfunc)(void *, size_t);
  15. static reallocfunc next_realloc = NULL;
  16. /* Pointer to next calloc function */
  17. typedef void *(*callocfunc)(size_t, size_t);
  18. static callocfunc next_calloc = NULL;
  19. /* Interval at which to fail */
  20. static int fail_interval = 0;
  21. /* Fail randomly or deterministically? */
  22. static int fail_random = 0;
  23. /* Counter tracking how many successfully allocations we've had since the last
  24. * injected failure */
  25. static int ntimes = 0;
  26. /* Break to debugger */
  27. static void break_to_gdb()
  28. {
  29. #ifdef MALLOC_INJECTOR_BREAK_TO_GDB
  30. int tmp;
  31. tmp = fail_interval;
  32. fail_interval = -1;
  33. char buffer[2048];
  34. int pid = getpid();
  35. snprintf(buffer, 2048, "xterm -e 'echo gdb /proc/%d/exe %d; sleep 60'", pid, pid);
  36. unsetenv("LD_PRELOAD");
  37. system(buffer);
  38. fail_interval = tmp;
  39. #endif
  40. }
  41. /* Grab our "randomness" setting from the environment */
  42. static int get_fail_random()
  43. {
  44. char *str_itvl = getenv("INJECT_MALLOC_FAIL_RANDOM");
  45. if (str_itvl == NULL)
  46. return 0;
  47. else if (! strcmp(str_itvl, "0"))
  48. return 0;
  49. else
  50. {
  51. srand(time(NULL));
  52. return 1;
  53. }
  54. }
  55. /* Grab our "interval" setting from the environment */
  56. static int get_fail_interval()
  57. {
  58. int value = 0;
  59. char *str_itvl = getenv("INJECT_MALLOC_FAIL_INTERVAL");
  60. if (str_itvl != NULL)
  61. value = atoi(str_itvl);
  62. if (value == 0)
  63. return -1;
  64. return value;
  65. }
  66. static void prepare()
  67. {
  68. if (next_malloc == NULL)
  69. {
  70. next_malloc = dlsym(RTLD_NEXT, "malloc");
  71. next_realloc = dlsym(RTLD_NEXT, "realloc");
  72. next_calloc = dlsym(RTLD_NEXT, "calloc");
  73. fail_interval = get_fail_interval();
  74. fail_random = get_fail_random();
  75. printf("Initialized malloc failure injector (%d, %d)\n", fail_interval, fail_random);
  76. }
  77. }
  78. static int time_to_fail()
  79. {
  80. if (fail_interval > 0)
  81. {
  82. if (fail_random)
  83. {
  84. if ((rand() % fail_interval) == 0)
  85. return 1;
  86. }
  87. else
  88. {
  89. if (++ ntimes == fail_interval)
  90. {
  91. ntimes = 0;
  92. return 1;
  93. }
  94. }
  95. }
  96. return 0;
  97. }
  98. /* Intercept malloc, injecting failures as appropriate */
  99. void *malloc(size_t size)
  100. {
  101. prepare();
  102. if (time_to_fail())
  103. {
  104. if (fail_random)
  105. printf("Injected random malloc failure on request for %u bytes\n", size);
  106. else
  107. printf("Injected malloc failure on request for %u bytes\n", size);
  108. break_to_gdb();
  109. return NULL;
  110. }
  111. return (*next_malloc)(size);
  112. }
  113. /* Intercept realloc, injecting failures as appropriate */
  114. void *realloc(void *old, size_t size)
  115. {
  116. prepare();
  117. if (time_to_fail())
  118. {
  119. if (fail_random)
  120. printf("Injected random realloc failure on request for %u bytes\n", size);
  121. else
  122. printf("Injected realloc failure on request for %u bytes\n", size);
  123. break_to_gdb();
  124. return NULL;
  125. }
  126. return (*next_realloc)(old, size);
  127. }
  128. /* Intercept calloc, injecting failures as appropriate */
  129. void *calloc(size_t nmemb, size_t size)
  130. {
  131. prepare();
  132. if (time_to_fail())
  133. {
  134. if (fail_random)
  135. printf("Injected random calloc failure on request for %u*%u=%u bytes\n", nmemb, size, nmemb*size);
  136. else
  137. printf("Injected calloc failure on request for %u*%u=%u bytes\n", nmemb, size, nmemb*size);
  138. break_to_gdb();
  139. return NULL;
  140. }
  141. return (*next_calloc)(nmemb, size);
  142. }