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