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.

malloc.cc 3.4 KiB

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