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.
 
 
 
 
 
 

151 lines
4.0 KiB

  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(__has_feature)
  16. #if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
  17. #define OPENSSL_ASAN
  18. #endif
  19. #endif
  20. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  21. #define OPENSSL_GLIBC
  22. #endif
  23. // This file isn't built on ARM or Aarch64 because we link statically in those
  24. // builds and trying to override malloc in a static link doesn't work. It also
  25. // requires glibc. It's also disabled on ASan builds as this interferes with
  26. // ASan's malloc interceptor.
  27. //
  28. // TODO(davidben): See if this and ASan's and MSan's interceptors can be made to
  29. // coexist.
  30. #if defined(__linux__) && defined(OPENSSL_GLIBC) && !defined(OPENSSL_ARM) && \
  31. !defined(OPENSSL_AARCH64) && !defined(OPENSSL_ASAN)
  32. #include <errno.h>
  33. #include <signal.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <new>
  39. /* This file defines overrides for the standard allocation functions that allow
  40. * a given allocation to be made to fail for testing. If the program is run
  41. * with MALLOC_NUMBER_TO_FAIL set to a base-10 number then that allocation will
  42. * return NULL. If MALLOC_BREAK_ON_FAIL is also defined then the allocation
  43. * will signal SIGTRAP rather than return NULL.
  44. *
  45. * This code is not thread safe. */
  46. static uint64_t current_malloc_count = 0;
  47. static uint64_t malloc_number_to_fail = 0;
  48. static char failure_enabled = 0, break_on_fail = 0;
  49. static int in_call = 0;
  50. extern "C" {
  51. /* These are other names for the standard allocation functions. */
  52. extern void *__libc_malloc(size_t size);
  53. extern void *__libc_calloc(size_t num_elems, size_t size);
  54. extern void *__libc_realloc(void *ptr, size_t size);
  55. }
  56. static void exit_handler(void) {
  57. if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
  58. _exit(88);
  59. }
  60. }
  61. static void cpp_new_handler() {
  62. // Return to try again. It won't fail a second time.
  63. return;
  64. }
  65. /* should_fail_allocation returns true if the current allocation should fail. */
  66. static int should_fail_allocation() {
  67. static int init = 0;
  68. char should_fail;
  69. if (in_call) {
  70. return 0;
  71. }
  72. in_call = 1;
  73. if (!init) {
  74. const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
  75. if (env != NULL && env[0] != 0) {
  76. char *endptr;
  77. malloc_number_to_fail = strtoull(env, &endptr, 10);
  78. if (*endptr == 0) {
  79. failure_enabled = 1;
  80. atexit(exit_handler);
  81. std::set_new_handler(cpp_new_handler);
  82. }
  83. }
  84. break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
  85. init = 1;
  86. }
  87. in_call = 0;
  88. if (!failure_enabled) {
  89. return 0;
  90. }
  91. should_fail = (current_malloc_count == malloc_number_to_fail);
  92. current_malloc_count++;
  93. if (should_fail && break_on_fail) {
  94. raise(SIGTRAP);
  95. }
  96. return should_fail;
  97. }
  98. extern "C" {
  99. void *malloc(size_t size) {
  100. if (should_fail_allocation()) {
  101. errno = ENOMEM;
  102. return NULL;
  103. }
  104. return __libc_malloc(size);
  105. }
  106. void *calloc(size_t num_elems, size_t size) {
  107. if (should_fail_allocation()) {
  108. errno = ENOMEM;
  109. return NULL;
  110. }
  111. return __libc_calloc(num_elems, size);
  112. }
  113. void *realloc(void *ptr, size_t size) {
  114. if (should_fail_allocation()) {
  115. errno = ENOMEM;
  116. return NULL;
  117. }
  118. return __libc_realloc(ptr, size);
  119. }
  120. } // extern "C"
  121. #endif /* defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN */