Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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