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.
 
 
 
 
 
 

149 rivejä
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 bool failure_enabled = false, break_on_fail = false, in_call = false;
  49. extern "C" {
  50. // These are other names for the standard allocation functions.
  51. extern void *__libc_malloc(size_t size);
  52. extern void *__libc_calloc(size_t num_elems, size_t size);
  53. extern void *__libc_realloc(void *ptr, size_t size);
  54. }
  55. static void exit_handler(void) {
  56. if (failure_enabled && current_malloc_count > malloc_number_to_fail) {
  57. _exit(88);
  58. }
  59. }
  60. static void cpp_new_handler() {
  61. // Return to try again. It won't fail a second time.
  62. return;
  63. }
  64. // should_fail_allocation returns true if the current allocation should fail.
  65. static bool should_fail_allocation() {
  66. static bool init = false;
  67. if (in_call) {
  68. return false;
  69. }
  70. in_call = true;
  71. if (!init) {
  72. const char *env = getenv("MALLOC_NUMBER_TO_FAIL");
  73. if (env != NULL && env[0] != 0) {
  74. char *endptr;
  75. malloc_number_to_fail = strtoull(env, &endptr, 10);
  76. if (*endptr == 0) {
  77. failure_enabled = true;
  78. atexit(exit_handler);
  79. std::set_new_handler(cpp_new_handler);
  80. }
  81. }
  82. break_on_fail = (NULL != getenv("MALLOC_BREAK_ON_FAIL"));
  83. init = true;
  84. }
  85. in_call = false;
  86. if (!failure_enabled) {
  87. return false;
  88. }
  89. bool should_fail = (current_malloc_count == malloc_number_to_fail);
  90. current_malloc_count++;
  91. if (should_fail && break_on_fail) {
  92. raise(SIGTRAP);
  93. }
  94. return should_fail;
  95. }
  96. extern "C" {
  97. void *malloc(size_t size) {
  98. if (should_fail_allocation()) {
  99. errno = ENOMEM;
  100. return NULL;
  101. }
  102. return __libc_malloc(size);
  103. }
  104. void *calloc(size_t num_elems, size_t size) {
  105. if (should_fail_allocation()) {
  106. errno = ENOMEM;
  107. return NULL;
  108. }
  109. return __libc_calloc(num_elems, size);
  110. }
  111. void *realloc(void *ptr, size_t size) {
  112. if (should_fail_allocation()) {
  113. errno = ENOMEM;
  114. return NULL;
  115. }
  116. return __libc_realloc(ptr, size);
  117. }
  118. } // extern "C"
  119. #endif /* defined(linux) && GLIBC && !ARM && !AARCH64 && !ASAN */