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