You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/base.h>
  15. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  16. #define OPENSSL_GLIBC
  17. #endif
  18. // This file isn't built on ARM or Aarch64 because we link statically in those
  19. // builds and trying to override malloc in a static link doesn't work. It also
  20. // requires glibc. It's also disabled on ASan builds as this interferes with
  21. // ASan's malloc interceptor.
  22. //
  23. // TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
  24. // coexist.
  25. #if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
  26. !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN) && \
  27. !defined(OPENSSL_MSAN) && !defined(OPENSSL_TSAN)
  28. #include <errno.h>
  29. #include <signal.h>
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <new>
  35. // This file defines overrides for the standard allocation functions that allow
  36. // a given allocation to be made to fail for testing. If the program is run
  37. // with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
  38. // return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation
  39. // will signal SIGTRAP rather than return NULL.
  40. //
  41. // This code is not thread safe.
  42. static uint64_t current_malloc_count = 0;
  43. static uint64_t malloc_number_to_fail = 0;
  44. static bool failure_enabled = false, break_on_fail = false, in_call = false;
  45. extern "C" {
  46. // These are other names for the standard allocation functions.
  47. extern void *__libc_malloc(size_t size);
  48. extern void *__libc_calloc(size_t num_elems, size_t size);
  49. extern void *__libc_realloc(void *ptr, size_t size);
  50. }
  51. static void exit_handler(void) {
  52. if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
  53. _exit(88);
  54. }
  55. }
  56. static void cpp_new_handler() {
  57. // Return to try again. It won't fail a second time.
  58. return;
  59. }
  60. // should_fail_allocation returns true if the current allocation should fail.
  61. static bool should_fail_allocation() {
  62. static bool init = false;
  63. if (in_call) {
  64. return false;
  65. }
  66. in_call = true;
  67. if (!init) {
  68. const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
  69. if (env != NULL && env[0] != 0) {
  70. char *endptr;
  71. malloc_number_to_fail = strtoull(env, &endptr, 10);
  72. if (*endptr == 0) {
  73. failure_enabled = true;
  74. atexit(exit_handler);
  75. std::set_new_handler(cpp_new_handler);
  76. }
  77. }
  78. break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
  79. init = true;
  80. }
  81. in_call = false;
  82. if (!failure_enabled) {
  83. return false;
  84. }
  85. bool should_fail = (current_malloc_count == malloc_number_to_fail);
  86. current_malloc_count++;
  87. if (should_fail && break_on_fail) {
  88. raise(SIGTRAP);
  89. }
  90. return should_fail;
  91. }
  92. extern "C" {
  93. void *malloc(size_t size) {
  94. if (should_fail_allocation()) {
  95. errno = ENOMEM;
  96. return NULL;
  97. }
  98. return __libc_malloc(size);
  99. }
  100. void *calloc(size_t num_elems, size_t size) {
  101. if (should_fail_allocation()) {
  102. errno = ENOMEM;
  103. return NULL;
  104. }
  105. return __libc_calloc(num_elems, size);
  106. }
  107. void *realloc(void *ptr, size_t size) {
  108. if (should_fail_allocation()) {
  109. errno = ENOMEM;
  110. return NULL;
  111. }
  112. return __libc_realloc(ptr, size);
  113. }
  114. } // extern "C"
  115. #endif // defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN && !TSAN